4
4
5
5
from django .db import transaction
6
6
from core .utils import get_client_ip_address
7
- from core .settings import MAX_FILES_PER_UPLOAD
7
+ from core .settings import MAX_FILES_PER_UPLOAD , MAX_FILE_SIZE_IN_MB
8
8
9
9
from rest_framework import status
10
10
from rest_framework .parsers import FormParser , MultiPartParser
11
11
from rest_framework .response import Response
12
12
from rest_framework .views import APIView
13
13
from rest_framework .exceptions import APIException
14
14
from rest_framework .permissions import IsAuthenticated
15
- from rest_framework .authentication import SessionAuthentication , BasicAuthentication , TokenAuthentication
16
15
from rest_framework .throttling import UserRateThrottle
17
16
from rest_framework .throttling import ScopedRateThrottle
18
17
from rest_framework .decorators import throttle_classes
33
32
class ValidationRequestDetailAPIView (APIView ):
34
33
35
34
queryset = ValidationRequest .objects .all ()
36
- authentication_classes = [SessionAuthentication , TokenAuthentication , BasicAuthentication ]
37
35
permission_classes = [IsAuthenticated ]
38
36
parser_classes = (MultiPartParser , FormParser )
39
37
serializer_class = ValidationRequestSerializer
@@ -78,7 +76,6 @@ def delete(self, request, id, *args, **kwargs):
78
76
class ValidationRequestListAPIView (APIView ):
79
77
80
78
queryset = ValidationRequest .objects .all ()
81
- authentication_classes = [SessionAuthentication , TokenAuthentication , BasicAuthentication ]
82
79
permission_classes = [IsAuthenticated ]
83
80
parser_classes = (MultiPartParser , FormParser )
84
81
serializer_class = ValidationRequestSerializer
@@ -90,7 +87,6 @@ def get_throttles(self):
90
87
"""
91
88
Applies scoped throttling only for POST requests (aka submitting a new Validation Request).
92
89
"""
93
- logger .info ('*** ' + self .request .method )
94
90
return [ScopedRateThrottle ()] if self .request .method == 'POST' else [UserRateThrottle ()]
95
91
96
92
@extend_schema (operation_id = 'validationrequest_list' )
@@ -103,6 +99,10 @@ def get(self, request, *args, **kwargs):
103
99
logger .info ('API request - User IP: %s Request Method: %s Request URL: %s Content-Length: %s' % (get_client_ip_address (request ), request .method , request .path , request .META .get ('CONTENT_LENGTH' )))
104
100
105
101
user_requests = ValidationRequest .objects .filter (created_by__id = request .user .id , deleted = False )
102
+ public_id = self .request .query_params .get ('public_id' , None )
103
+ if public_id :
104
+ user_requests = user_requests .filter (id = ValidationRequest .to_private_id (public_id ))
105
+
106
106
serializer = self .serializer_class (user_requests , many = True )
107
107
return Response (serializer .data , status = status .HTTP_200_OK )
108
108
@@ -132,14 +132,29 @@ def post(self, request, *args, **kwargs):
132
132
if file_i is not None : files += file_i
133
133
logger .info (f"Received { len (files )} file(s) - files: { files } " )
134
134
135
+ # only accept one file (for now)
136
+ if len (files ) != 1 :
137
+ data = {'message' : f"Only one file can be uploaded at a time." }
138
+ return Response (data , status = status .HTTP_400_BAD_REQUEST )
139
+
135
140
# retrieve file size and save
136
141
uploaded_file = serializer .validated_data
137
142
logger .info (f'uploaded_file = { uploaded_file } ' )
138
143
f = uploaded_file ['file' ]
139
144
f .seek (0 , 2 )
140
145
file_length = f .tell ()
141
146
file_name = uploaded_file ['file_name' ]
142
- logger .info (f"file_length for uploaded file { file_name } = { file_length } " )
147
+ logger .info (f"file_length for uploaded file { file_name } = { file_length } ({ file_length / (1024 * 1024 )} MB)" )
148
+
149
+ # check if file name ends with .ifc
150
+ if not file_name .lower ().endswith ('.ifc' ):
151
+ data = {'file_name' : "File name must end with '.ifc'." }
152
+ return Response (data , status = status .HTTP_400_BAD_REQUEST )
153
+
154
+ # apply file size limit
155
+ if file_length > MAX_FILE_SIZE_IN_MB * 1024 * 1024 :
156
+ data = {'message' : f"File size exceeds allowed file size limit ({ MAX_FILE_SIZE_IN_MB } MB)." }
157
+ return Response (data , status = status .HTTP_413_REQUEST_ENTITY_TOO_LARGE )
143
158
144
159
# can't use this, file hasn't been saved yet
145
160
#file = os.path.join(MEDIA_ROOT, uploaded_file['file_name'])
@@ -167,7 +182,6 @@ def submit_task(instance):
167
182
class ValidationTaskDetailAPIView (APIView ):
168
183
169
184
queryset = ValidationTask .objects .all ()
170
- authentication_classes = [SessionAuthentication , BasicAuthentication , TokenAuthentication ]
171
185
permission_classes = [IsAuthenticated ]
172
186
serializer_class = ValidationTaskSerializer
173
187
throttle_classes = [UserRateThrottle ]
@@ -193,7 +207,6 @@ def get(self, request, id, *args, **kwargs):
193
207
class ValidationTaskListAPIView (APIView ):
194
208
195
209
queryset = ValidationTask .objects .all ()
196
- authentication_classes = [SessionAuthentication , BasicAuthentication , TokenAuthentication ]
197
210
permission_classes = [IsAuthenticated ]
198
211
serializer_class = ValidationTaskSerializer
199
212
throttle_classes = [UserRateThrottle ]
@@ -224,7 +237,6 @@ def get(self, request, *args, **kwargs):
224
237
class ValidationOutcomeDetailAPIView (APIView ):
225
238
226
239
queryset = ValidationOutcome .objects .all ()
227
- authentication_classes = [SessionAuthentication , BasicAuthentication , TokenAuthentication ]
228
240
permission_classes = [IsAuthenticated ]
229
241
serializer_class = ValidationOutcomeSerializer
230
242
throttle_classes = [UserRateThrottle ]
@@ -250,7 +262,6 @@ def get(self, request, id, *args, **kwargs):
250
262
class ValidationOutcomeListAPIView (APIView ):
251
263
252
264
queryset = ValidationOutcome .objects .all ()
253
- authentication_classes = [SessionAuthentication , BasicAuthentication , TokenAuthentication ]
254
265
permission_classes = [IsAuthenticated ]
255
266
serializer_class = ValidationOutcomeSerializer
256
267
throttle_classes = [UserRateThrottle ]
@@ -285,7 +296,6 @@ def get(self, request, *args, **kwargs):
285
296
class ModelDetailAPIView (APIView ):
286
297
287
298
queryset = Model .objects .all ()
288
- authentication_classes = [SessionAuthentication , BasicAuthentication , TokenAuthentication ]
289
299
permission_classes = [IsAuthenticated ]
290
300
serializer_class = ModelSerializer
291
301
throttle_classes = [UserRateThrottle ]
@@ -311,7 +321,6 @@ def get(self, request, id, *args, **kwargs):
311
321
class ModelListAPIView (APIView ):
312
322
313
323
queryset = Model .objects .all ()
314
- authentication_classes = [SessionAuthentication , BasicAuthentication , TokenAuthentication ]
315
324
permission_classes = [IsAuthenticated ]
316
325
serializer_class = ModelSerializer
317
326
throttle_classes = [UserRateThrottle ]
0 commit comments