1 |
1 |
'''Views for Authentic2 API'''
|
|
2 |
import json
|
2 |
3 |
import smtplib
|
3 |
4 |
|
4 |
5 |
from django.db import models
|
... | ... | |
11 |
12 |
from django_rbac.utils import get_ou_model
|
12 |
13 |
|
13 |
14 |
from rest_framework import serializers
|
|
15 |
from rest_framework.viewsets import ModelViewSet
|
|
16 |
from rest_framework.routers import SimpleRouter
|
14 |
17 |
from rest_framework.generics import GenericAPIView
|
|
18 |
from rest_framework.mixins import ListModelMixin, CreateModelMixin
|
15 |
19 |
from rest_framework.response import Response
|
16 |
20 |
from rest_framework import authentication, permissions, status
|
17 |
21 |
from rest_framework.exceptions import PermissionDenied
|
18 |
22 |
|
19 |
|
from . import utils, decorators
|
|
23 |
from . import utils, decorators, models
|
20 |
24 |
|
21 |
25 |
|
22 |
26 |
class HasUserAddPermission(permissions.BasePermission):
|
... | ... | |
202 |
206 |
if request.user.is_anonymous():
|
203 |
207 |
return {}
|
204 |
208 |
return request.user.to_json()
|
|
209 |
|
|
210 |
|
|
211 |
def get_user_class():
|
|
212 |
user_class = get_user_model()
|
|
213 |
for at in models.Attribute.objects.filter(kind='string'):
|
|
214 |
def get_property(self):
|
|
215 |
try:
|
|
216 |
return json.loads(models.AttributeValue.objects.with_owner(self).get(attribute=at).content)
|
|
217 |
except models.AttributeValue.DoesNotExist:
|
|
218 |
return ''
|
|
219 |
def set_property(self, value):
|
|
220 |
at.set_value(self, value)
|
|
221 |
setattr(user_class, at.name, property(get_property, set_property))
|
|
222 |
return user_class
|
|
223 |
|
|
224 |
|
|
225 |
class UserSerializer(serializers.ModelSerializer):
|
|
226 |
zob = serializers.CharField()
|
|
227 |
|
|
228 |
def create(self, validated_data):
|
|
229 |
extra_field = {}
|
|
230 |
for at in models.Attribute.objects.filter(kind='string'):
|
|
231 |
if at.name in validated_data:
|
|
232 |
extra_field[at.name] = validated_data.pop(at.name)
|
|
233 |
instance = super(UserSerializer, self).create(validated_data)
|
|
234 |
for key, value in extra_field.iteritems():
|
|
235 |
setattr(instance, key, value)
|
|
236 |
print validated_data
|
|
237 |
if 'password' in validated_data:
|
|
238 |
instance.set_password(validated_data['password'])
|
|
239 |
instance.save()
|
|
240 |
return instance
|
|
241 |
|
|
242 |
def update(self, instance, validated_data):
|
|
243 |
extra_field = {}
|
|
244 |
for at in models.Attribute.objects.filter(kind='string'):
|
|
245 |
if at.name in validated_data:
|
|
246 |
extra_field[at.name] = validated_data.pop(at.name)
|
|
247 |
super(UserSerializer, self).update(instance, validated_data)
|
|
248 |
for key, value in extra_field.iteritems():
|
|
249 |
setattr(instance, key, value)
|
|
250 |
if 'password' in validated_data:
|
|
251 |
instance.set_password(validated_data['password'])
|
|
252 |
instance.save()
|
|
253 |
return instance
|
|
254 |
|
|
255 |
class Meta:
|
|
256 |
model = get_user_model()
|
|
257 |
exclude = ('date_joined', 'user_permissions', 'groups', 'last_login')
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
class UsersAPI(ModelViewSet):
|
|
262 |
def get_serializer_class(self):
|
|
263 |
return UserSerializer
|
|
264 |
|
|
265 |
def get_queryset(sel):
|
|
266 |
User = get_user_class()
|
|
267 |
return User.objects.all()
|
|
268 |
|
|
269 |
|
|
270 |
router = SimpleRouter()
|
|
271 |
router.register(r'users', UsersAPI, base_name='a2-api-users')
|
205 |
|
-
|