Skip to content

Commit 50c6f15

Browse files
Reeya123HadleyKing
andcommitted
Create_Prefixes (#222)
* Create_Prefix * Update test_api_prefixes_create.py * Update tests and Swagger Docs for `prefixes_create` Fix #172 Changes to be committed: modified: api/model/prefix.py modified: api/views.py modified: tests/test_views/test_api_prefixes_create.py --------- Co-authored-by: hadleyking <[email protected]> Co-authored-by: Hadley King <[email protected]>
1 parent 20f1d16 commit 50c6f15

File tree

4 files changed

+425
-165
lines changed

4 files changed

+425
-165
lines changed

api/model/prefix.py

Lines changed: 76 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,16 @@ def post_api_prefixes_create(request):
9494
return_data = []
9595
any_failed = False
9696
for creation_object in bulk_request:
97-
owner_user = User.objects.get(username=creation_object["owner_user"])
97+
try:
98+
owner_user = User.objects.get(username=creation_object["owner_user"])
99+
except User.DoesNotExist:
100+
return_data.append(
101+
db_utils.messages(parameters={"username": creation_object["owner_user"]})[
102+
"404_user_not_found"
103+
]
104+
)
105+
any_failed = True
106+
continue
98107
if creation_object["owner_group"] == "bco_drafter":
99108
is_public = True
100109
else:
@@ -119,18 +128,6 @@ def post_api_prefixes_create(request):
119128
any_failed = True
120129
continue
121130

122-
if (
123-
user_utils.check_user_exists(user_name=creation_object["owner_user"])
124-
is False
125-
):
126-
return_data.append(
127-
db_utils.messages(parameters={creation_object["owner_user"]})[
128-
"404_user_not_found"
129-
]
130-
)
131-
any_failed = True
132-
continue
133-
134131
if "expiration_date" in prfx:
135132
if (
136133
db_utils.check_expiration(dt_string=prfx["expiration_date"])
@@ -144,86 +141,79 @@ def post_api_prefixes_create(request):
144141
any_failed = True
145142
continue
146143

147-
# Did any check fail?
148-
if any_failed is False:
149-
draft = prfx["prefix"].lower() + "_drafter"
150-
publish = prfx["prefix"].lower() + "_publisher"
151-
152-
if len(Group.objects.filter(name=draft)) != 0:
153-
drafters = Group.objects.get(name=draft)
154-
owner_user.groups.add(drafters)
155-
else:
156-
Group.objects.create(name=draft)
157-
drafters = Group.objects.get(name=draft)
158-
owner_user.groups.add(drafters)
159-
GroupInfo.objects.create(
160-
delete_members_on_group_deletion=False,
161-
description=prfx["description"],
162-
group=drafters,
163-
max_n_members=-1,
164-
owner_user=owner_user,
165-
)
166-
167-
if len(Group.objects.filter(name=publish)) != 0:
168-
publishers = Group.objects.get(name=publish)
169-
owner_user.groups.add(publishers)
170-
else:
171-
Group.objects.create(name=publish)
172-
publishers = Group.objects.get(name=publish)
173-
owner_user.groups.add(publishers)
174-
GroupInfo.objects.create(
175-
delete_members_on_group_deletion=False,
176-
description=prfx["description"],
177-
group=publishers,
178-
max_n_members=-1,
179-
owner_user=owner_user,
180-
)
181-
if is_public is True:
182-
owner_group = "bco_drafter"
183-
else:
184-
owner_group = publish
185-
186-
write_result = DbUtils.DbUtils().write_object(
187-
p_app_label="api",
188-
p_model_name="Prefix",
189-
p_fields=[
190-
"created_by",
191-
"description",
192-
"owner_group",
193-
"owner_user",
194-
"prefix",
195-
],
196-
p_data={
197-
"created_by": user_utils.user_from_request(
198-
request=request
199-
).username,
200-
"description": prfx["description"],
201-
"owner_group": owner_group,
202-
"owner_user": creation_object["owner_user"],
203-
"prefix": standardized,
204-
},
144+
draft = prfx["prefix"].lower() + "_drafter"
145+
publish = prfx["prefix"].lower() + "_publisher"
146+
147+
if len(Group.objects.filter(name=draft)) != 0:
148+
drafters = Group.objects.get(name=draft)
149+
owner_user.groups.add(drafters)
150+
else:
151+
Group.objects.create(name=draft)
152+
drafters = Group.objects.get(name=draft)
153+
owner_user.groups.add(drafters)
154+
GroupInfo.objects.create(
155+
delete_members_on_group_deletion=False,
156+
description=prfx["description"],
157+
group=drafters,
158+
max_n_members=-1,
159+
owner_user=owner_user,
205160
)
206-
if write_result != 1:
207-
return_data.append(
208-
db_utils.messages(parameters={"prefix": standardized})[
209-
"409_prefix_conflict"
210-
]
211-
)
212-
any_failed = True
213-
continue
214161

162+
if len(Group.objects.filter(name=publish)) != 0:
163+
publishers = Group.objects.get(name=publish)
164+
owner_user.groups.add(publishers)
165+
else:
166+
Group.objects.create(name=publish)
167+
publishers = Group.objects.get(name=publish)
168+
owner_user.groups.add(publishers)
169+
GroupInfo.objects.create(
170+
delete_members_on_group_deletion=False,
171+
description=prfx["description"],
172+
group=publishers,
173+
max_n_members=-1,
174+
owner_user=owner_user,
175+
)
176+
if is_public is True:
177+
owner_group = "bco_drafter"
178+
else:
179+
owner_group = publish
180+
181+
write_result = DbUtils.DbUtils().write_object(
182+
p_app_label="api",
183+
p_model_name="Prefix",
184+
p_fields=[
185+
"created_by",
186+
"description",
187+
"owner_group",
188+
"owner_user",
189+
"prefix",
190+
],
191+
p_data={
192+
"created_by": user_utils.user_from_request(
193+
request=request
194+
).username,
195+
"description": prfx["description"],
196+
"owner_group": owner_group,
197+
"owner_user": creation_object["owner_user"],
198+
"prefix": standardized,
199+
},
200+
)
201+
if write_result != 1:
215202
return_data.append(
216203
db_utils.messages(parameters={"prefix": standardized})[
217-
"201_prefix_create"
204+
"409_prefix_conflict"
218205
]
219206
)
220-
# Created the prefix.
221-
# errors["201_prefix_create"] = db_utils.messages(
222-
# parameters={"prefix": standardized}
223-
# )["201_prefix_create"]
207+
any_failed = True
208+
continue
224209

225-
# Append the possible "errors".
226-
if any_failed and len(return_data) == 1:
210+
return_data.append(
211+
db_utils.messages(parameters={"prefix": standardized})[
212+
"201_prefix_create"
213+
]
214+
)
215+
216+
if any_failed and len(return_data) == 1:
227217
return Response(status=return_data[0]["status_code"], data=return_data)
228218

229219
if any_failed and len(return_data) > 1:

api/views.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,13 +1221,8 @@ class ApiPrefixesCreate(APIView):
12211221
```
12221222
"""
12231223

1224-
# Permissions - prefix admins only
1225-
permission_classes = [RequestorInPrefixAdminsGroup]
1224+
permission_classes = [RequestorInPrefixAdminsGroup, IsAuthenticated, ]
12261225

1227-
# TYPE_ARRAY explanation
1228-
# Source: https://stackoverflow.com/questions/53492889/drf-yasg-doesnt-take-type-array-as-a-valid-type
1229-
1230-
# TODO: Need to get the schema that is being sent here from FE
12311226
request_body = openapi.Schema(
12321227
type=openapi.TYPE_OBJECT,
12331228
title="Prefix Creation Schema",
@@ -1261,13 +1256,17 @@ class ApiPrefixesCreate(APIView):
12611256
@swagger_auto_schema(
12621257
request_body=request_body,
12631258
responses={
1264-
201: "The prefix was successfully created.",
1265-
400: "Bad request for one of two reasons: \n1) the prefix does not"
1266-
"follow the naming standard, or \n2) owner_user and/or"
1267-
"owner_group do not exist.",
1268-
401: "Unauthorized. Authentication credentials were not provided.",
1269-
403: "Forbidden. User doesnot have permission to perform this action",
1270-
409: "The prefix the requestor is attempting to create already exists.",
1259+
200: "All prefixes were successfully created.",
1260+
207: "Some or all prefix creations failed. Each object submitted"
1261+
" will have it's own response object with it's own status"
1262+
" code and message:\n"
1263+
"201: The prefix * was successfully created.\n"
1264+
"400: Bad Request. The expiration date * is not valid.\n"
1265+
"400: Bad Request. The prefix * does not follow the naming rules for a prefix.\n"
1266+
"403: Forbidden. User does not have permission to perform this action.\n"
1267+
"404: Not Found. The user * was not found on the server.\n"
1268+
"409: Conflict. The prefix the requestor is attempting to create already exists.\n",
1269+
401: "Unauthorized. Authentication credentials were not provided."
12711270
},
12721271
tags=["Prefix Management"],
12731272
)
@@ -1474,7 +1473,6 @@ class ApiPrefixesPermissionsSet(APIView):
14741473
]
14751474
}
14761475
```
1477-
14781476
"""
14791477

14801478
# Permissions - prefix admins only

0 commit comments

Comments
 (0)