class UserViewSet viewsets ModelViewSet queryset User objects filter i

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.filter(is_active=True)
permission_classes = [UserPermission]
filter_backends = AutocompleteFilterBackend,
autocomplete_field = 'acl_auto'
def get_serializer_class(self):
if self.action == 'create':
return InvitationSerializer
if self.action in ['update', 'retrieve', 'partial_update']:
return MyUserSerializer
return UserSerializer
def perform_create(self, serializer):
user, invitation = User.objects.get_or_invite(serializer.validated_data['email'], message=serializer.validated_data['message'],
by=self.request.user, first_name=serializer.validated_data['first_name'],
last_name=serializer.validated_data['last_name'])
@decorators.list_route(methods=['GET'])
def current(self, request):
if not request.user.is_authenticated():
return Response(status=status.HTTP_404_NOT_FOUND)
return Response(MyUserSerializer(request.user).data)
@decorators.detail_route(methods=['POST'])
def upload_avatar(self, request, pk):
fileobj = request.FILES.get('file')
if not fileobj:
return Response({'file': 'No file supplied'}, status=status.HTTP_400_BAD_REQUEST)
user = get_object_or_404(User, pk=pk)
av = Avatar.objects.create(user=user, primary=True)
av.avatar.save(fileobj.name, fileobj)
av.save()
Avatar.objects.filter(user=request.user).exclude(id=av.id).delete()
return Response(status=status.HTTP_200_OK)
@decorators.detail_route(methods=['POST'])
def update_password(self, request, pk):
obj = get_object_or_404(User, pk=pk)
class DummyUser(object):
def __init__(self, *args, **kwargs):
self.id = obj.id
self.password = obj.password
self.has_usable_password = obj.has_usable_password()
def update(self, data):
self.new_password = data['password1']
serializer = ChangePasswordSerializer(DummyUser(), data=request.DATA)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
obj.set_password(serializer.validated_data.new_password)
obj.save()
return Response(status=status.HTTP_200_OK)
@decorators.detail_route(methods=['PUT'])
def update_two_factor_info(self, request, pk):
usr = self.get_object()
serializer = TwoFactorSerializer(usr, data=request.DATA, partial=True)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@decorators.list_route(methods=['POST'])
def logout(self, request):
logout(request)
return Response({}, status=status.HTTP_204_NO_CONTENT)
@decorators.detail_route(methods=['GET'])
def organizations(self, request, pk):
user = self.get_object()
organizations = [membership.organization for membership in OrganizationMembership.objects.filter(user=user)]
return Response(OrganizationSerializer(organizations, many=True).data)