Clean up and refactor a bit

main
Thu Trang Pham 2020-11-08 07:47:39 -08:00
parent b845e57285
commit c586100098
2 changed files with 11 additions and 12 deletions

View File

@ -68,7 +68,6 @@ class AdminConfirmMixin:
TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR) TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR)
) )
if to_field and not self.to_field_allowed(request, to_field): if to_field and not self.to_field_allowed(request, to_field):
print("OKAY WHAT")
raise DisallowedModelAdminToField( raise DisallowedModelAdminToField(
"The field %s cannot be referenced." % to_field "The field %s cannot be referenced." % to_field
) )
@ -83,7 +82,6 @@ class AdminConfirmMixin:
obj = None obj = None
else: else:
self.message_user(request, add)
obj = self.get_object(request, unquote(object_id), to_field) obj = self.get_object(request, unquote(object_id), to_field)
if obj is None: if obj is None:
@ -104,12 +102,16 @@ class AdminConfirmMixin:
else: else:
new_object = form.instance new_object = form.instance
# End code from super()._changeform_view
changed_data = {} changed_data = {}
if add: if add:
for name in form.changed_data: for name in form.changed_data:
changed_data[name] = [None, new_object.__getattribute__(name)] new_value = new_object.__getattribute__(name)
if new_value is not None:
changed_data[name] = [None, new_value]
else: else:
# Parse the changed data - Note that using form.changed_data would not work as initial is not set # Parse the changed data - Note that using form.changed_data would not work because initial is not set
for name, field in form.fields.items(): for name, field in form.fields.items():
initial_value = obj.__getattribute__(name) initial_value = obj.__getattribute__(name)
new_value = new_object.__getattribute__(name) new_value = new_object.__getattribute__(name)
@ -122,16 +124,14 @@ class AdminConfirmMixin:
# No confirmation required for changed fields, continue to save # No confirmation required for changed fields, continue to save
return super()._changeform_view(request, object_id, form_url, extra_context) return super()._changeform_view(request, object_id, form_url, extra_context)
# Parse the original save action from request
save_action = None
for action in ["_save", "_saveasnew", "_addanother", "_continue"]:
if action in request.POST:
save_action = action
break
# Parse raw form data from POST # Parse raw form data from POST
form_data = {} form_data = {}
# Parse the original save action from request
save_action = None
for key in request.POST: for key in request.POST:
if key in ["_save", "_saveasnew", "_addanother", "_continue"]:
save_action = key
if key.startswith("_") or key == 'csrfmiddlewaretoken': if key.startswith("_") or key == 'csrfmiddlewaretoken':
continue continue
form_data[key] = request.POST.get(key) form_data[key] = request.POST.get(key)

View File

@ -105,7 +105,6 @@ class TestAdminConfirmMixin(TestCase):
response = self.client.post( response = self.client.post(
f'/admin/market/shop/{shop.id}/change/', data) f'/admin/market/shop/{shop.id}/change/', data)
# Redirects to changelist # Redirects to changelist
print(response)
self.assertEqual(response.status_code, 302) self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, '/admin/market/shop/') self.assertEqual(response.url, '/admin/market/shop/')
# Shop has changed # Shop has changed