django-admin-confirm 0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ include *.rst
2
+ recursive-include admin-confirm/*
@@ -0,0 +1,94 @@
1
+ Metadata-Version: 2.1
2
+ Name: django-admin-confirm
3
+ Version: 0.1
4
+ Summary: Adds confirmation to Django Admin changes and additions
5
+ Home-page: https://github.com/trangpham/django-admin-confirm/
6
+ Author: Thu Trang Pham
7
+ Author-email: thuutrangpham@gmail.com
8
+ License: Apache 2.0
9
+ Description: # Django Admin Confirm
10
+
11
+ ![coverage](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/coverage.svg)
12
+
13
+ AdminConfirmMixin is a mixin for ModelAdmin to add confirmations to changes and additions.
14
+
15
+ ![Screenshot of Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/screenshot.png)
16
+
17
+ It can be configured to add a confirmation page upon saving changes and/or additions on ModelAdmin.
18
+
19
+ Typical Usage:
20
+
21
+ ```py
22
+ from admin_confirm import AdminConfirmMixin
23
+
24
+ class MyModelAdmin(AdminConfirmMixin, ModelAdmin):
25
+ confirm_change = True
26
+ confirmation_fields = ['field1', 'field2']
27
+ ```
28
+
29
+ ## Installation
30
+
31
+ Install django-admin-confirm by running:
32
+
33
+ pip install django-admin-confirm
34
+
35
+ Add to INSTALLED_APPS in your project settings before `django.contrib.admin`:
36
+
37
+ INSTALLED_APPS = [
38
+ ...
39
+ 'admin_confirm',
40
+
41
+ 'django.contrib.admin',
42
+ ...
43
+ ]
44
+
45
+ Note that this project follows the template override rules of Django.
46
+ To override a template, your app should be listed before `admin_confirm` in INSTALLED_APPS.
47
+
48
+ ## Configuration Options
49
+
50
+ - `confirm_change` _Optional[bool]_ - decides if changes should trigger confirmation
51
+ - `confirm_add` _Optional[bool]_ - decides if additions should trigger confirmation
52
+ - `confirmation_fields` _Optional[Array[string]]_ - sets which fields changes should trigger confirmation
53
+ - `change_confirmation_template` _Optional[string]_ - path to custom html template to use
54
+
55
+ Note that setting `confirmation_fields` without setting `confirm_change` or `confirm_add` would not trigger confirmation.
56
+
57
+ ## Contribution & Appreciation
58
+
59
+ Contributions are most welcome :) Feel free to:
60
+
61
+ - address an issue
62
+ - raise an issue
63
+ - add more test cases
64
+ - add feature requests
65
+
66
+ Your appreciation is also very welcome :) Feel free to:
67
+
68
+ - star the project
69
+ - open an issue just to share your thanks
70
+
71
+ ## Feature List
72
+
73
+ This is a list of features which could potentially be added in the future. Some of which might make more sense in their own package.
74
+
75
+ - [ ] confirmations on changelist actions
76
+ - [ ] global actions on changelist page
77
+ - [ ] instance actions on change/view page
78
+ - [ ] action logs (adding actions to history of instances)
79
+ - [ ] add help tooltip/popover to any field for more info
80
+ - [ ] run scripts from admin
81
+ - [ ] completed action summary page
82
+ - [ ] add top and bottom areas to instance pages which can be configured for any content
83
+
84
+ ## Support
85
+
86
+ If you are having issues, please let us know through raising an issue.
87
+
88
+ ## License
89
+
90
+ The project is licensed under the Apache 2.0 license.
91
+
92
+ Platform: UNKNOWN
93
+ Requires-Python: >=3
94
+ Description-Content-Type: text/markdown
@@ -0,0 +1,82 @@
1
+ # Django Admin Confirm
2
+
3
+ ![coverage](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/coverage.svg)
4
+
5
+ AdminConfirmMixin is a mixin for ModelAdmin to add confirmations to changes and additions.
6
+
7
+ ![Screenshot of Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/screenshot.png)
8
+
9
+ It can be configured to add a confirmation page upon saving changes and/or additions on ModelAdmin.
10
+
11
+ Typical Usage:
12
+
13
+ ```py
14
+ from admin_confirm import AdminConfirmMixin
15
+
16
+ class MyModelAdmin(AdminConfirmMixin, ModelAdmin):
17
+ confirm_change = True
18
+ confirmation_fields = ['field1', 'field2']
19
+ ```
20
+
21
+ ## Installation
22
+
23
+ Install django-admin-confirm by running:
24
+
25
+ pip install django-admin-confirm
26
+
27
+ Add to INSTALLED_APPS in your project settings before `django.contrib.admin`:
28
+
29
+ INSTALLED_APPS = [
30
+ ...
31
+ 'admin_confirm',
32
+
33
+ 'django.contrib.admin',
34
+ ...
35
+ ]
36
+
37
+ Note that this project follows the template override rules of Django.
38
+ To override a template, your app should be listed before `admin_confirm` in INSTALLED_APPS.
39
+
40
+ ## Configuration Options
41
+
42
+ - `confirm_change` _Optional[bool]_ - decides if changes should trigger confirmation
43
+ - `confirm_add` _Optional[bool]_ - decides if additions should trigger confirmation
44
+ - `confirmation_fields` _Optional[Array[string]]_ - sets which fields changes should trigger confirmation
45
+ - `change_confirmation_template` _Optional[string]_ - path to custom html template to use
46
+
47
+ Note that setting `confirmation_fields` without setting `confirm_change` or `confirm_add` would not trigger confirmation.
48
+
49
+ ## Contribution & Appreciation
50
+
51
+ Contributions are most welcome :) Feel free to:
52
+
53
+ - address an issue
54
+ - raise an issue
55
+ - add more test cases
56
+ - add feature requests
57
+
58
+ Your appreciation is also very welcome :) Feel free to:
59
+
60
+ - star the project
61
+ - open an issue just to share your thanks
62
+
63
+ ## Feature List
64
+
65
+ This is a list of features which could potentially be added in the future. Some of which might make more sense in their own package.
66
+
67
+ - [ ] confirmations on changelist actions
68
+ - [ ] global actions on changelist page
69
+ - [ ] instance actions on change/view page
70
+ - [ ] action logs (adding actions to history of instances)
71
+ - [ ] add help tooltip/popover to any field for more info
72
+ - [ ] run scripts from admin
73
+ - [ ] completed action summary page
74
+ - [ ] add top and bottom areas to instance pages which can be configured for any content
75
+
76
+ ## Support
77
+
78
+ If you are having issues, please let us know through raising an issue.
79
+
80
+ ## License
81
+
82
+ The project is licensed under the Apache 2.0 license.
@@ -0,0 +1 @@
1
+ from .admin import AdminConfirmMixin
@@ -0,0 +1,170 @@
1
+ from django.contrib.admin.exceptions import DisallowedModelAdminToField
2
+ from django.contrib.admin.utils import flatten_fieldsets, unquote
3
+ from django.core.exceptions import PermissionDenied
4
+ from django.template.response import TemplateResponse
5
+ from django.contrib.admin.options import TO_FIELD_VAR
6
+ from django.utils.translation import gettext as _
7
+
8
+
9
+ class AdminConfirmMixin:
10
+ # Should we ask for confirmation for changes?
11
+ confirm_change = None
12
+
13
+ # Should we ask for confirmation for additions?
14
+ confirm_add = None
15
+
16
+ # If asking for confirmation, which fields should we confirm for?
17
+ confirmation_fields = None
18
+
19
+ # Custom templates (designed to be over-ridden in subclasses)
20
+ confirmation_template = None
21
+
22
+ def get_confirmation_fields(self, request, obj=None):
23
+ """
24
+ Hook for specifying confirmation fields
25
+ """
26
+ if self.confirmation_fields is not None:
27
+ return self.confirmation_fields
28
+
29
+ return flatten_fieldsets(self.get_fieldsets(request, obj))
30
+
31
+ def render_change_confirmation(self, request, context):
32
+ opts = self.model._meta
33
+ app_label = opts.app_label
34
+
35
+ request.current_app = self.admin_site.name
36
+ context.update(
37
+ media=self.media,
38
+ )
39
+
40
+ return TemplateResponse(
41
+ request,
42
+ self.confirmation_template
43
+ or [
44
+ "admin/{}/{}/change_confirmation.html".format(
45
+ app_label, opts.model_name
46
+ ),
47
+ "admin/{}/change_confirmation.html".format(app_label),
48
+ "admin/change_confirmation.html",
49
+ ],
50
+ context,
51
+ )
52
+
53
+ def changeform_view(self, request, object_id=None, form_url="", extra_context=None):
54
+ if request.method == "POST":
55
+ if (not object_id and "_confirm_add" in request.POST) or (
56
+ object_id and "_confirm_change" in request.POST
57
+ ):
58
+ return self._change_confirmation_view(
59
+ request, object_id, form_url, extra_context
60
+ )
61
+
62
+ extra_context = {
63
+ **(extra_context or {}),
64
+ "confirm_add": self.confirm_add,
65
+ "confirm_change": self.confirm_change,
66
+ }
67
+ return super().changeform_view(request, object_id, form_url, extra_context)
68
+
69
+ def _change_confirmation_view(self, request, object_id, form_url, extra_context):
70
+ # This code is taken from super()._changeform_view
71
+ to_field = request.POST.get(TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR))
72
+ if to_field and not self.to_field_allowed(request, to_field):
73
+ raise DisallowedModelAdminToField(
74
+ "The field %s cannot be referenced." % to_field
75
+ )
76
+
77
+ model = self.model
78
+ opts = model._meta
79
+
80
+ add = object_id is None
81
+ if add:
82
+ if not self.has_add_permission(request):
83
+ raise PermissionDenied
84
+
85
+ obj = None
86
+ else:
87
+ obj = self.get_object(request, unquote(object_id), to_field)
88
+ if obj is None:
89
+ return self._get_obj_does_not_exist_redirect(request, opts, object_id)
90
+
91
+ if not self.has_view_or_change_permission(request, obj):
92
+ raise PermissionDenied
93
+
94
+ fieldsets = self.get_fieldsets(request, obj)
95
+ ModelForm = self.get_form(
96
+ request, obj, change=not add, fields=flatten_fieldsets(fieldsets)
97
+ )
98
+
99
+ form = ModelForm(request.POST, request.FILES, obj)
100
+ form_validated = form.is_valid()
101
+ if form_validated:
102
+ new_object = self.save_form(request, form, change=not add)
103
+ else:
104
+ new_object = form.instance
105
+
106
+ # End code from super()._changeform_view
107
+
108
+ changed_data = {}
109
+ if form_validated:
110
+ if add:
111
+ for name in form.changed_data:
112
+ new_value = getattr(new_object, name)
113
+ # Don't consider default values as changed for adding
114
+ if (
115
+ new_value is not None
116
+ and new_value != model._meta.get_field(name).default
117
+ ):
118
+ changed_data[name] = [None, new_value]
119
+ else:
120
+ # Parse the changed data - Note that using form.changed_data would not work because initial is not set
121
+ for name, field in form.fields.items():
122
+ initial_value = getattr(obj, name)
123
+ new_value = getattr(new_object, name)
124
+ if (
125
+ field.has_changed(initial_value, new_value)
126
+ and initial_value != new_value
127
+ ):
128
+ changed_data[name] = [initial_value, new_value]
129
+
130
+ changed_confirmation_fields = set(
131
+ self.get_confirmation_fields(request, obj)
132
+ ) & set(changed_data.keys())
133
+ if not bool(changed_confirmation_fields):
134
+ # No confirmation required for changed fields, continue to save
135
+ return super()._changeform_view(request, object_id, form_url, extra_context)
136
+
137
+ # Parse raw form data from POST
138
+ form_data = {}
139
+ # Parse the original save action from request
140
+ save_action = None
141
+ for key in request.POST:
142
+ if key in ["_save", "_saveasnew", "_addanother", "_continue"]:
143
+ save_action = key
144
+
145
+ if key.startswith("_") or key == "csrfmiddlewaretoken":
146
+ continue
147
+ form_data[key] = request.POST.get(key)
148
+
149
+ if add:
150
+ title_action = _("adding")
151
+ else:
152
+ title_action = _("changing")
153
+
154
+ context = {
155
+ **self.admin_site.each_context(request),
156
+ "preserved_filters": self.get_preserved_filters(request),
157
+ "title": f"{_('Confirm')} {title_action} {opts.verbose_name}",
158
+ "subtitle": str(obj),
159
+ "object_name": str(obj),
160
+ "object_id": object_id,
161
+ "app_label": opts.app_label,
162
+ "model_name": opts.model_name,
163
+ "opts": opts,
164
+ "form_data": form_data,
165
+ "changed_data": changed_data,
166
+ "add": add,
167
+ "submit_name": save_action,
168
+ **(extra_context or {}),
169
+ }
170
+ return self.render_change_confirmation(request, context)
@@ -0,0 +1,94 @@
1
+ Metadata-Version: 2.1
2
+ Name: django-admin-confirm
3
+ Version: 0.1
4
+ Summary: Adds confirmation to Django Admin changes and additions
5
+ Home-page: https://github.com/trangpham/django-admin-confirm/
6
+ Author: Thu Trang Pham
7
+ Author-email: thuutrangpham@gmail.com
8
+ License: Apache 2.0
9
+ Description: # Django Admin Confirm
10
+
11
+ ![coverage](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/coverage.svg)
12
+
13
+ AdminConfirmMixin is a mixin for ModelAdmin to add confirmations to changes and additions.
14
+
15
+ ![Screenshot of Confirmation Page](https://raw.githubusercontent.com/TrangPham/django-admin-confirm/main/screenshot.png)
16
+
17
+ It can be configured to add a confirmation page upon saving changes and/or additions on ModelAdmin.
18
+
19
+ Typical Usage:
20
+
21
+ ```py
22
+ from admin_confirm import AdminConfirmMixin
23
+
24
+ class MyModelAdmin(AdminConfirmMixin, ModelAdmin):
25
+ confirm_change = True
26
+ confirmation_fields = ['field1', 'field2']
27
+ ```
28
+
29
+ ## Installation
30
+
31
+ Install django-admin-confirm by running:
32
+
33
+ pip install django-admin-confirm
34
+
35
+ Add to INSTALLED_APPS in your project settings before `django.contrib.admin`:
36
+
37
+ INSTALLED_APPS = [
38
+ ...
39
+ 'admin_confirm',
40
+
41
+ 'django.contrib.admin',
42
+ ...
43
+ ]
44
+
45
+ Note that this project follows the template override rules of Django.
46
+ To override a template, your app should be listed before `admin_confirm` in INSTALLED_APPS.
47
+
48
+ ## Configuration Options
49
+
50
+ - `confirm_change` _Optional[bool]_ - decides if changes should trigger confirmation
51
+ - `confirm_add` _Optional[bool]_ - decides if additions should trigger confirmation
52
+ - `confirmation_fields` _Optional[Array[string]]_ - sets which fields changes should trigger confirmation
53
+ - `change_confirmation_template` _Optional[string]_ - path to custom html template to use
54
+
55
+ Note that setting `confirmation_fields` without setting `confirm_change` or `confirm_add` would not trigger confirmation.
56
+
57
+ ## Contribution & Appreciation
58
+
59
+ Contributions are most welcome :) Feel free to:
60
+
61
+ - address an issue
62
+ - raise an issue
63
+ - add more test cases
64
+ - add feature requests
65
+
66
+ Your appreciation is also very welcome :) Feel free to:
67
+
68
+ - star the project
69
+ - open an issue just to share your thanks
70
+
71
+ ## Feature List
72
+
73
+ This is a list of features which could potentially be added in the future. Some of which might make more sense in their own package.
74
+
75
+ - [ ] confirmations on changelist actions
76
+ - [ ] global actions on changelist page
77
+ - [ ] instance actions on change/view page
78
+ - [ ] action logs (adding actions to history of instances)
79
+ - [ ] add help tooltip/popover to any field for more info
80
+ - [ ] run scripts from admin
81
+ - [ ] completed action summary page
82
+ - [ ] add top and bottom areas to instance pages which can be configured for any content
83
+
84
+ ## Support
85
+
86
+ If you are having issues, please let us know through raising an issue.
87
+
88
+ ## License
89
+
90
+ The project is licensed under the Apache 2.0 license.
91
+
92
+ Platform: UNKNOWN
93
+ Requires-Python: >=3
94
+ Description-Content-Type: text/markdown
@@ -0,0 +1,10 @@
1
+ MANIFEST.in
2
+ README.md
3
+ setup.py
4
+ admin_confirm/__init__.py
5
+ admin_confirm/admin.py
6
+ django_admin_confirm.egg-info/PKG-INFO
7
+ django_admin_confirm.egg-info/SOURCES.txt
8
+ django_admin_confirm.egg-info/dependency_links.txt
9
+ django_admin_confirm.egg-info/requires.txt
10
+ django_admin_confirm.egg-info/top_level.txt
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,22 @@
1
+ import os
2
+ from setuptools import setup
3
+
4
+ here = os.path.abspath(os.path.dirname(__file__))
5
+ README = open(os.path.join(here, "README.md")).read()
6
+
7
+ setup(
8
+ name="django-admin-confirm",
9
+ version="0.1",
10
+ packages=["admin_confirm"],
11
+ description="Adds confirmation to Django Admin changes and additions",
12
+ long_description_content_type="text/markdown",
13
+ long_description=README,
14
+ author="Thu Trang Pham",
15
+ author_email="thuutrangpham@gmail.com",
16
+ url="https://github.com/trangpham/django-admin-confirm/",
17
+ license="Apache 2.0",
18
+ install_requires=[
19
+ "Django>=1.7",
20
+ ],
21
+ python_requires='>=3',
22
+ )