@@ -41,7 +41,12 @@ def post_api_objects_drafts_modify(request):
4141
4242 db_utils = DbUtils .DbUtils ()
4343 user = UserUtils .UserUtils ().user_from_request (request = request )
44- bulk_request = request .data ["POST_api_objects_drafts_modify" ]
44+ try :
45+ bulk_request = request .data ["POST_api_objects_drafts_modify" ]
46+ except KeyError as error :
47+ return Response (status = status .HTTP_400_BAD_REQUEST , data = {
48+ 'KeyError' : f'{ str (error )} '
49+ })
4550 px_perms = UserUtils .UserUtils ().prefix_perms_for_user (
4651 flatten = True , user_object = user , specific_permission = ["add" ]
4752 )
@@ -50,101 +55,111 @@ def post_api_objects_drafts_modify(request):
5055 returning = []
5156 any_failed = False
5257 for draft_object in bulk_request :
53- # Get the prefix for this draft.
54- prefix = draft_object ["object_id" ].split ("/" )[- 2 ].split ("_" )[0 ].upper ()
58+ try :
59+ # Get the prefix for this draft.
60+ prefix = draft_object ["object_id" ].split ("/" )[- 2 ].split ("_" )[0 ].upper ()
5561
56- # Does the requestor have change permissions for
57- # the *prefix*?
62+ # Does the requestor have change permissions for
63+ # the *prefix*?
5864
59- # TODO: add permission setting view...
60- # if 'change_' + prefix in px_perms:
61- if "add_" + prefix in px_perms :
65+ # TODO: add permission setting view...
66+ # if 'change_' + prefix in px_perms:
6267
63- # The requestor has change permissions for
64- # the prefix, but do they have object-level
65- # change permissions?
68+ if "add_" + prefix in px_perms :
6669
67- # This can be checked by seeing if the requestor
68- # is the object owner OR they are a user with
69- # object-level change permissions OR if they are in a
70- # group that has object-level change permissions.
71- # To check these options, we need the actual object.
70+ # The requestor has change permissions for
71+ # the prefix, but do they have object-level
72+ # change permissions?
7273
73- if draft_object ["object_id" ] not in draft_object ["contents" ]["object_id" ]:
74- returning .append (
75- db_utils .messages (
76- parameters = {
77- "object_id" : draft_object ["contents" ]["object_id" ],
78- "draft_object_id" : draft_object ["object_id" ],
79- }
80- )["409_draft_object_id_conflict" ]
81- )
82- any_failed = True
83- continue
74+ # This can be checked by seeing if the requestor
75+ # is the object owner OR they are a user with
76+ # object-level change permissions OR if they are in a
77+ # group that has object-level change permissions.
78+ # To check these options, we need the actual object.
79+ if draft_object ["object_id" ] not in draft_object ["contents" ]["object_id" ]:
80+ returning .append (
81+ db_utils .messages (
82+ parameters = {
83+ "object_id" : draft_object ["contents" ]["object_id" ],
84+ "draft_object_id" : draft_object ["object_id" ],
85+ }
86+ )["409_draft_object_id_conflict" ]
87+ )
88+ any_failed = True
89+ continue
8490
85- if BCO .objects .filter (
86- object_id = draft_object ["contents" ]["object_id" ]
87- ).exists ():
88- objected = BCO .objects .get (
91+ if BCO .objects .filter (
8992 object_id = draft_object ["contents" ]["object_id" ]
90- )
93+ ).exists ():
94+ objected = BCO .objects .get (
95+ object_id = draft_object ["contents" ]["object_id" ]
96+ )
9197
92- # We don't care where the view permission comes from,
93- # be it a User permission or a Group permission.
94- all_permissions = get_perms (user , objected )
95- # TODO: add permission setting view...
96- if (
97- user .username == objected .owner_user .username
98- or "add_" + prefix in px_perms
99- ):
100-
101- # # User does *NOT* have to be in the owner group!
102- # # to assign the object's group owner.
103- # if Group.objects.filter(
104- # name = draft_object['owner_group'].lower()
105- # ).exists():
106- #
107- # Update the object.
108- # *** COMPLETELY OVERWRITES CONTENTS!!! ***
109- objected .contents = draft_object ["contents" ]
110-
111- if "state" in draft_object :
112- if draft_object ["state" ] == "DELETE" :
113- objected .state = "DELETE"
114-
115- # Set the update time.
116- objected .last_update = timezone .now ()
117-
118- # Save it.
119- objected .save ()
120-
121- # Update the request status.
98+ # We don't care where the view permission comes from,
99+ # be it a User permission or a Group permission.
100+ all_permissions = get_perms (user , objected )
101+ # TODO: add permission setting view...
102+ if (
103+ user .username == objected .owner_user .username
104+ or "add_" + prefix in px_perms
105+ ):
106+
107+ # # User does *NOT* have to be in the owner group!
108+ # # to assign the object's group owner.
109+ # if Group.objects.filter(
110+ # name = draft_object['owner_group'].lower()
111+ # ).exists():
112+ #
113+ # Update the object.
114+ # *** COMPLETELY OVERWRITES CONTENTS!!! ***
115+ objected .contents = draft_object ["contents" ]
116+
117+ if "state" in draft_object :
118+ if draft_object ["state" ] == "DELETE" :
119+ objected .state = "DELETE"
120+
121+ # Set the update time.
122+ objected .last_update = timezone .now ()
123+
124+ # Save it.
125+ objected .save ()
126+
127+ # Update the request status.
128+ returning .append (
129+ db_utils .messages (
130+ parameters = {"object_id" : draft_object ["object_id" ]}
131+ )["200_update" ]
132+ )
133+ else :
134+ # Insufficient permissions.
135+ returning .append (
136+ db_utils .messages (parameters = {
137+ })["403_insufficient_permissions" ]
138+ )
139+ any_failed = True
140+
141+ else :
122142 returning .append (
123143 db_utils .messages (
124144 parameters = {"object_id" : draft_object ["object_id" ]}
125- )["200_update" ]
126- )
127- else :
128- # Insufficient permissions.
129- returning .append (
130- db_utils .messages (parameters = {})["403_insufficient_permissions" ]
145+ )["404_object_id" ]
131146 )
132147 any_failed = True
133-
134148 else :
135149 returning .append (
136- db_utils .messages (
137- parameters = { "object_id" : draft_object [ "object_id" ]}
138- )[ "404_object_id" ]
150+ db_utils .messages (parameters = { "prefix" : prefix })[
151+ "401_prefix_unauthorized"
152+ ]
139153 )
140154 any_failed = True
141- else :
155+ except :
142156 returning .append (
143- db_utils .messages (parameters = {"prefix" : prefix })[
144- "401_prefix_unauthorized "
157+ db_utils .messages (parameters = {})[
158+ "400_bad_request "
145159 ]
146160 )
147161 any_failed = True
162+
148163 if any_failed and len (returning ) == 1 :
149164 if returning [0 ]["status_code" ] == "403" :
150165 return Response (status = status .HTTP_403_FORBIDDEN , data = returning )
0 commit comments