-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathviews.py
More file actions
296 lines (249 loc) · 10.5 KB
/
Copy pathviews.py
File metadata and controls
296 lines (249 loc) · 10.5 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import os
from api.paginations import CustomPagination
from datasets.serializers import (
DatasetAndDataPartnerViewSerializer,
DatasetEditSerializer,
DatasetViewSerializerV2,
DatasetCreateSerializerV2,
)
from django.db.models.query_utils import Q
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import generics, status
from rest_framework.filters import OrderingFilter
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import (
CreateModelMixin,
DestroyModelMixin,
ListModelMixin,
RetrieveModelMixin,
UpdateModelMixin,
)
from rest_framework.response import Response
from rest_framework.views import APIView
from drf_spectacular.utils import extend_schema
from drf_spectacular.types import OpenApiTypes
from shared.mapping.models import Dataset, VisibilityChoices
from shared.mapping.permissions import (
CanAdmin,
CanEdit,
CanView,
get_user_permissions_on_dataset,
)
class DatasetIndex(GenericAPIView, ListModelMixin, CreateModelMixin):
"""
API view to list all datasets with support for filtering and pagination.
This view allows users to retrieve a list of datasets based on specific
filter criteria such as dataset ID, data partner, and visibility status.
It also supports pagination to handle large datasets efficiently.
- If the request method is POST, the `DatasetCreateSerializerV2` is used
to handle dataset creation.
- For GET requests, the `DatasetViewSerializerV2` is used to serialize
the dataset data.
Filtering options:
- `id`: Filter datasets by their IDs.
- `data_partner`: Filter datasets by their associated data partner.
- `hidden`: Filter datasets based on their hidden status.
The queryset returned depends on the user's permissions and visibility
settings of the datasets.
"""
serializer_class = DatasetViewSerializerV2
filter_backends = [DjangoFilterBackend, OrderingFilter]
ordering_fields = ["id", "name", "created_at", "visibility"]
filterset_fields = {
"id": ["in"],
"data_partner": ["in", "exact"],
"hidden": ["in", "exact"],
}
def get_serializer_class(self):
if self.request.method in ["POST"]:
return DatasetCreateSerializerV2
return super().get_serializer_class()
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
admins = serializer.initial_data.get("admins")
# If no admins given, add the user uploading the dataset
if not admins:
serializer.save(admins=[self.request.user])
# If the user is not in the admins, add them
elif self.request.user.id not in admins:
serializer.save(admins=admins + [self.request.user.id])
# All is well, save
else:
serializer.save()
def get_queryset(self):
"""
If the User is the `AZ_FUNCTION_USER`, return all Datasets.
Else, return only the Datasets which are on projects a user is a member,
which are "PUBLIC", or "RESTRICTED" Datasets that a user is a viewer of.
"""
if self.request.user.username == os.getenv("AZ_FUNCTION_USER"):
return Dataset.objects.all().distinct()
return Dataset.objects.filter(
Q(visibility=VisibilityChoices.PUBLIC)
| Q(
viewers=self.request.user.id,
visibility=VisibilityChoices.RESTRICTED,
)
| Q(
editors=self.request.user.id,
visibility=VisibilityChoices.RESTRICTED,
)
| Q(
admins=self.request.user.id,
visibility=VisibilityChoices.RESTRICTED,
),
project__members=self.request.user.id,
).distinct()
class DatasetAndDataPartnerListView(GenericAPIView, ListModelMixin):
"""
API view to list all datasets with filtering, ordering, and
pagination support.
This view provides a list of datasets based on the user's access
level and membership in projects. It supports filtering by various
fields, ordering by specific attributes, and paginated responses.
Attributes:
serializer_class (DatasetAndDataPartnerViewSerializer): The
serializer used to format the dataset data.
pagination_class (CustomPagination): The pagination class used
to paginate the dataset list.
filter_backends (list): A list of filter backends used for
filtering and ordering the dataset list.
ordering_fields (list): Fields that can be used for ordering
the dataset list.
filterset_fields (dict): Fields that can be used for filtering
the dataset list.
ordering (str): Default ordering for the dataset list.
Methods:
get(request, *args, **kwargs):
Handles GET requests to retrieve the list of datasets.
get_queryset():
Returns the queryset of datasets based on the user's access
level:
- If the user is the `AZ_FUNCTION_USER`, all datasets are
returned.
- Otherwise, only datasets that are public, restricted
datasets the user has access to, or datasets in projects
the user is a member of are returned.
"""
serializer_class = DatasetAndDataPartnerViewSerializer
pagination_class = CustomPagination
filter_backends = [DjangoFilterBackend, OrderingFilter]
ordering_fields = ["id", "name", "created_at", "visibility", "data_partner"]
filterset_fields = {
"id": ["in"],
"hidden": ["in", "exact"],
"name": ["in", "icontains"],
"project": ["exact"],
}
ordering = "-created_at"
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def get_queryset(self):
"""
If the User is the `AZ_FUNCTION_USER`, return all Datasets.
Else, return only the Datasets which are on projects a user is a member,
which are "PUBLIC", or "RESTRICTED" Datasets that a user is a viewer of.
"""
if self.request.user.username == os.getenv("AZ_FUNCTION_USER"):
return Dataset.objects.prefetch_related("data_partner").all().distinct()
return (
Dataset.objects.filter(
Q(visibility=VisibilityChoices.PUBLIC)
| Q(
viewers=self.request.user.id,
visibility=VisibilityChoices.RESTRICTED,
)
| Q(
editors=self.request.user.id,
visibility=VisibilityChoices.RESTRICTED,
)
| Q(
admins=self.request.user.id,
visibility=VisibilityChoices.RESTRICTED,
),
project__members=self.request.user.id,
)
.prefetch_related("data_partner")
.distinct()
.order_by("-id")
)
class DatasetDetail(
GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin
):
"""
Dataset Detail View.
This view provides detailed operations for a Dataset object,
including retrieving, updating, and deleting. The permissions and
serializers used are dynamically determined based on the HTTP
method of the request.
Inherits:
- GenericAPIView: Base class for all API views.
- RetrieveModelMixin: Adds retrieve functionality.
- UpdateModelMixin: Adds update functionality.
- DestroyModelMixin: Adds delete functionality.
Permissions:
- GET: Requires `CanView`, `CanAdmin`, or `CanEdit` permissions.
- POST, PATCH, PUT: Requires `CanView` and either `CanAdmin` or
`CanEdit` permissions.
- DELETE: Requires `CanView` and `CanAdmin` permissions.
Serializers:
- DatasetEditSerializer: Used for POST, PATCH, PUT, and DELETE
requests.
- DatasetViewSerializerV2: Used for GET requests.
Methods:
- initial: Dynamically sets permissions based on the request
method.
- get_queryset: Returns the queryset filtered by the primary
key (`pk`).
- get_serializer_class: Determines the serializer class based
on the request method.
- get_serializer_context: Provides additional context for the
serializer.
- get: Handles GET requests to retrieve a dataset.
- patch: Handles PATCH requests to partially update a dataset.
"""
permission_classes = [CanView | CanAdmin | CanEdit]
def initial(self, request, *args, **kwargs):
self.permission_classes = [CanView | CanAdmin | CanEdit]
if self.request.method in ["POST", "PATCH", "PUT"]:
self.permission_classes = [CanView & (CanAdmin | CanEdit)]
if self.request.method in ["DELETE"]:
self.permission_classes = [CanView & CanAdmin]
return super().initial(request)
def get_queryset(self):
return Dataset.objects.filter(id=self.kwargs.get("pk"))
def get_serializer_class(self):
if self.request.method in ["POST", "PATCH", "PUT", "DELETE"]:
return DatasetEditSerializer
return DatasetViewSerializerV2
def get_serializer_context(self):
return {"projects": self.request.data.get("projects")}
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
class DatasetPermissionView(APIView):
"""
API for retrieving the permissions a user has on a specific dataset.
This view handles GET requests to fetch the permissions associated
with a dataset for the currently authenticated user. Permissions
are determined based on the user's role and access level for the
specified dataset.
Methods:
get(request, pk):
Handles GET requests to retrieve the user's permissions for
the dataset identified by the primary key (pk).
"""
@extend_schema(
responses={
200: OpenApiTypes.OBJECT,
403: OpenApiTypes.OBJECT,
},
description="Get the permissions for a dataset.",
)
def get(self, request, pk):
permissions = get_user_permissions_on_dataset(request, pk)
return Response({"permissions": permissions}, status=status.HTTP_200_OK)