99from rest_framework .views import APIView
1010from config .services import legacy_api_converter , response_constructor
1111from prefix .services import PrefixSerializer , delete_prefix
12+ from prefix .selectors import get_prefix_object , get_user_prefixes
1213
1314PREFIX_CREATE_SCHEMA = openapi .Schema (
1415 type = openapi .TYPE_ARRAY ,
@@ -180,11 +181,8 @@ def post(self, request) -> Response:
180181 return Response (status = status .HTTP_201_CREATED , data = response_data )
181182
182183class PrefixesDeleteApi (APIView ):
183- """
184- Delete a Prefix [Bulk Enabled]
184+ """Delete a Prefix [Bulk Enabled]
185185
186- # Deletes a prefix for BCOs.
187- --------------------
188186 The requestor *must* be the prefix owner to delete a prefix.
189187
190188 __Any object created under this prefix will have its permissions
@@ -210,7 +208,7 @@ class PrefixesDeleteApi(APIView):
210208 responses = {
211209 200 : "Deleting a prefix was successful." ,
212210 401 : "Unauthorized. Authentication credentials were not provided." ,
213- 403 : "Forbidden. User doesnot have permission to perform this action" ,
211+ 403 : "Forbidden. User does not have permission to perform this action" ,
214212 404 : "The prefix couldn't be found so therefore it could not be deleted." ,
215213 },
216214 tags = ["Prefix Management" ],
@@ -234,7 +232,7 @@ def post(self, request) -> Response:
234232 response_data .append (response_constructor (
235233 identifier = response_id ,
236234 status = "SUCCESS" ,
237- code = 201 ,
235+ code = 200 ,
238236 message = f"Prefix { response_id } deleted" ,
239237 ))
240238 accepted_requests = True
@@ -270,10 +268,7 @@ def post(self, request) -> Response:
270268 return Response (status = status .HTTP_201_CREATED , data = response_data )
271269
272270class PrefixesModifyApi (APIView ):
273- """
274- Modify a Prefix [Bulk Enabled]
275-
276- --------------------
271+ """Modify a Prefix [Bulk Enabled]
277272
278273 Modify a prefix which already exists.
279274
@@ -357,3 +352,134 @@ def post(self, request) -> Response:
357352 )
358353
359354 return Response (status = status .HTTP_201_CREATED , data = response_data )
355+
356+ class PrefixGetInfoApi (APIView ):
357+ """Get Prefix Info [Bulk Enabled]
358+
359+ Returns a serialized Prefix instance. If the Prefix is not public and the
360+ requestor has the apropirate permissions then a dictionary with users
361+ and the associated Prefix permisssions will also be included.
362+ """
363+
364+ permission_classes = [IsAuthenticated ]
365+
366+ request_body = openapi .Schema (
367+ type = openapi .TYPE_ARRAY ,
368+ title = "Prefix Info Schema" ,
369+ description = "Retrieve a serialized Prefix instance." ,
370+ items = openapi .Schema (
371+ type = openapi .TYPE_STRING ,
372+ example = "TEST"
373+ )
374+ )
375+
376+ @swagger_auto_schema (
377+ request_body = request_body ,
378+ responses = {
379+ 200 : "Retrieving prefix info was successful." ,
380+ 401 : "Unauthorized. Authentication credentials were not provided." ,
381+ 403 : "Forbidden. User does not have permission to perform this action" ,
382+ 404 : "That prefix could not be found." ,
383+ },
384+ tags = ["Prefix Management" ],
385+ )
386+
387+ def post (self , request ) -> Response :
388+ response_data = []
389+ requester = request .user
390+ data = request .data
391+ rejected_requests = False
392+ accepted_requests = False
393+
394+ for index , object in enumerate (data ):
395+ response_id = object
396+ response_object = get_prefix_object (object )
397+
398+ try :
399+ if response_object ['fields' ]['public' ] is True or \
400+ requester .username in response_object ['user_permissions' ]:
401+ response_data .append (response_constructor (
402+ identifier = response_id ,
403+ status = "SUCCESS" ,
404+ code = 200 ,
405+ message = f"Prefix { response_id } retrieved" ,
406+ data = response_object
407+ ))
408+ accepted_requests = True
409+ else :
410+ response_data .append (response_constructor (
411+ identifier = response_id ,
412+ status = "FORBIDDEN" ,
413+ code = 403 ,
414+ message = f"User, { requester } , does not have permissions for this Prefix, { response_id } ." ,
415+ ))
416+ rejected_requests = True
417+
418+ except TypeError :
419+ if response_object is None :
420+ response_data .append (response_constructor (
421+ identifier = response_id ,
422+ status = "NOT FOUND" ,
423+ code = 404 ,
424+ message = f"That Prefix, { response_id } , does not exist." ,
425+ ))
426+ rejected_requests = True
427+ else :
428+ response_data .append (response_constructor (
429+ identifier = response_id ,
430+ status = "BAD REQUEST" ,
431+ code = 400 ,
432+ message = f"There was a problem with that Prefix, { response_id } ." ,
433+ ))
434+ rejected_requests = True
435+
436+ if accepted_requests is False :
437+ return Response (
438+ status = status .HTTP_400_BAD_REQUEST ,
439+ data = response_data
440+ )
441+
442+ if accepted_requests is True and rejected_requests is True :
443+ return Response (
444+ status = status .HTTP_207_MULTI_STATUS ,
445+ data = response_data
446+ )
447+
448+ if accepted_requests is True and rejected_requests is False :
449+ return Response (
450+ status = status .HTTP_200_OK ,
451+ data = response_data
452+ )
453+
454+ return Response (status = status .HTTP_201_CREATED , data = response_data )
455+
456+ class PrefixesForUserApi (APIView ):
457+ """Get Prefixes for User
458+
459+ Returns a list of prefixes the requestor is permitted to use.
460+ """
461+
462+ permission_classes = [IsAuthenticated ]
463+
464+ @swagger_auto_schema (
465+ manual_parameters = [
466+ openapi .Parameter (
467+ "Authorization" ,
468+ openapi .IN_HEADER ,
469+ description = "Authorization Token" ,
470+ type = openapi .TYPE_STRING ,
471+ default = "Token 627626823549f787c3ec763ff687169206626149"
472+ )
473+ ],
474+ responses = {
475+ 200 : "Authorization is successful." ,
476+ 403 : "Forbidden. Authentication credentials were not provided." ,
477+ 403 : "Invalid token"
478+ },
479+ tags = ["Prefix Management" ],
480+ )
481+
482+ def post (self , request ) -> Response :
483+ return Response (
484+ status = status .HTTP_200_OK , data = get_user_prefixes (request .user )
485+ )
0 commit comments