From 127a6f2f67f5f40d0b147e6d5d2e070095366ad5 Mon Sep 17 00:00:00 2001 From: Hadley King Date: Mon, 28 Mar 2022 20:47:36 -0400 Subject: [PATCH] Update to POST_api_objects_drafts_create.py Changes to be committed: modified: bco_api/api/scripts/method_specific/POST_api_objects_drafts_create.py modified: bco_api/api/views.py --- .../POST_api_objects_drafts_create.py | 85 ++++++++++++++++++- bco_api/api/views.py | 4 +- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/bco_api/api/scripts/method_specific/POST_api_objects_drafts_create.py b/bco_api/api/scripts/method_specific/POST_api_objects_drafts_create.py index 3139fb13..be4365e0 100755 --- a/bco_api/api/scripts/method_specific/POST_api_objects_drafts_create.py +++ b/bco_api/api/scripts/method_specific/POST_api_objects_drafts_create.py @@ -6,6 +6,7 @@ Creates a new BCO draft object. """ +from email import message from api.scripts.utilities import DbUtils, UserUtils from api.models import prefix_table from django.conf import settings @@ -14,7 +15,7 @@ from rest_framework import status from rest_framework.response import Response -def POST_api_objects_drafts_create(request): +def post_api_objects_drafts_create(request): """Create BCO Draft Parameters @@ -49,6 +50,88 @@ def POST_api_objects_drafts_create(request): # Since bulk_request is an array, go over each # item in the array. + + if len(bulk_request) == 1: + creation_object = bulk_request[0] + standardized = creation_object['prefix'].upper() + if 'add_' + standardized in prefix_perms and \ + 'draft_' + standardized in prefix_perms: + if Group.objects.filter( + name = creation_object['owner_group'].lower()).exists(): + constructed_name = object_naming_info['uri_regex'].replace( + 'root_uri', + object_naming_info['root_uri'] + ) + constructed_name = constructed_name.replace('prefix', standardized) + prefix_location = constructed_name.index(standardized) + prefix_length = len(standardized) + constructed_name = constructed_name[0:prefix_location+prefix_length] + prefix_counter = prefix_table.objects.get(prefix=standardized) + creation_object['object_id'] = constructed_name + '_' + \ + '{:06d}'.format(prefix_counter.n_objects) + '/DRAFT' + + creation_object['contents']['object_id'] = creation_object['object_id'] + bco_id = creation_object['object_id'] + owner_group = Group.objects.get(name = creation_object['owner_group']) + creation_object['owner_group'] = owner_group.name + creation_object['owner_user'] = user.username + creation_object['prefix'] = standardized + creation_object['state'] = 'DRAFT' + creation_object['last_update'] = timezone.now() + objects_written = db_utils.write_object( + p_app_label = 'api', + p_model_name = 'bco', + p_fields = [ + 'contents', + 'last_update', + 'object_id', + 'owner_group', + 'owner_user', + 'prefix', + 'schema', + 'state'], + p_data = creation_object + ) + prefix_counter.n_objects = prefix_counter.n_objects + 1 + prefix_counter.save() + + if objects_written < 1: + # Issue with writing out to DB + return Response( + status=status.HTTP_400_BAD_REQUEST, + data='The request could not be processed with the'\ + ' parameters provided.' + ) + + return Response( + status=status.HTTP_201_CREATED, + data= { + 'request_status': 'SUCCESS', + 'status_code' : '201', + 'message' : f'The object with ID {bco_id} was'\ + ' created on the server.', + 'object_id' : bco_id + }, + ) + + else: + # Update the request status. + returning.append(db_utils.messages(parameters = {}) + ['400_bad_request']) + any_failed = True + + else: + # Update the request status. + return Response( + status=status.HTTP_401_UNAUTHORIZED, + data= { + 'request_status': 'FAILURE', + 'status_code' : '401', + 'message' : 'The token provided does not have draft'\ + f'permissions for prefix {standardized}.' + } + ) + for creation_object in bulk_request: # Standardize the prefix. standardized = creation_object['prefix'].upper() diff --git a/bco_api/api/views.py b/bco_api/api/views.py index f951ea36..a2365441 100755 --- a/bco_api/api/views.py +++ b/bco_api/api/views.py @@ -35,7 +35,7 @@ from api.scripts.method_specific.POST_api_accounts_describe import POST_api_accounts_describe from api.scripts.method_specific.POST_api_accounts_new import POST_api_accounts_new -from api.scripts.method_specific.POST_api_objects_drafts_create import POST_api_objects_drafts_create +from api.scripts.method_specific.POST_api_objects_drafts_create import post_api_objects_drafts_create from api.scripts.method_specific.POST_api_objects_drafts_modify import POST_api_objects_drafts_modify from api.scripts.method_specific.POST_api_objects_drafts_permissions import POST_api_objects_drafts_permissions from api.scripts.method_specific.POST_api_objects_drafts_permissions_set import POST_api_objects_drafts_permissions_set @@ -494,7 +494,7 @@ class ApiObjectsDraftsCreate(APIView): 403: "Invalid token." }, tags=["BCO Management"]) def post(self, request) -> Response: - return check_post_and_process(request, POST_api_objects_drafts_create) + return check_post_and_process(request, post_api_objects_drafts_create) class ApiObjectsDraftsModify(APIView):