Skip to content

Commit

Permalink
Merge pull request #36 from biocompute-objects/update_documentation
Browse files Browse the repository at this point in the history
additional error handing and documentation for various requests
  • Loading branch information
syntheticgio authored Nov 12, 2021
2 parents 8b33200 + 1ed7c29 commit d591104
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 71 deletions.
30 changes: 13 additions & 17 deletions bco_api/api/scripts/method_specific/POST_api_accounts_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def POST_api_accounts_new(request):
if db.check_user_exists( p_app_label='api', p_model_name='new_users', p_email=bulk_request['email']) is None:
if User.objects.filter(email=bulk_request['email']).exists():
# Account has already been activated.
return Response(status=status.HTTP_403_FORBIDDEN)
return Response(status=status.HTTP_409_CONFLICT, data={"message": "Account has already been activated."})

# The email has not already been asked for and
# it has not been activated.
Expand All @@ -57,7 +57,6 @@ def POST_api_accounts_new(request):
temp_identifier = uuid.uuid4().hex

if 'token' in bulk_request and 'hostname' in bulk_request:

p_data = {
'email': bulk_request['email'],
'temp_identifier': temp_identifier,
Expand All @@ -66,19 +65,22 @@ def POST_api_accounts_new(request):
}

else:

p_data = {
'email': bulk_request['email'],
'temp_identifier': temp_identifier
}

db.write_object(
objects_written = db.write_object(
p_app_label='api',
p_model_name='new_users',
p_fields=['email', 'temp_identifier', 'hostname', 'token'],
p_data=p_data
)

if objects_written < 1:
# There is a problem with the write.
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR, data="Not able to save the new account.")

# Send an e-mail to let the requestor know that they
# need to follow the activation link within 10 minutes.

Expand Down Expand Up @@ -108,16 +110,12 @@ def POST_api_accounts_new(request):
)

except Exception as e:
pass
# TODO: Should handle when the send_mail function fails?
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR, data={"message": "Not able to send authentication email."})

return (
Response(
status=status.HTTP_201_CREATED
)
)
return Response(status=status.HTTP_201_CREATED)

elif settings.PRODUCTION == 'False':

# Go straight to account activation.
straight_activated = GET_activate_account(
username=bulk_request['email'],
Expand All @@ -133,7 +131,8 @@ def POST_api_accounts_new(request):

return Response(
data={
'message': 'New account succesfully created on development server ' + settings.PUBLIC_HOSTNAME + '. Parse the \'token\' key for your new token.',
'message': 'New account successfully created on development server ' + settings.PUBLIC_HOSTNAME + '. Parse the \'token\' key for your '
'new token.',
'token': user_token,
'username': straight_activated.data['data']['username']
},
Expand All @@ -143,8 +142,5 @@ def POST_api_accounts_new(request):
else:

# Account has already been asked for.
return (
Response(
status=status.HTTP_403_FORBIDDEN
)
)
return Response(status=status.HTTP_409_CONFLICT, data={"message": "Account has already been requested."})

28 changes: 23 additions & 5 deletions bco_api/api/scripts/method_specific/POST_api_groups_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def POST_api_groups_delete(request):
# Construct an array to return information about processing
# the request.
returning = []
any_failed = False

# Since bulk_request is an array, go over each
# item in the array.
Expand All @@ -60,18 +61,35 @@ def POST_api_groups_delete(request):
# Delete all members of the group.
User.objects.filter(groups__name=grouped.name).delete()
# Delete the group itself.
grouped.delete()
deleted_count, deleted_info = grouped.delete()
if deleted_count < 1:
# Too few deleted, error with this delete
returning.append(db.messages(parameters={
'group': grouped.name })['404_missing_bulk_parameters'])
any_failed = True
continue
elif deleted_count > 1:
# We don't expect there to be duplicates, so while this was successful it should throw a warning
returning.append(db.messages(parameters={
'group': grouped.name })['418_too_many_deleted'])
any_failed = True
continue
# Everything looks OK
returning.append(db.messages(parameters={'group': grouped.name})['200_OK_group_delete'])
else:
# Requestor is not the admin.
returning.append(db.messages(parameters={})['403_invalid_token'])
returning.append(db.messages(parameters={})['403_insufficient_permissions'])
any_failed = True
else:
# Update the request status.
returning.append(db.messages(parameters={})['400_bad_request'])
any_failed = True

# As this view is for a bulk operation, status 200
# means that the request was successfully processed,
# but NOT necessarily each item in the request.
return (
Response(status=status.HTTP_200_OK, data=returning)
)
if any_failed:
return Response(status=status.HTTP_300_MULTIPLE_CHOICES, data=returning)

return Response(status=status.HTTP_200_OK, data=returning)

Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ def POST_api_groups_modify(request):
returning.append(db.messages(parameters={'group': grouped.name})['200_OK_group_modify'])
else:
# Requestor is not the admin.
# TODO: This is invalid permissions not exactly invalid token; might want to change
returning.append(db.messages(parameters={})['403_invalid_token'])
returning.append(db.messages(parameters={})['403_insufficient_permissions'])
else:
# Update the request status.
returning.append(db.messages(parameters={})['400_bad_request'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def POST_api_objects_drafts_create(incoming):

# Construct an array to return the objects.
returning = []
any_failed = False

# Since bulk_request is an array, go over each
# item in the array.
Expand Down Expand Up @@ -131,13 +132,18 @@ def POST_api_objects_drafts_create(incoming):
creation_object['last_update'] = timezone.now()

# Write to the database.
db.write_object(
objects_written = db.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
)

if objects_written < 1:
# Issue with writing out to DB
returning.append(db.messages(parameters={ })['400_bad_request'])
any_failed = True

# Object creator automatically has full permissions
# on the object. This is checked by checking whether
# or not the requestor matches the owner_user primary
Expand All @@ -150,38 +156,24 @@ def POST_api_objects_drafts_create(incoming):
# receiver in models.py

# Update the request status.
returning.append(
db.messages(
parameters = creation_object
)['201_create']
)
returning.append(db.messages(parameters = creation_object)['201_create'])

else:

# Update the request status.
returning.append(
db.messages(
parameters = {}
)['400_bad_request']
)
returning.append(db.messages(parameters = {})['400_bad_request'])
any_failed = True

else:

# Update the request status.
returning.append(
db.messages(
parameters = {
'prefix': creation_object['prefix']
}
)['401_prefix_unauthorized']
)
returning.append(db.messages(parameters = {'prefix': creation_object['prefix']})['401_prefix_unauthorized'])
any_failed = True

# As this view is for a bulk operation, status 200
# means that the request was successfully processed,
# but NOT necessarily each item in the request.
# For example, a table may not have been found for the first
# requested draft.
return Response(
status = status.HTTP_200_OK,
data = returning
)
if any_failed:
return Response(status=status.HTTP_300_MULTIPLE_CHOICES, data=returning)

return Response(status = status.HTTP_200_OK, data = returning)
26 changes: 18 additions & 8 deletions bco_api/api/scripts/utilities/DbUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,11 @@ def messages(
'status_code': '403',
'message': 'The token provided was not able to be used on this object.'
},
'404_missing_bulk_parameters': {
'request_status': 'FAILURE',
'status_code' : '404',
'message' : 'One or more missing optional parameters are required for this call to have an effect.'
},
'404_missing_prefix': {
'request_status': 'FAILURE',
'status_code': '404',
Expand All @@ -661,6 +666,11 @@ def messages(
'status_code': '409',
'message': 'The provided prefix \'' + parameters['prefix'] + '\' has already been created on this server.'
},
'418_too_many_deleted': {
'request_status': 'FAILURE',
'status_code' : '418',
'message' : 'Only one object was expected to be deleted, but multiple were removed.'
},
}


Expand Down Expand Up @@ -811,7 +821,7 @@ def publish(
p_data = publishable
)

# Successfuly saved the object.
# Successfully saved the object.
return {
'published_id': published['object_id']
}
Expand Down Expand Up @@ -841,21 +851,18 @@ def write_object(
incoming_fields = p_fields
)

serialized = serializer(
data = p_data
)
serialized = serializer(data = p_data)

# Save (update) it.
if p_update is False:

# Write a new object.
if serialized.is_valid():
serialized.save()
return 1
else:
print(serialized.errors)

return -1
else:

# Update an existing object.
# apps.get_model(
# app_label = p_app_label,
Expand All @@ -866,13 +873,16 @@ def write_object(
# contents = p_data['contents']
# )

apps.get_model(
objects_modified = apps.get_model(
app_label = p_app_label,
model_name = p_model_name
).objects.filter(
object_id = p_data['object_id']
).update(
contents = p_data['contents']
)

return objects_modified

def convert_id_form(oi_root):
return oi_root.split("_")[0] + '{:06d}'.format(int(oi_root.split("_")[1]))
Loading

0 comments on commit d591104

Please sign in to comment.