Skip to content

Commit b3f8419

Browse files
committed
Merge remote-tracking branch 'origin/22.04' into testing
Changes to be committed: modified: bco_api/api/scripts/method_specific/POST_api_objects_drafts_create.py modified: bco_api/api/scripts/method_specific/POST_api_objects_drafts_modify.py modified: bco_api/api/scripts/utilities/UserUtils.py new file: bco_api/api/tests.py modified: bco_api/api/views.py
2 parents a7c7f56 + 127a6f2 commit b3f8419

File tree

5 files changed

+133
-41
lines changed

5 files changed

+133
-41
lines changed

bco_api/api/scripts/method_specific/POST_api_objects_drafts_create.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Creates a new BCO draft object.
77
"""
88

9+
from email import message
910
from api.scripts.utilities import DbUtils, UserUtils
1011
from api.models import prefix_table
1112
from django.conf import settings
@@ -14,7 +15,7 @@
1415
from rest_framework import status
1516
from rest_framework.response import Response
1617

17-
def POST_api_objects_drafts_create(request):
18+
def post_api_objects_drafts_create(request):
1819
"""Create BCO Draft
1920
2021
Parameters
@@ -49,6 +50,88 @@ def POST_api_objects_drafts_create(request):
4950

5051
# Since bulk_request is an array, go over each
5152
# item in the array.
53+
54+
if len(bulk_request) == 1:
55+
creation_object = bulk_request[0]
56+
standardized = creation_object['prefix'].upper()
57+
if 'add_' + standardized in prefix_perms and \
58+
'draft_' + standardized in prefix_perms:
59+
if Group.objects.filter(
60+
name = creation_object['owner_group'].lower()).exists():
61+
constructed_name = object_naming_info['uri_regex'].replace(
62+
'root_uri',
63+
object_naming_info['root_uri']
64+
)
65+
constructed_name = constructed_name.replace('prefix', standardized)
66+
prefix_location = constructed_name.index(standardized)
67+
prefix_length = len(standardized)
68+
constructed_name = constructed_name[0:prefix_location+prefix_length]
69+
prefix_counter = prefix_table.objects.get(prefix=standardized)
70+
creation_object['object_id'] = constructed_name + '_' + \
71+
'{:06d}'.format(prefix_counter.n_objects) + '/DRAFT'
72+
73+
creation_object['contents']['object_id'] = creation_object['object_id']
74+
bco_id = creation_object['object_id']
75+
owner_group = Group.objects.get(name = creation_object['owner_group'])
76+
creation_object['owner_group'] = owner_group.name
77+
creation_object['owner_user'] = user.username
78+
creation_object['prefix'] = standardized
79+
creation_object['state'] = 'DRAFT'
80+
creation_object['last_update'] = timezone.now()
81+
objects_written = db_utils.write_object(
82+
p_app_label = 'api',
83+
p_model_name = 'bco',
84+
p_fields = [
85+
'contents',
86+
'last_update',
87+
'object_id',
88+
'owner_group',
89+
'owner_user',
90+
'prefix',
91+
'schema',
92+
'state'],
93+
p_data = creation_object
94+
)
95+
prefix_counter.n_objects = prefix_counter.n_objects + 1
96+
prefix_counter.save()
97+
98+
if objects_written < 1:
99+
# Issue with writing out to DB
100+
return Response(
101+
status=status.HTTP_400_BAD_REQUEST,
102+
data='The request could not be processed with the'\
103+
' parameters provided.'
104+
)
105+
106+
return Response(
107+
status=status.HTTP_201_CREATED,
108+
data= {
109+
'request_status': 'SUCCESS',
110+
'status_code' : '201',
111+
'message' : f'The object with ID {bco_id} was'\
112+
' created on the server.',
113+
'object_id' : bco_id
114+
},
115+
)
116+
117+
else:
118+
# Update the request status.
119+
returning.append(db_utils.messages(parameters = {})
120+
['400_bad_request'])
121+
any_failed = True
122+
123+
else:
124+
# Update the request status.
125+
return Response(
126+
status=status.HTTP_401_UNAUTHORIZED,
127+
data= {
128+
'request_status': 'FAILURE',
129+
'status_code' : '401',
130+
'message' : 'The token provided does not have draft'\
131+
f'permissions for prefix {standardized}.'
132+
}
133+
)
134+
52135
for creation_object in bulk_request:
53136
# Standardize the prefix.
54137
standardized = creation_object['prefix'].upper()

bco_api/api/scripts/method_specific/POST_api_objects_drafts_modify.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ def POST_api_objects_drafts_modify(request):
8989
# ).exists():
9090
#
9191
# Update the object.
92-
9392
# *** COMPLETELY OVERWRITES CONTENTS!!! ***
9493
objected.contents = draft_object['contents']
9594

95+
if draft_object['state'] == 'DELETE':
96+
objected.state = 'DELETE'
97+
9698
# Set the update time.
9799
objected.last_update = timezone.now()
98100

bco_api/api/scripts/utilities/UserUtils.py

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@ class UserUtils:
3030
3131
"""
3232
def check_permission_exists(self, perm):
33-
# Does the user exist?
33+
"""Does the user exist?"""
3434
return Permission.objects.get(codename='test')
3535

3636
def check_group_exists(self, n):
37-
# Does the user exist?
37+
"""Does the user exist?"""
3838
return Group.objects.filter(name=n).exists()
3939

4040
def check_user_exists(self, un):
41-
# Does the user exist?
41+
"""Does the user exist?"""
4242
return User.objects.filter(username=un).exists()
4343

4444
def check_user_in_group(self, un, gn):
45+
"""Check if a user is in a group.
4546
46-
# Check if a user is in a group.
47+
First check that the user exists.
48+
Then check that the groups exists.
49+
Finally, check that the user is in
50+
the group.
4751
48-
# First check that the user exists.
49-
# Then check that the groups exists.
50-
# Finally, check that the user is in
51-
# the group.
52-
53-
# Try/except is preferred because
54-
# the query is only run one time.
52+
Try/except is preferred because
53+
the query is only run one time.
54+
"""
5555

5656
try:
5757

@@ -86,39 +86,23 @@ def check_user_in_group(self, un, gn):
8686
# Bad user.
8787
return False
8888

89-
def check_user_owns_prefix(
90-
self,
91-
un,
92-
prfx
93-
):
89+
def check_user_owns_prefix(self, un, prfx):
90+
"""Check if a user owns a prefix."""
9491

95-
# Check if a user owns a prefix.
9692
return Prefix.objects.filter(owner_user=un, prefix=prfx).exists()
9793

98-
def get_user_groups_by_token(
99-
self,
100-
token
101-
):
102-
103-
# Takes token to give groups.
104-
105-
# First, get the groups for this token.
106-
107-
# This means getting the user ID for the token,
108-
# then the username.
109-
user_id = Token.objects.get(
110-
key=token
111-
).user_id
94+
def get_user_groups_by_token(self, token):
95+
"""Takes token to give groups.
96+
First, get the groups for this token.
97+
This means getting the user ID for the token,
98+
then the username."""
11299

113-
username = User.objects.get(
114-
id=user_id
115-
)
100+
user_id = Token.objects.get(key=token).user_id
101+
username = User.objects.get(id=user_id)
116102

117103
# Get the groups for this username (at a minimum the user
118104
# group created when the account was created should show up).
119-
return Group.objects.filter(
120-
user=username
121-
)
105+
return Group.objects.filter(user=username)
122106

123107
def get_user_groups_by_username(self, un):
124108
"""Takes usernames to give groups.

bco_api/api/tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
"""Tests
3+
4+
1st round of testing
5+
"""
6+
7+
from django.test import TestCase
8+
from django.apps import AppConfig
9+
from django.db.models.signals import post_migrate
10+
from api.signals import populate_models
11+
12+
class ApiConfig(TestCase):
13+
"""API Configuration
14+
"""
15+
16+
default_auto_field = 'django.db.models.AutoField'
17+
name = 'api'
18+
19+
def ready(self):
20+
"""Create the anonymous user if they don't exist."""
21+
post_migrate.connect(populate_models, sender=self)
22+
print('test')
23+
self.assertIs()

bco_api/api/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
from api.scripts.method_specific.POST_api_accounts_describe import POST_api_accounts_describe
3737
from api.scripts.method_specific.POST_api_accounts_new import POST_api_accounts_new
38-
from api.scripts.method_specific.POST_api_objects_drafts_create import POST_api_objects_drafts_create
38+
from api.scripts.method_specific.POST_api_objects_drafts_create import post_api_objects_drafts_create
3939
from api.scripts.method_specific.POST_api_objects_drafts_modify import POST_api_objects_drafts_modify
4040
from api.scripts.method_specific.POST_api_objects_drafts_permissions import POST_api_objects_drafts_permissions
4141
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):
494494
403: "Invalid token."
495495
}, tags=["BCO Management"])
496496
def post(self, request) -> Response:
497-
return check_post_and_process(request, POST_api_objects_drafts_create)
497+
return check_post_and_process(request, post_api_objects_drafts_create)
498498

499499

500500
class ApiObjectsDraftsModify(APIView):

0 commit comments

Comments
 (0)