feat(ISSUE-3): travis and coveralls (#10)
* feat(ISSUE-8): ISSUE-8: ManyToManyField causes error on confirmations * feat(ISSUE-8): Update some readme and remove print statements * feat(ISSUE-8): Generate new version of package * feat(ISSUE-3): Adding .travis.yml * feat(ISSUE-3): Adding coveralls * feat(ISSUE-3): Trying github actions * feat(ISSUE-3): remove travis * feat(ISSUE-3): Change python versions to test * feat(ISSUE-3): Some refactoring and trying tox * feat(ISSUE-3): Try action matrix * feat(ISSUE-3): Some more refactors * feat(ISSUE-3): Fix tests * feat(ISSUE-3): Refactor/fix tests * feat(ISSUE-3): Remove tox * feat(ISSUE-3): Adding pypi version badge to readme * feat(ISSUE-3): Update readme again Co-authored-by: Thu Trang Pham <thu@joinmodernhealth.com>
This commit is contained in:
@@ -1 +1,2 @@
|
||||
from .admin import AdminConfirmMixin
|
||||
__all__ = ["admin"]
|
||||
from .admin import AdminConfirmMixin # noqa
|
||||
|
||||
+46
-25
@@ -1,3 +1,4 @@
|
||||
from typing import Dict
|
||||
from django.contrib.admin.exceptions import DisallowedModelAdminToField
|
||||
from django.contrib.admin.utils import flatten_fieldsets, unquote
|
||||
from django.core.exceptions import PermissionDenied
|
||||
@@ -5,8 +6,12 @@ from django.template.response import TemplateResponse
|
||||
from django.contrib.admin.options import TO_FIELD_VAR
|
||||
from django.utils.translation import gettext as _
|
||||
from django.contrib.admin import helpers
|
||||
from django.db.models import Model
|
||||
from django.forms import ModelForm
|
||||
from admin_confirm.utils import snake_to_title_case
|
||||
|
||||
SAVE_ACTIONS = ["_save", "_saveasnew", "_addanother", "_continue"]
|
||||
|
||||
|
||||
class AdminConfirmMixin:
|
||||
# Should we ask for confirmation for changes?
|
||||
@@ -92,6 +97,40 @@ class AdminConfirmMixin:
|
||||
}
|
||||
return super().changeform_view(request, object_id, form_url, extra_context)
|
||||
|
||||
def _get_changed_data(
|
||||
self, form: ModelForm, model: Model, obj: object, add: bool
|
||||
) -> Dict:
|
||||
"""
|
||||
Given a form, detect the changes on the form from the default values (if add) or
|
||||
from the database values of the object (model instance)
|
||||
|
||||
form - Submitted form that is attempting to alter the obj
|
||||
model - the model class of the obj
|
||||
obj - instance of model which is being altered
|
||||
add - are we attempting to add the obj or does it already exist in the database
|
||||
|
||||
Returns a dictionary of the fields and their changed values if any
|
||||
"""
|
||||
changed_data = {}
|
||||
if form.is_valid():
|
||||
if add:
|
||||
for name, new_value in form.cleaned_data.items():
|
||||
# Don't consider default values as changed for adding
|
||||
default_value = model._meta.get_field(name).get_default()
|
||||
if new_value is not None and new_value != default_value:
|
||||
# Show what the default value is
|
||||
changed_data[name] = [str(default_value), new_value]
|
||||
else:
|
||||
# Parse the changed data - Note that using form.changed_data would not work because initial is not set
|
||||
for name, new_value in form.cleaned_data.items():
|
||||
# Since the form considers initial as the value first shown in the form
|
||||
# It could be incorrect when user hits save, and then hits "No, go back to edit"
|
||||
obj.refresh_from_db()
|
||||
initial_value = getattr(obj, name)
|
||||
if initial_value != new_value:
|
||||
changed_data[name] = [initial_value, new_value]
|
||||
return changed_data
|
||||
|
||||
def _change_confirmation_view(self, request, object_id, form_url, extra_context):
|
||||
# This code is taken from super()._changeform_view
|
||||
to_field = request.POST.get(TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR))
|
||||
@@ -125,24 +164,7 @@ class AdminConfirmMixin:
|
||||
form = ModelForm(request.POST, request.FILES, obj)
|
||||
# End code from super()._changeform_view
|
||||
|
||||
changed_data = {}
|
||||
if form.is_valid():
|
||||
if add:
|
||||
for name, new_value in form.cleaned_data.items():
|
||||
# Don't consider default values as changed for adding
|
||||
default_value = model._meta.get_field(name).get_default()
|
||||
if new_value is not None and new_value != default_value:
|
||||
# Show what the default value is
|
||||
changed_data[name] = [str(default_value), new_value]
|
||||
else:
|
||||
# Parse the changed data - Note that using form.changed_data would not work because initial is not set
|
||||
for name, new_value in form.cleaned_data.items():
|
||||
# Since the form considers initial as the value first shown in the form
|
||||
# It could be incorrect when user hits save, and then hits "No, go back to edit"
|
||||
obj.refresh_from_db()
|
||||
initial_value = getattr(obj, name)
|
||||
if initial_value != new_value:
|
||||
changed_data[name] = [initial_value, new_value]
|
||||
changed_data = self._get_changed_data(form, model, obj, add)
|
||||
|
||||
changed_confirmation_fields = set(
|
||||
self.get_confirmation_fields(request, obj)
|
||||
@@ -155,18 +177,17 @@ class AdminConfirmMixin:
|
||||
form_data = {}
|
||||
# Parse the original save action from request
|
||||
save_action = None
|
||||
for key in request.POST:
|
||||
if key in ["_save", "_saveasnew", "_addanother", "_continue"]:
|
||||
for key, value in request.POST.items():
|
||||
if key in SAVE_ACTIONS:
|
||||
save_action = key
|
||||
continue
|
||||
|
||||
if key.startswith("_") or key == "csrfmiddlewaretoken":
|
||||
continue
|
||||
form_data[key] = request.POST.get(key)
|
||||
|
||||
if add:
|
||||
title_action = _("adding")
|
||||
else:
|
||||
title_action = _("changing")
|
||||
form_data[key] = value
|
||||
|
||||
title_action = _("adding") if add else _("changing")
|
||||
|
||||
context = {
|
||||
**self.admin_site.each_context(request),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from django.test import TestCase, RequestFactory
|
||||
from django.contrib.admin.sites import AdminSite
|
||||
from django.contrib.auth.models import Permission, User
|
||||
from django.contrib.admin.options import TO_FIELD_VAR
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user