-
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathviews.py
More file actions
209 lines (162 loc) · 6.96 KB
/
views.py
File metadata and controls
209 lines (162 loc) · 6.96 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import swapper
from rest_framework.generics import (
ListAPIView,
ListCreateAPIView,
RetrieveUpdateDestroyAPIView,
)
from rest_framework.response import Response
from rest_framework.views import APIView
from openwisp_users.api.authentication import BearerAuthentication
from openwisp_users.api.mixins import (
FilterByOrganizationManaged,
FilterByOrganizationMembership,
FilterByOrganizationOwned,
FilterByParentManaged,
FilterByParentMembership,
FilterByParentOwned,
)
from openwisp_users.api.permissions import (
BaseOrganizationPermission,
IsOrganizationManager,
IsOrganizationMember,
IsOrganizationOwner,
ViewDjangoModelPermissions,
)
from .models import Book, Config, Shelf, Template
from .serializers import (
BookManagerSerializer,
BookMemberSerializer,
BookOwnerSerializer,
BookSerializer,
ShelfSerializer,
TemplateSerializer,
)
Organization = swapper.load_model('openwisp_users', 'Organization')
class BookOrgMixin:
def get_parent_queryset(self):
shelf_org = Shelf.objects.get(pk=self.kwargs['shelf_id']).organization
qs = Book.objects.filter(organization=shelf_org.pk)
return qs
class BaseGetApiView(APIView):
queryset = Template.objects.all()
def get(self, request, *args, **kwargs):
testorg, _ = Organization.objects.get_or_create(name='test org')
template, _ = Template.objects.get_or_create(
name='sample', organization=testorg
)
self.check_object_permissions(request, template)
return Response({})
class ApiMemberView(BaseGetApiView):
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationMember,)
class ApiManagerView(BaseGetApiView):
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationManager,)
class ApiOwnerView(BaseGetApiView):
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationOwner,)
class BaseOrganizationPermissionView(BaseGetApiView):
authentication_classes = (BearerAuthentication,)
permission_classes = (BaseOrganizationPermission,)
class OrganizationFieldView(APIView):
queryset = Config.objects.all()
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationMember,)
organization_field = 'template__organization'
def get(self, request, *args, **kwargs):
testorg, _ = Organization.objects.get_or_create(name='test org')
temp1, _ = Template.objects.get_or_create(name='temp', organization=testorg)
config, _ = Config.objects.get_or_create(template=temp1, organization=testorg)
self.check_object_permissions(request, config)
return Response({})
class ErrorOrganizationFieldView(BaseGetApiView):
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationMember,)
organization_field = 'error__organization'
class ShelfListMemberView(FilterByOrganizationMembership, ListAPIView):
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationMember,)
serializer_class = ShelfSerializer
queryset = Shelf.objects.all()
class ShelfListManagerView(FilterByOrganizationManaged, ListAPIView):
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationManager,)
serializer_class = ShelfSerializer
queryset = Shelf.objects.all()
class ShelfListOwnerView(FilterByOrganizationOwned, ListAPIView):
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationOwner,)
serializer_class = ShelfSerializer
queryset = Shelf.objects.all()
class BooksListMemberView(BookOrgMixin, FilterByParentMembership, ListCreateAPIView):
queryset = Book.objects.none()
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationMember,)
serializer_class = BookMemberSerializer
def get_queryset(self):
shelf = Shelf.objects.get(pk=self.kwargs['shelf_id'])
super().get_queryset()
return shelf.book_set.all()
class BooksListManagerView(BookOrgMixin, FilterByParentManaged, ListCreateAPIView):
queryset = Book.objects.none()
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationManager,)
serializer_class = BookManagerSerializer
def get_queryset(self):
shelf = Shelf.objects.get(pk=self.kwargs['shelf_id'])
super().get_queryset()
return shelf.book_set.all()
class BooksListOwnerView(BookOrgMixin, FilterByParentOwned, ListCreateAPIView):
queryset = Book.objects.none()
authentication_classes = (BearerAuthentication,)
permission_classes = (IsOrganizationOwner,)
serializer_class = BookOwnerSerializer
def get_queryset(self):
shelf = Shelf.objects.get(pk=self.kwargs['shelf_id'])
super().get_queryset()
return shelf.book_set.all()
# some views may not contain `permission_classes` and have to be tested separately
class ShelfListUnauthorizedView(FilterByOrganizationMembership, ListAPIView):
authentication_classes = (BearerAuthentication,)
serializer_class = ShelfSerializer
queryset = Shelf.objects.all()
class BooksListUnauthorizedView(BookOrgMixin, FilterByParentOwned, ListAPIView):
queryset = Book.objects.none()
authentication_classes = (BearerAuthentication,)
serializer_class = BookSerializer
def get_queryset(self):
shelf = Shelf.objects.get(pk=self.kwargs['shelf_id'])
super().get_queryset()
return shelf.book_set.all()
class TemplateListCreateView(FilterByOrganizationManaged, ListCreateAPIView):
serializer_class = TemplateSerializer
authentication_classes = (BearerAuthentication,)
permission_classes = (
IsOrganizationMember,
ViewDjangoModelPermissions,
)
queryset = Template.objects.all()
class TemplateDetailView(FilterByOrganizationManaged, RetrieveUpdateDestroyAPIView):
serializer_class = TemplateSerializer
authentication_classes = (BearerAuthentication,)
permission_classes = (
IsOrganizationMember,
ViewDjangoModelPermissions,
)
queryset = Template.objects.all()
api_member_view = ApiMemberView.as_view()
api_manager_view = ApiManagerView.as_view()
api_owner_view = ApiOwnerView.as_view()
base_org_view = BaseOrganizationPermissionView.as_view()
org_field_view = OrganizationFieldView.as_view()
error_field_view = ErrorOrganizationFieldView.as_view()
books_list_member_view = BooksListMemberView.as_view()
books_list_manager_view = BooksListManagerView.as_view()
books_list_owner_view = BooksListOwnerView.as_view()
book_list_unauthorized_view = BooksListUnauthorizedView.as_view()
shelf_list_unauthorized_view = ShelfListUnauthorizedView.as_view()
shelf_list_member_view = ShelfListMemberView.as_view()
shelf_list_manager_view = ShelfListManagerView.as_view()
shelf_list_owner_view = ShelfListOwnerView.as_view()
template_list = TemplateListCreateView.as_view()
template_detail = TemplateDetailView.as_view()