1- # For getting objects out of the database.
2- from .. utilities import DbUtils
1+ #!/usr/bin/env python3
2+ """Create a prefix
33
4- # Checking that a user is in a group.
5- from ..utilities import UserUtils
4+ Create a prefix to be used to classify BCOs and to determine permissions
5+ for objects created under that prefix. The requestor must be in the group
6+ prefix_admins to create a prefix.
7+ """
68
7- # Model fields
8- from ...models import prefixes
9-
10- # Checking prefixes
119import re
1210
13- # Responses
11+ from api .scripts .utilities import DbUtils
12+ from api .scripts .utilities import UserUtils
13+ from api .models import prefixes
14+
1415from rest_framework import status
1516from rest_framework .response import Response
1617
1718
1819
1920
20- def POST_api_prefixes_create (
21- incoming
22- ):
21+ def POST_api_prefixes_create (request ):
22+ """Create a prefix
23+
24+ Parameters
25+ ----------
26+ request: rest_framework.request.Request
27+ Django request object.
28+
29+ Returns
30+ -------
31+ rest_framework.response.Response
32+ An HttpResponse that allows its data to be rendered into
33+ arbitrary media types.
34+ """
35+
36+ # TODO: replace user/group looping with basic filtering
37+ # on the model. Loops now are slower than a single
38+ # filter call.
2339
2440 # Instantiate any necessary imports.
25- db = DbUtils .DbUtils ()
26- uu = UserUtils .UserUtils ()
41+ db_utils = DbUtils .DbUtils ()
42+ user_utils = UserUtils .UserUtils ()
2743
2844 # Define the bulk request.
29- bulk_request = incoming .data ['POST_api_prefixes_create' ][ 'prefixes ' ]
45+ bulk_request = request .data ['POST_api_prefixes_create' ]
3046
3147 # Get all existing prefixes.
3248 available_prefixes = list (
33- prefixes .objects .all ().values_list (
34- 'prefix' ,
35- flat = True
36- )
37- )
49+ prefixes .objects .all ().values_list ('prefix' , flat = True ))
3850
3951 # Construct an array to return information about processing
4052 # the request.
@@ -43,130 +55,92 @@ def POST_api_prefixes_create(
4355 # Since bulk_request is an array, go over each
4456 # item in the array.
4557 for creation_object in bulk_request :
46-
47- # Create a list to hold information about errors.
48- errors = {}
49-
50- # Standardize the prefix name.
51- standardized = creation_object ['prefix' ].upper ()
52-
53- # TODO: abstract this error check to schema checker?
54-
55- # Check for each error.
56-
57- # Create a flag for if one of these checks fails.
58- error_check = False
59-
60- # Does the prefix follow the regex for prefixes?
61- if not re .match (
62- r"^[A-Z]{3,5}$" ,
63- standardized
64- ):
65-
66- error_check = True
67-
68- # Bad request because the prefix doesn't follow
69- # the naming rules.
70- errors ['400_bad_request_malformed_prefix' ] = db .messages (
71- parameters = {
72- 'prefix' : standardized .upper ()
73- }
74- )['400_bad_request_malformed_prefix' ]
75-
76- # Has the prefix already been created?
77- if standardized in available_prefixes :
78-
79- error_check = True
80-
81- # Update the request status.
82- errors ['409_prefix_conflict' ] = db .messages (
83- parameters = {
84- 'prefix' : standardized .upper ()
85- }
86- )['409_prefix_conflict' ]
87-
88- # Does the user exist?
89- if uu .check_user_exists (un = creation_object ['owner_user' ]) is False :
90-
91- error_check = True
92-
93- # Bad request.
94- errors ['404_user_not_found' ] = db .messages (
95- parameters = {
96- 'username' : creation_object ['owner_user' ]
97- }
98- )['404_user_not_found' ]
99-
100- # Does the group exist?
101- if uu .check_group_exists (n = creation_object ['owner_group' ]) is False :
102-
103- error_check = True
104-
105- # Bad request.
106- errors ['404_group_not_found' ] = db .messages (
107- parameters = {
108- 'group' : creation_object ['owner_group' ]
109- }
110- )['404_group_not_found' ]
111-
112- # Was the expiration date validly formatted and, if so,
113- # is it after right now?
114- if 'expiration_date' in creation_object :
115- if db .check_expiration (dt_string = creation_object ['expiration_date' ]) is not None :
58+ # Go over each prefix proposed.
59+ for prfx in creation_object ['prefixes' ]:
60+ # Create a list to hold information about errors.
61+ errors = {}
62+ # Standardize the prefix name.
63+ standardized = prfx ['prefix' ].upper ()
64+ # TODO: abstract this error check to schema checker?
65+ # Check for each error.
66+ # Create a flag for if one of these checks fails.
67+ error_check = False
68+ # Does the prefix follow the regex for prefixes?
69+ if not re .match (r"^[A-Z]{3,5}$" , standardized ):
70+ error_check = True
71+ # Bad request because the prefix doesn't follow
72+ # the naming rules.
73+ errors ['400_bad_request_malformed_prefix' ] = db_utils .messages (
74+ parameters = {'prefix' : standardized .upper ()}
75+ )['400_bad_request_malformed_prefix' ]
76+
77+ # Has the prefix already been created?
78+ if standardized in available_prefixes :
79+ error_check = True
80+ # Update the request status.
81+ errors ['409_prefix_conflict' ] = db_utils .messages (
82+ parameters = {
83+ 'prefix' : standardized .upper ()
84+ }
85+ )['409_prefix_conflict' ]
86+
87+ # Does the user exist?
88+ if user_utils .check_user_exists (un = creation_object ['owner_user' ]) is False :
11689
11790 error_check = True
118-
91+
11992 # Bad request.
120- errors ['400_invalid_expiration_date' ] = db .messages (
121- parameters = {
122- 'expiration_date' : creation_object ['expiration_date' ]
123- }
124- )['400_invalid_expiration_date' ]
125-
126- # Did any check fail?
127- if error_check is False :
128- # The prefix has not been created, so create it.
129- DbUtils .DbUtils ().write_object (
130- p_app_label = 'api' ,
131- p_model_name = 'prefixes' ,
132- p_fields = ['created_by' , 'description' , 'owner_group' , 'owner_user' , 'prefix' ],
133- p_data = {
134- 'created_by' : uu .user_from_request (
135- request = incoming
136- ).username ,
137- 'description' : creation_object ['description' ],
138- 'owner_group' : creation_object ['owner_group' ],
139- 'owner_user' : creation_object ['owner_user' ],
140- 'prefix' : standardized
141- }
142- )
143-
144- DbUtils .DbUtils ().write_object (
145- p_app_label = 'api' ,
146- p_model_name = 'prefix_table' ,
147- p_fields = ['n_objects' , 'prefix' ],
148- p_data = {
149- 'n_objects' : 0 ,
150- 'prefix' : standardized
151- }
152- )
153-
154- # Created the prefix.
155- errors ['201_prefix_create' ] = db .messages (
156- parameters = {
93+ errors ['404_user_not_found' ] = db_utils .messages (
94+ parameters = {'username' : creation_object ['owner_user' ]}
95+ )['404_user_not_found' ]
96+
97+ # Does the group exist?
98+ if user_utils .check_group_exists (n = creation_object ['owner_group' ]) is False :
99+ error_check = True
100+ # Bad request.
101+ errors ['404_group_not_found' ] = db_utils .messages (
102+ parameters = {'group' : creation_object ['owner_group' ]}
103+ )['404_group_not_found' ]
104+
105+ # Was the expiration date validly formatted and, if so,
106+ # is it after right now?
107+ if 'expiration_date' in prfx :
108+ if db_utils .check_expiration (dt_string = prfx ['expiration_date' ]) is not None :
109+ error_check = True
110+ # Bad request.
111+ errors ['400_invalid_expiration_date' ] = db_utils .messages (
112+ parameters = {
113+ 'expiration_date' : prfx ['expiration_date' ]}
114+ )['400_invalid_expiration_date' ]
115+ # Did any check fail?
116+ if error_check is False :
117+ # The prefix has not been created, so create it.
118+ DbUtils .DbUtils ().write_object (
119+ p_app_label = 'api' ,
120+ p_model_name = 'prefixes' ,
121+ p_fields = ['created_by' , 'description' , 'owner_group' , 'owner_user' , 'prefix' ],
122+ p_data = {
123+ 'created_by' : user_utils .user_from_request (
124+ request = request
125+ ).username ,
126+ 'description' : prfx ['description' ],
127+ 'owner_group' : creation_object ['owner_group' ],
128+ 'owner_user' : creation_object ['owner_user' ],
157129 'prefix' : standardized
158130 }
159- )['201_prefix_create' ]
160-
161- # Append the possible "errors".
162- returning .append (errors )
163-
131+ )
132+
133+ # Created the prefix.
134+ errors ['201_prefix_create' ] = db_utils .messages (
135+ parameters = {
136+ 'prefix' : standardized
137+ }
138+ )['201_prefix_create' ]
139+
140+ # Append the possible "errors".
141+ returning .append (errors )
142+
164143 # As this view is for a bulk operation, status 200
165144 # means that the request was successfully processed,
166145 # but NOT necessarily each item in the request.
167- return (
168- Response (
169- status = status .HTTP_200_OK ,
170- data = returning
171- )
172- )
146+ return Response (status = status .HTTP_200_OK , data = returning )
0 commit comments