Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/22.04' into testing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HadleyKing committed Mar 29, 2022
2 parents a7c7f56 + 127a6f2 commit b3f8419
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ def POST_api_objects_drafts_modify(request):
# ).exists():
#
# Update the object.

# *** COMPLETELY OVERWRITES CONTENTS!!! ***
objected.contents = draft_object['contents']

if draft_object['state'] == 'DELETE':
objected.state = 'DELETE'

# Set the update time.
objected.last_update = timezone.now()

Expand Down
58 changes: 21 additions & 37 deletions bco_api/api/scripts/utilities/UserUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ class UserUtils:
"""
def check_permission_exists(self, perm):
# Does the user exist?
"""Does the user exist?"""
return Permission.objects.get(codename='test')

def check_group_exists(self, n):
# Does the user exist?
"""Does the user exist?"""
return Group.objects.filter(name=n).exists()

def check_user_exists(self, un):
# Does the user exist?
"""Does the user exist?"""
return User.objects.filter(username=un).exists()

def check_user_in_group(self, un, gn):
"""Check if a user is in a group.
# Check if a user is in a group.
First check that the user exists.
Then check that the groups exists.
Finally, check that the user is in
the group.
# First check that the user exists.
# Then check that the groups exists.
# Finally, check that the user is in
# the group.

# Try/except is preferred because
# the query is only run one time.
Try/except is preferred because
the query is only run one time.
"""

try:

Expand Down Expand Up @@ -86,39 +86,23 @@ def check_user_in_group(self, un, gn):
# Bad user.
return False

def check_user_owns_prefix(
self,
un,
prfx
):
def check_user_owns_prefix(self, un, prfx):
"""Check if a user owns a prefix."""

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

def get_user_groups_by_token(
self,
token
):

# Takes token to give groups.

# First, get the groups for this token.

# This means getting the user ID for the token,
# then the username.
user_id = Token.objects.get(
key=token
).user_id
def get_user_groups_by_token(self, token):
"""Takes token to give groups.
First, get the groups for this token.
This means getting the user ID for the token,
then the username."""

username = User.objects.get(
id=user_id
)
user_id = Token.objects.get(key=token).user_id
username = User.objects.get(id=user_id)

# Get the groups for this username (at a minimum the user
# group created when the account was created should show up).
return Group.objects.filter(
user=username
)
return Group.objects.filter(user=username)

def get_user_groups_by_username(self, un):
"""Takes usernames to give groups.
Expand Down
23 changes: 23 additions & 0 deletions bco_api/api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3
"""Tests
1st round of testing
"""

from django.test import TestCase
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from api.signals import populate_models

class ApiConfig(TestCase):
"""API Configuration
"""

default_auto_field = 'django.db.models.AutoField'
name = 'api'

def ready(self):
"""Create the anonymous user if they don't exist."""
post_migrate.connect(populate_models, sender=self)
print('test')
self.assertIs()
4 changes: 2 additions & 2 deletions bco_api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b3f8419

Please sign in to comment.