Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop tests and document '/api/objects/drafts/modify/' #256

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 90 additions & 75 deletions api/scripts/method_specific/POST_api_objects_drafts_modify.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ def post_api_objects_drafts_modify(request):

db_utils = DbUtils.DbUtils()
user = UserUtils.UserUtils().user_from_request(request=request)
bulk_request = request.data["POST_api_objects_drafts_modify"]
try:
bulk_request = request.data["POST_api_objects_drafts_modify"]
except KeyError as error:
return Response(status=status.HTTP_400_BAD_REQUEST, data={
'KeyError': f'{str(error)}'
})
px_perms = UserUtils.UserUtils().prefix_perms_for_user(
flatten=True, user_object=user, specific_permission=["add"]
)
Expand All @@ -50,101 +55,111 @@ def post_api_objects_drafts_modify(request):
returning = []
any_failed = False
for draft_object in bulk_request:
# Get the prefix for this draft.
prefix = draft_object["object_id"].split("/")[-2].split("_")[0].upper()
try:
# Get the prefix for this draft.
prefix = draft_object["object_id"].split("/")[-2].split("_")[0].upper()

# Does the requestor have change permissions for
# the *prefix*?
# Does the requestor have change permissions for
# the *prefix*?

# TODO: add permission setting view...
# if 'change_' + prefix in px_perms:
if "add_" + prefix in px_perms:
# TODO: add permission setting view...
# if 'change_' + prefix in px_perms:

# The requestor has change permissions for
# the prefix, but do they have object-level
# change permissions?
if "add_" + prefix in px_perms:

# This can be checked by seeing if the requestor
# is the object owner OR they are a user with
# object-level change permissions OR if they are in a
# group that has object-level change permissions.
# To check these options, we need the actual object.
# The requestor has change permissions for
# the prefix, but do they have object-level
# change permissions?

if draft_object["object_id"] not in draft_object["contents"]["object_id"]:
returning.append(
db_utils.messages(
parameters={
"object_id": draft_object["contents"]["object_id"],
"draft_object_id": draft_object["object_id"],
}
)["409_draft_object_id_conflict"]
)
any_failed = True
continue
# This can be checked by seeing if the requestor
# is the object owner OR they are a user with
# object-level change permissions OR if they are in a
# group that has object-level change permissions.
# To check these options, we need the actual object.
if draft_object["object_id"] not in draft_object["contents"]["object_id"]:
returning.append(
db_utils.messages(
parameters={
"object_id": draft_object["contents"]["object_id"],
"draft_object_id": draft_object["object_id"],
}
)["409_draft_object_id_conflict"]
)
any_failed = True
continue

if BCO.objects.filter(
object_id=draft_object["contents"]["object_id"]
).exists():
objected = BCO.objects.get(
if BCO.objects.filter(
object_id=draft_object["contents"]["object_id"]
)
).exists():
objected = BCO.objects.get(
object_id=draft_object["contents"]["object_id"]
)

# We don't care where the view permission comes from,
# be it a User permission or a Group permission.
all_permissions = get_perms(user, objected)
# TODO: add permission setting view...
if (
user.username == objected.owner_user.username
or "add_" + prefix in px_perms
):

# # User does *NOT* have to be in the owner group!
# # to assign the object's group owner.
# if Group.objects.filter(
# name = draft_object['owner_group'].lower()
# ).exists():
#
# Update the object.
# *** COMPLETELY OVERWRITES CONTENTS!!! ***
objected.contents = draft_object["contents"]

if "state" in draft_object:
if draft_object["state"] == "DELETE":
objected.state = "DELETE"

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

# Save it.
objected.save()

# Update the request status.
# We don't care where the view permission comes from,
# be it a User permission or a Group permission.
all_permissions = get_perms(user, objected)
# TODO: add permission setting view...
if (
user.username == objected.owner_user.username
or "add_" + prefix in px_perms
):

# # User does *NOT* have to be in the owner group!
# # to assign the object's group owner.
# if Group.objects.filter(
# name = draft_object['owner_group'].lower()
# ).exists():
#
# Update the object.
# *** COMPLETELY OVERWRITES CONTENTS!!! ***
objected.contents = draft_object["contents"]

if "state" in draft_object:
if draft_object["state"] == "DELETE":
objected.state = "DELETE"

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

# Save it.
objected.save()

# Update the request status.
returning.append(
db_utils.messages(
parameters={"object_id": draft_object["object_id"]}
)["200_update"]
)
else:
# Insufficient permissions.
returning.append(
db_utils.messages(parameters={
})["403_insufficient_permissions"]
)
any_failed = True

else:
returning.append(
db_utils.messages(
parameters={"object_id": draft_object["object_id"]}
)["200_update"]
)
else:
# Insufficient permissions.
returning.append(
db_utils.messages(parameters={})["403_insufficient_permissions"]
)["404_object_id"]
)
any_failed = True

else:
returning.append(
db_utils.messages(
parameters={"object_id": draft_object["object_id"]}
)["404_object_id"]
db_utils.messages(parameters={"prefix": prefix})[
"401_prefix_unauthorized"
]
)
any_failed = True
else:
except:
returning.append(
db_utils.messages(parameters={"prefix": prefix})[
"401_prefix_unauthorized"
db_utils.messages(parameters={})[
"400_bad_request"
]
)
any_failed = True

if any_failed and len(returning) == 1:
if returning[0]["status_code"] == "403":
return Response(status=status.HTTP_403_FORBIDDEN, data=returning)
Expand Down
Loading