-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
123 lines (105 loc) · 3.51 KB
/
views.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from django.contrib.auth.models import Group
from django.shortcuts import get_object_or_404
from rest_framework import (
viewsets,
generics,
status,
mixins,
views
)
from rest_framework.compat import coreapi
from rest_framework.response import Response
from .models import User, Organization
from .permissions import (
OrgPermissions,
UserPermissions,
UserOrgPermissions
)
from .serializers import (
GroupSerializer,
UserDefaultSerializer,
UserInfoSerializer,
UserCreateSerializer,
UserOrgSerializer,
OrganizationSerializer
)
class GroupList(generics.ListAPIView):
"""
A generic List API for viewing Authentication Groups.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
class InfoAPIView(views.APIView):
"""
API for viewing user and server info.
"""
def get(self, request):
"""
GET user name, organization and server information
Should return {`user_name`, `id`, `organization_name`, `public_ip`}
"""
return Response({
'id': request.user.id,
'user_name': request.user.name,
'organization_name': request.user.organization.name,
'public_ip': request.META.get('REMOTE_ADDR')
})
class OrganizationViewSet(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):
"""
A viewset for viewing and editing org instances.
"""
serializer_class = OrganizationSerializer
queryset = Organization.objects.all()
permission_classes = [OrgPermissions, ]
http_method_names = ['get', 'patch', 'options', 'head']
ordering_fields = []
ordering = []
class UserOrganizationViewSet(viewsets.ReadOnlyModelViewSet):
"""
A viewset for viewing users org instances.
"""
serializer_class = UserOrgSerializer
queryset = User.objects.all()
permission_classes = (UserOrgPermissions,)
ordering_fields = []
ordering = []
def get_queryset(self):
pk = self.kwargs.get('org_id')
try:
org = Organization.objects.get(pk=pk)
return User.objects.filter(organization=org)
except Organization.DoesNotExist:
return User.objects.none()
class UserViewSet(viewsets.ModelViewSet):
"""
A viewset for viewing and editing user instances.
"""
serializer_class = UserInfoSerializer
queryset = User.objects.all()
permission_classes = (UserPermissions,)
http_method_names = ['get', 'post', 'patch', 'delete', 'options', 'head']
filterset_fields = ['phone']
search_fields = ['name', 'email']
ordering_fields = []
ordering = []
def get_serializer_class(self):
if self.action == 'list':
return UserDefaultSerializer
if self.action == 'create':
return UserCreateSerializer
return UserInfoSerializer
def get_queryset(self):
org = self.request.user.organization
return User.objects.filter(organization=org)
def create(self, request):
"""
Create an user for the organization, must set password as well.
Request user must be `Administrator`
"""
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(org=request.user.organization) # set the same org
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)