django-formdefaults 0.5.0__py3-none-any.whl
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.
- django_formdefaults-0.5.0.dist-info/METADATA +405 -0
- django_formdefaults-0.5.0.dist-info/RECORD +38 -0
- django_formdefaults-0.5.0.dist-info/WHEEL +4 -0
- django_formdefaults-0.5.0.dist-info/licenses/LICENSE +21 -0
- formdefaults/__init__.py +3 -0
- formdefaults/_autosnap_backfill.py +29 -0
- formdefaults/admin.py +66 -0
- formdefaults/apps.py +15 -0
- formdefaults/core.py +151 -0
- formdefaults/forms.py +123 -0
- formdefaults/helpers.py +24 -0
- formdefaults/locale/pl/LC_MESSAGES/django.mo +0 -0
- formdefaults/locale/pl/LC_MESSAGES/django.po +47 -0
- formdefaults/management/__init__.py +0 -0
- formdefaults/management/commands/__init__.py +0 -0
- formdefaults/management/commands/dedupe_formdefaults.py +31 -0
- formdefaults/migrations/0001_initial.py +139 -0
- formdefaults/migrations/0002_django32.py +17 -0
- formdefaults/migrations/0003_formrepresentation_pre_registered.py +21 -0
- formdefaults/migrations/0004_unique_field_user.py +24 -0
- formdefaults/migrations/0005_unique_field_system.py +22 -0
- formdefaults/migrations/0006_formfielddefaultvalue_is_auto_snapshot.py +21 -0
- formdefaults/migrations/0007_backfill_is_auto_snapshot.py +34 -0
- formdefaults/migrations/__init__.py +0 -0
- formdefaults/models.py +188 -0
- formdefaults/permissions.py +51 -0
- formdefaults/registry.py +64 -0
- formdefaults/signals.py +60 -0
- formdefaults/static/formdefaults/modal.css +84 -0
- formdefaults/static/formdefaults/modal.js +89 -0
- formdefaults/templates/formdefaults/_button.html +8 -0
- formdefaults/templates/formdefaults/_modal_fragment.html +19 -0
- formdefaults/templates/formdefaults/_user_edit_form.html +10 -0
- formdefaults/templatetags/__init__.py +0 -0
- formdefaults/templatetags/formdefaults.py +25 -0
- formdefaults/urls.py +18 -0
- formdefaults/util.py +17 -0
- formdefaults/views.py +114 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-formdefaults
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: Per-user, database-backed default values for Django forms — lets users (and admins) save the values they typed last time.
|
|
5
|
+
Project-URL: Homepage, https://github.com/iplweb/django-formdefaults
|
|
6
|
+
Project-URL: Repository, https://github.com/iplweb/django-formdefaults
|
|
7
|
+
Project-URL: Issues, https://github.com/iplweb/django-formdefaults/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/iplweb/django-formdefaults/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Michał Pasternak <michal.dtz@gmail.com>
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: defaults,django,form-defaults,forms,initial-values,per-user
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Environment :: Web Environment
|
|
15
|
+
Classifier: Framework :: Django
|
|
16
|
+
Classifier: Framework :: Django :: 4.2
|
|
17
|
+
Classifier: Framework :: Django :: 5.0
|
|
18
|
+
Classifier: Framework :: Django :: 5.1
|
|
19
|
+
Classifier: Framework :: Django :: 5.2
|
|
20
|
+
Classifier: Intended Audience :: Developers
|
|
21
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
22
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
23
|
+
Classifier: Operating System :: POSIX
|
|
24
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
25
|
+
Classifier: Programming Language :: Python
|
|
26
|
+
Classifier: Programming Language :: Python :: 3
|
|
27
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
30
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
31
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
32
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
33
|
+
Requires-Python: >=3.10
|
|
34
|
+
Requires-Dist: django>=4.2
|
|
35
|
+
Provides-Extra: test
|
|
36
|
+
Requires-Dist: psycopg[binary]>=3; extra == 'test'
|
|
37
|
+
Requires-Dist: pytest-django>=4.8; extra == 'test'
|
|
38
|
+
Requires-Dist: pytest>=7; extra == 'test'
|
|
39
|
+
Requires-Dist: testcontainers[postgres]>=4; extra == 'test'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# django-formdefaults
|
|
43
|
+
|
|
44
|
+
[](https://github.com/iplweb/django-formdefaults/actions/workflows/tests.yml)
|
|
45
|
+
[](https://pypi.org/project/django-formdefaults/)
|
|
46
|
+
[](https://pypi.org/project/django-formdefaults/)
|
|
47
|
+
[](LICENSE)
|
|
48
|
+
|
|
49
|
+
Database-backed default values for Django forms. Plug it into any Django form
|
|
50
|
+
in one line and admins (system-wide) plus end-users (personal overrides) can
|
|
51
|
+
curate `Form.initial` from the UI.
|
|
52
|
+
|
|
53
|
+
Originally extracted from [iplweb/bpp](https://github.com/iplweb/bpp).
|
|
54
|
+
|
|
55
|
+
## Screenshots
|
|
56
|
+
|
|
57
|
+
| Regular user | Superuser |
|
|
58
|
+
|---|---|
|
|
59
|
+
|  |  |
|
|
60
|
+
| One button — opens the popup that edits *their own* override. | Two buttons — adds a second popup for editing the system-wide default that every user sees. |
|
|
61
|
+
|
|
62
|
+
System-wide editor (admins only — by default `is_superuser`, overridable
|
|
63
|
+
per-form or globally via a hook):
|
|
64
|
+
|
|
65
|
+

|
|
66
|
+
|
|
67
|
+
## Why?
|
|
68
|
+
|
|
69
|
+
Django forms usually default to whatever a developer hard-coded in
|
|
70
|
+
`Form.initial` — fine for a brand-new form, painful when the same user
|
|
71
|
+
fills the same form weekly with the same five fields. This package lets
|
|
72
|
+
admins curate defaults system-wide from the UI, and lets each user
|
|
73
|
+
override their own defaults via a popup. No code changes per form.
|
|
74
|
+
|
|
75
|
+
## Features
|
|
76
|
+
|
|
77
|
+
- **Two editing scopes** — system-wide (admins) and per-user (end users),
|
|
78
|
+
both available from a popup next to the form. Per-user overrides shadow
|
|
79
|
+
system-wide values.
|
|
80
|
+
- **Permission hook** — `formdefaults_can_edit_system_wide(user, form_repr)`
|
|
81
|
+
decides who sees the "System defaults" button. Default: `user.is_superuser`.
|
|
82
|
+
Overridable per-form via a class attribute or globally via the
|
|
83
|
+
`FORMDEFAULTS_CAN_EDIT_SYSTEM_WIDE` setting.
|
|
84
|
+
- **Three ways to register a form** — `@register_form` decorator,
|
|
85
|
+
`FORMDEFAULTS_FORMS` setting (for forms you don't own), or zero
|
|
86
|
+
registration (snapshot on first render via `FormDefaultsMixin` /
|
|
87
|
+
`get_form_defaults`).
|
|
88
|
+
- **Vanilla-JS popup**, no jQuery / HTMX dependency. Renders a modal
|
|
89
|
+
with one input per form field, pre-filled with the currently-effective
|
|
90
|
+
default. Companion checkbox per field decides whether to upsert or
|
|
91
|
+
clear the override.
|
|
92
|
+
- **Auto-snapshot lifecycle** — rows tracking the code-level `Form.initial`
|
|
93
|
+
auto-refresh when developers change `initial=...`, but become sticky
|
|
94
|
+
the moment a user/admin edits them in the UI.
|
|
95
|
+
- **JSON-storable defaults** — covers the common Django field types
|
|
96
|
+
(numbers, strings, booleans, dates/datetimes/times, `ModelChoiceField`
|
|
97
|
+
PKs); non-serialisable lambdas/callables keep working at the form
|
|
98
|
+
level but aren't persisted.
|
|
99
|
+
- **Optional in-form HTML** — each form representation has
|
|
100
|
+
`html_before`/`html_after` text fields available as
|
|
101
|
+
`formdefaults_pre_html` / `formdefaults_post_html` in the form's
|
|
102
|
+
`initial` dict.
|
|
103
|
+
|
|
104
|
+
## Idea
|
|
105
|
+
|
|
106
|
+
When a form is rendered, `django-formdefaults`:
|
|
107
|
+
|
|
108
|
+
1. **Builds or refreshes a representation of the form in the DB** — its set
|
|
109
|
+
of fields, their order, types and labels, and a snapshot of `Form.initial`.
|
|
110
|
+
2. **Lets you set default values per field** — e.g. a boolean that should
|
|
111
|
+
always default to `True`, a date that should always default to the current
|
|
112
|
+
month, an integer with a fixed initial.
|
|
113
|
+
3. **Exposes two editing scopes**:
|
|
114
|
+
- **System-wide** — any superuser edits in Django admin (one default per
|
|
115
|
+
field, applied to everyone).
|
|
116
|
+
- **Per-user** — each logged-in user overrides their own copy via a popup
|
|
117
|
+
rendered next to the form. Their override shadows the system-wide value.
|
|
118
|
+
|
|
119
|
+
A form's DB representation can be created in **three ways**:
|
|
120
|
+
|
|
121
|
+
- `@register_form` decorator on the Form class — snapshot at Django startup
|
|
122
|
+
(`post_migrate`).
|
|
123
|
+
- `FORMDEFAULTS_FORMS` setting — list of dotted paths, also snapshot at
|
|
124
|
+
startup. Useful for forms you don't own.
|
|
125
|
+
- **No registration** — snapshot happens on first render via
|
|
126
|
+
`get_form_defaults()` / `FormDefaultsMixin`.
|
|
127
|
+
|
|
128
|
+
## Installation
|
|
129
|
+
|
|
130
|
+
Using [uv](https://docs.astral.sh/uv/) (recommended):
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
uv add django-formdefaults
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Using pip:
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
pip install django-formdefaults
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`INSTALLED_APPS`:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
INSTALLED_APPS = [
|
|
146
|
+
# ...
|
|
147
|
+
"formdefaults",
|
|
148
|
+
]
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
`urls.py` (only required if you want the per-user popup):
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
urlpatterns = [
|
|
155
|
+
# ...
|
|
156
|
+
path("formdefaults/", include("formdefaults.urls")),
|
|
157
|
+
]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Run migrations:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
./manage.py migrate
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Supported versions
|
|
167
|
+
|
|
168
|
+
### Python
|
|
169
|
+
|
|
170
|
+
| 3.10 | 3.11 | 3.12 | 3.13 |
|
|
171
|
+
|------|------|------|------|
|
|
172
|
+
| ✓ | ✓ | ✓ | ✓ |
|
|
173
|
+
|
|
174
|
+
### Django × Python
|
|
175
|
+
|
|
176
|
+
| Django | 3.10 | 3.11 | 3.12 | 3.13 | Status |
|
|
177
|
+
|---------|------|------|------|------|---------------------------------|
|
|
178
|
+
| 4.2 LTS | ✓ | ✓ | ✓ | — | EOL April 2026 |
|
|
179
|
+
| 5.0 | ✓ | ✓ | ✓ | — | EOL April 2025 |
|
|
180
|
+
| 5.1 | ✓ | ✓ | ✓ | ✓ | EOL December 2025 |
|
|
181
|
+
| 5.2 LTS | ✓ | ✓ | ✓ | ✓ | Active LTS (extended Apr 2028) |
|
|
182
|
+
| 6.0 | — | — | ✓ | ✓ | Pre-release (testing on `--pre`)|
|
|
183
|
+
|
|
184
|
+
Cells reflect what CI actually exercises — see
|
|
185
|
+
[`.github/workflows/tests.yml`](.github/workflows/tests.yml).
|
|
186
|
+
|
|
187
|
+
## Quick start
|
|
188
|
+
|
|
189
|
+
### Path 1 — Decorator (recommended for forms you own)
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
# myapp/forms.py
|
|
193
|
+
from django import forms
|
|
194
|
+
from formdefaults import register_form
|
|
195
|
+
|
|
196
|
+
@register_form(label="Monthly report")
|
|
197
|
+
class MonthlyReportForm(forms.Form):
|
|
198
|
+
year = forms.IntegerField(initial=2026)
|
|
199
|
+
month = forms.ChoiceField(choices=[(i, str(i)) for i in range(1, 13)])
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Snapshot is created on `migrate`.
|
|
203
|
+
|
|
204
|
+
### Path 2 — Setting (for forms you don't own)
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
# settings.py
|
|
208
|
+
FORMDEFAULTS_FORMS = [
|
|
209
|
+
"thirdparty.forms.SomeForm",
|
|
210
|
+
"myapp.forms.UserSettingsForm",
|
|
211
|
+
]
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Optional class-level `formdefaults_label = "..."` becomes the row's label;
|
|
215
|
+
otherwise the class name is used.
|
|
216
|
+
|
|
217
|
+
### Path 3 — Ad-hoc (no registration)
|
|
218
|
+
|
|
219
|
+
In a CBV, mix in `FormDefaultsMixin`:
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
from formdefaults.helpers import FormDefaultsMixin
|
|
223
|
+
|
|
224
|
+
class MonthlyReportView(FormDefaultsMixin, FormView):
|
|
225
|
+
form_class = MonthlyReportForm
|
|
226
|
+
title = "Monthly report"
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
In an FBV, call `get_form_defaults`:
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
from formdefaults.core import get_form_defaults
|
|
233
|
+
|
|
234
|
+
initial = get_form_defaults(MonthlyReportForm(), user=request.user)
|
|
235
|
+
form = MonthlyReportForm(initial=initial)
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Either way, snapshot is created on first render.
|
|
239
|
+
|
|
240
|
+
## Editing defaults
|
|
241
|
+
|
|
242
|
+
### System-wide — popup (preferred)
|
|
243
|
+
|
|
244
|
+
When the current user passes the
|
|
245
|
+
`formdefaults_can_edit_system_wide(user, form_repr)` permission check
|
|
246
|
+
(default: `is_superuser`), `{% formdefaults_button form %}` renders a
|
|
247
|
+
second button — **System defaults** — that opens the same popup but bound
|
|
248
|
+
to `FormFieldDefaultValue` rows with `user=NULL`. The modal is visibly
|
|
249
|
+
flagged so admins don't mistakenly edit the wrong scope.
|
|
250
|
+
|
|
251
|
+
To restrict or expand who can edit system-wide defaults:
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
# settings.py — global override
|
|
255
|
+
FORMDEFAULTS_CAN_EDIT_SYSTEM_WIDE = "myapp.permissions.can_edit_fd_system"
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
# myapp/forms.py — per-form override (wins over the global one)
|
|
260
|
+
class SecretForm(forms.Form):
|
|
261
|
+
formdefaults_can_edit_system_wide = staticmethod(
|
|
262
|
+
lambda user, form_repr: user.is_superuser and user.username == "alice"
|
|
263
|
+
)
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### System-wide (Django admin)
|
|
267
|
+
|
|
268
|
+
Still available at `/admin/formdefaults/formrepresentation/` — pick a form
|
|
269
|
+
by label, then for each field add or edit a `FormFieldDefaultValue` row
|
|
270
|
+
with `User` empty.
|
|
271
|
+
|
|
272
|
+
The `FormRepresentation` row also has `html_before` and `html_after` text
|
|
273
|
+
fields, useful for surfacing in-form legends, contextual help, or a quick
|
|
274
|
+
note. They land in the form's `initial` dict under
|
|
275
|
+
`formdefaults_pre_html` and `formdefaults_post_html`. Render them in your
|
|
276
|
+
template however you like:
|
|
277
|
+
|
|
278
|
+
```django
|
|
279
|
+
{{ form.initial.formdefaults_pre_html|safe }}
|
|
280
|
+
{{ form }}
|
|
281
|
+
{{ form.initial.formdefaults_post_html|safe }}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Per-user (popup next to the form)
|
|
285
|
+
|
|
286
|
+
In your template:
|
|
287
|
+
|
|
288
|
+
```django
|
|
289
|
+
{% load formdefaults static %}
|
|
290
|
+
|
|
291
|
+
<form method="post">
|
|
292
|
+
{% csrf_token %}
|
|
293
|
+
{{ form }}
|
|
294
|
+
<button type="submit">Submit</button>
|
|
295
|
+
</form>
|
|
296
|
+
|
|
297
|
+
{% formdefaults_button form %}
|
|
298
|
+
|
|
299
|
+
<script src="{% static 'formdefaults/modal.js' %}" defer></script>
|
|
300
|
+
<link rel="stylesheet" href="{% static 'formdefaults/modal.css' %}">
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
> Place `{% formdefaults_button form %}` **outside** the `<form>` element — the modal injects its own `<form>` for saving overrides, and HTML5 forbids nesting forms.
|
|
304
|
+
|
|
305
|
+
The button only renders for authenticated users. Clicking it opens a modal
|
|
306
|
+
with one input per form field, pre-filled with the currently-effective
|
|
307
|
+
default value (your override if you have one, otherwise the system-wide
|
|
308
|
+
value).
|
|
309
|
+
|
|
310
|
+
Each field in the popup has a small checkbox to its left. The checkbox
|
|
311
|
+
controls whether your edit becomes an override:
|
|
312
|
+
|
|
313
|
+
- Unchecked → leave the field alone; the system-wide default applies to
|
|
314
|
+
you.
|
|
315
|
+
- Checked → save the value next to it as your personal override.
|
|
316
|
+
|
|
317
|
+
The checkbox auto-checks when you actually edit the field, so the common
|
|
318
|
+
flow is "type your new value, save". Uncheck if you want to delete a
|
|
319
|
+
previously-saved override.
|
|
320
|
+
|
|
321
|
+
## Try it locally
|
|
322
|
+
|
|
323
|
+
```bash
|
|
324
|
+
git clone https://github.com/iplweb/django-formdefaults
|
|
325
|
+
cd django-formdefaults/example_project
|
|
326
|
+
python manage.py migrate
|
|
327
|
+
python manage.py runserver
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
Visit `http://127.0.0.1:8000/`. Three forms demonstrate all three registration
|
|
331
|
+
paths. Create a superuser (`./manage.py createsuperuser`) to try system-wide
|
|
332
|
+
editing in `/admin/`.
|
|
333
|
+
|
|
334
|
+
## Public API
|
|
335
|
+
|
|
336
|
+
| Symbol | Purpose |
|
|
337
|
+
|---|---|
|
|
338
|
+
| `formdefaults.register_form` | Decorator: register a Form class for startup snapshot. |
|
|
339
|
+
| `formdefaults.helpers.FormDefaultsMixin` | CBV mixin: provides `get_initial()`. |
|
|
340
|
+
| `formdefaults.core.get_form_defaults(form, label=None, user=None, update_db_repr=True)` | Snapshot + return `{field_name: value}`. |
|
|
341
|
+
| `formdefaults.core.update_form_db_repr(form, form_repr, user=None)` | Lower-level: refresh DB representation. |
|
|
342
|
+
| `formdefaults.forms.build_user_defaults_form(form_repr, user, data=None)` | Build the popup edit form. |
|
|
343
|
+
| `formdefaults.views.UserFormDefaultsView` | View backing the per-user popup endpoint. |
|
|
344
|
+
| `formdefaults.views.SystemFormDefaultsView` | View backing the system-wide popup endpoint. |
|
|
345
|
+
| `formdefaults.permissions.can_edit_system_wide_defaults(user, form_repr=None, form_class=None)` | Resolve the permission hook (per-form attr → setting → default). |
|
|
346
|
+
| `{% formdefaults_button form %}` | Template tag rendering the personal button — and, if the hook allows, the system-defaults button. |
|
|
347
|
+
| `formdefaults.models.FormRepresentation` / `FormFieldRepresentation` / `FormFieldDefaultValue` | DB models. |
|
|
348
|
+
|
|
349
|
+
## Storage
|
|
350
|
+
|
|
351
|
+
Three tables:
|
|
352
|
+
|
|
353
|
+
```
|
|
354
|
+
FormRepresentation
|
|
355
|
+
full_name (PK) # "myapp.forms.MonthlyReportForm"
|
|
356
|
+
label # human-readable
|
|
357
|
+
pre_registered # True if registered via decorator/setting
|
|
358
|
+
html_before, html_after
|
|
359
|
+
|
|
360
|
+
FormFieldRepresentation
|
|
361
|
+
parent → FormRepresentation
|
|
362
|
+
name, label, klass, order
|
|
363
|
+
unique_together (parent, name)
|
|
364
|
+
|
|
365
|
+
FormFieldDefaultValue
|
|
366
|
+
parent → FormRepresentation
|
|
367
|
+
field → FormFieldRepresentation
|
|
368
|
+
user → AUTH_USER_MODEL # nullable; null = system-wide
|
|
369
|
+
value (JSON)
|
|
370
|
+
is_auto_snapshot # True until first UI edit; sticky after
|
|
371
|
+
unique constraint (field, user) for non-NULL users
|
|
372
|
+
unique constraint (field) WHERE user IS NULL for system-wide
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## Limitations & gotchas
|
|
376
|
+
|
|
377
|
+
- Forms identified by fully qualified Python path. Renaming or moving a Form
|
|
378
|
+
class invalidates the saved defaults.
|
|
379
|
+
- Only fields whose `initial` is JSON-serialisable get a stored default.
|
|
380
|
+
Lambdas / callables on `initial` keep working at the form level but aren't
|
|
381
|
+
persisted.
|
|
382
|
+
- `FormFieldDefaultValue.clean()` re-instantiates the form to validate typed
|
|
383
|
+
values. Forms with heavy `__init__` cost slow down admin saves.
|
|
384
|
+
- The popup is opt-in: it only works if you include `formdefaults.urls`,
|
|
385
|
+
load the template tag, and serve the static JS/CSS.
|
|
386
|
+
- `is_auto_snapshot` is set to True heuristically for pre-0.3.0 rows
|
|
387
|
+
during the data migration: rows whose value matches the form's
|
|
388
|
+
current `initial` become `True` (the row looks untouched), the rest
|
|
389
|
+
become `False`. False positives happen rarely (someone deliberately
|
|
390
|
+
edited the value to match the code default and the data migration
|
|
391
|
+
can't tell). False positives become real on the next code change —
|
|
392
|
+
the value is treated as auto-snapshot and refreshed.
|
|
393
|
+
|
|
394
|
+
## Running the tests
|
|
395
|
+
|
|
396
|
+
```bash
|
|
397
|
+
pip install -e ".[test]"
|
|
398
|
+
pytest
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
Tests run against PostgreSQL via [testcontainers](https://testcontainers-python.readthedocs.io/) — Docker is required on the test machine.
|
|
402
|
+
|
|
403
|
+
## License
|
|
404
|
+
|
|
405
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
formdefaults/__init__.py,sha256=hsgdJTFXugnrvLXXAwch1riZ735lEep-lWdsbtL3enE,77
|
|
2
|
+
formdefaults/_autosnap_backfill.py,sha256=-RsPiEQdr6Tj1322xKRLL3CoK2yAX16oJsduZZcDYWc,882
|
|
3
|
+
formdefaults/admin.py,sha256=ORVxfuNOklP3GXMVpAvD_l6pI0-OS97BKcCU__ArE54,1952
|
|
4
|
+
formdefaults/apps.py,sha256=xNIXu1WOi0D2w6Kjc25acXu3IvicfEe9TeuQImlC10I,605
|
|
5
|
+
formdefaults/core.py,sha256=ymELDoCCQgtVhGDS6L3m4PlGZdMW9LNa7HqZEeDsGSQ,5015
|
|
6
|
+
formdefaults/forms.py,sha256=gITUB25Jnkw0fmAo96xqK4inMPB_ydFMiza0pxVDt9k,4474
|
|
7
|
+
formdefaults/helpers.py,sha256=JGMkQOh4m2UdiICFMwXhTVQdvH8VTEVWs2VmohnqwOw,700
|
|
8
|
+
formdefaults/models.py,sha256=0AxDtP2E-kNkByd-LYH5tDcRtDzh5o2M6xqir9nU8wA,7227
|
|
9
|
+
formdefaults/permissions.py,sha256=R7j7Yhe0uHalEW6PeFHG3t6QScfyZ7KAGo2lCQQ0zU8,1791
|
|
10
|
+
formdefaults/registry.py,sha256=Gyom1oNHmBbRk4pEJ4QOy_NEqp9ZpSQ-goLRlxS_F2I,1686
|
|
11
|
+
formdefaults/signals.py,sha256=lyWKuwAScur8UcL5WrMeAIqiVCRN3gNtgMBTly1jY08,1926
|
|
12
|
+
formdefaults/urls.py,sha256=PfXFJEnNnhnbpAU29ccROZlPilTgn6vQIgL0VuCPQ_Q,407
|
|
13
|
+
formdefaults/util.py,sha256=6b5nb_jVcC69yV48_Qg7z1UqF2Nlw9OjX6etySLDIe0,479
|
|
14
|
+
formdefaults/views.py,sha256=eiyAxA_TrK5UuAHEEARKRSHhaYzv45iA0v1-kc-x9B0,3849
|
|
15
|
+
formdefaults/locale/pl/LC_MESSAGES/django.mo,sha256=Dd8zzAoWIQc8Fy724La-7roVYNiq3s0vUK4jY9Czy4k,1784
|
|
16
|
+
formdefaults/locale/pl/LC_MESSAGES/django.po,sha256=8zhEZfiDZHuX4DDYWn5KKe77-4V_194kaV5rF9Y-V68,1829
|
|
17
|
+
formdefaults/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
+
formdefaults/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
19
|
+
formdefaults/management/commands/dedupe_formdefaults.py,sha256=ZmozzzkkwNYmaRSq7ldRX7uJzm_9gs-hZxNLV9D-kB4,1130
|
|
20
|
+
formdefaults/migrations/0001_initial.py,sha256=kJia_MFQP4v1OC3TVI8I1yeZfWzxbRL6Fo2MdrMExys,5296
|
|
21
|
+
formdefaults/migrations/0002_django32.py,sha256=JA2kg220EZSecptwCIjQ-6mxb54jhukAFl4q1FJN-CE,428
|
|
22
|
+
formdefaults/migrations/0003_formrepresentation_pre_registered.py,sha256=79Owt73bX3nLbbG2kxLFnYQLIb6o0_3uRFNowRDoy9g,641
|
|
23
|
+
formdefaults/migrations/0004_unique_field_user.py,sha256=GWJHl8lgyZNjWkif8nm8Gfp4AWTcCnqptKzotf8D__k,752
|
|
24
|
+
formdefaults/migrations/0005_unique_field_system.py,sha256=xCd5iN2-yfJSAzoZP92nXO4IBv_Eh_FKeN1PwLPjR98,634
|
|
25
|
+
formdefaults/migrations/0006_formfielddefaultvalue_is_auto_snapshot.py,sha256=zwBzHogCcADnNZemX9-UEyIuXol-xg9_UVlUA01sNGw,775
|
|
26
|
+
formdefaults/migrations/0007_backfill_is_auto_snapshot.py,sha256=zfDFOOu6QujuWdpet4zjatb7k_9pfffeOI8PaMSCwYA,1186
|
|
27
|
+
formdefaults/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
28
|
+
formdefaults/static/formdefaults/modal.css,sha256=i56AMiVQ8MsQhh9HpqpNQbSLDv6dWK7VCT7EfBNOst8,2135
|
|
29
|
+
formdefaults/static/formdefaults/modal.js,sha256=b-g0JwirB4kCILyJ3ptU1V3hzhrCNmqLhSt9itH3akI,2667
|
|
30
|
+
formdefaults/templates/formdefaults/_button.html,sha256=SVLWHrfWmmNSRbzw4RoWqLItf5Xwir0pk3UOaCvDZQU,487
|
|
31
|
+
formdefaults/templates/formdefaults/_modal_fragment.html,sha256=ummaQsq3FWKbAlbEseVKOLKBYRhcxxHpaVVxB478NT8,1258
|
|
32
|
+
formdefaults/templates/formdefaults/_user_edit_form.html,sha256=lMPxUuTTnnvtHefBWc2gv19kqNInME41MC1wdTYw94U,424
|
|
33
|
+
formdefaults/templatetags/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
34
|
+
formdefaults/templatetags/formdefaults.py,sha256=7P43cPPPABw6f_yvGL1E5vRIXdIi4eTHkH_wuoo6Yvo,855
|
|
35
|
+
django_formdefaults-0.5.0.dist-info/METADATA,sha256=DQckTRn1tKFE4ddYF2sht0_zNXfmQwHs8fmUOPx4g-w,15008
|
|
36
|
+
django_formdefaults-0.5.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
37
|
+
django_formdefaults-0.5.0.dist-info/licenses/LICENSE,sha256=Mv1-9gzkGEKL3DkZ8PW9Z5iuQ10sjux60_yx6VgKdhk,1091
|
|
38
|
+
django_formdefaults-0.5.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Michał Pasternak and contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
formdefaults/__init__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Helper used by the 0007 data migration. Lives outside `migrations/`
|
|
2
|
+
so tests can import it directly."""
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def resolve_initial(form_full_name, field_name):
|
|
8
|
+
"""Best-effort lookup of `Form.initial` for a given (full_name,
|
|
9
|
+
field_name). Returns `(found: bool, initial)`. `found=False` means we
|
|
10
|
+
can't decide and the caller should treat the row as sticky."""
|
|
11
|
+
from formdefaults.util import get_python_class_by_name
|
|
12
|
+
|
|
13
|
+
try:
|
|
14
|
+
form_class = get_python_class_by_name(form_full_name)
|
|
15
|
+
except Exception:
|
|
16
|
+
return False, None
|
|
17
|
+
try:
|
|
18
|
+
instance = form_class()
|
|
19
|
+
except Exception:
|
|
20
|
+
return False, None
|
|
21
|
+
field = instance.fields.get(field_name)
|
|
22
|
+
if field is None:
|
|
23
|
+
return False, None
|
|
24
|
+
initial = field.initial
|
|
25
|
+
try:
|
|
26
|
+
json.dumps(initial)
|
|
27
|
+
except TypeError:
|
|
28
|
+
return False, None
|
|
29
|
+
return True, initial
|
formdefaults/admin.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from django import forms
|
|
2
|
+
from django.contrib import admin
|
|
3
|
+
from django.contrib.auth import get_user_model
|
|
4
|
+
from django.db.models import JSONField, TextField
|
|
5
|
+
|
|
6
|
+
from formdefaults.models import (
|
|
7
|
+
FormFieldDefaultValue,
|
|
8
|
+
FormFieldRepresentation,
|
|
9
|
+
FormRepresentation,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
WideTextInput = {"widget": forms.TextInput(attrs={"style": "width: 100%"})}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class FormFieldRepresentationInline(admin.TabularInline):
|
|
16
|
+
model = FormFieldRepresentation
|
|
17
|
+
fields = ["name", "label"]
|
|
18
|
+
readonly_fields = ["name", "label"]
|
|
19
|
+
extra = 0
|
|
20
|
+
can_delete = False
|
|
21
|
+
show_change_link = False
|
|
22
|
+
|
|
23
|
+
def has_add_permission(self, request, obj):
|
|
24
|
+
return False
|
|
25
|
+
|
|
26
|
+
formfield_overrides = {
|
|
27
|
+
TextField: WideTextInput,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class FormFieldDefaultValueForm(forms.ModelForm):
|
|
32
|
+
user = forms.ModelChoiceField(
|
|
33
|
+
queryset=get_user_model().objects.all(), empty_label="każdy", required=False
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
class Meta:
|
|
37
|
+
fields = ["user", "field", "value"]
|
|
38
|
+
model = FormFieldDefaultValue
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class FormFieldDefaultValueInline(admin.TabularInline):
|
|
42
|
+
model = FormFieldDefaultValue
|
|
43
|
+
form = FormFieldDefaultValueForm
|
|
44
|
+
formfield_overrides = {
|
|
45
|
+
JSONField: WideTextInput,
|
|
46
|
+
}
|
|
47
|
+
extra = 0
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@admin.register(FormRepresentation)
|
|
51
|
+
class FormRepresentationAdmin(admin.ModelAdmin):
|
|
52
|
+
list_display = ["label", "full_name", "pre_registered"]
|
|
53
|
+
list_filter = ["pre_registered"]
|
|
54
|
+
inlines = [FormFieldDefaultValueInline]
|
|
55
|
+
readonly_fields = ["full_name", "pre_registered"]
|
|
56
|
+
fields = ["label", "full_name", "pre_registered", "html_before", "html_after"]
|
|
57
|
+
|
|
58
|
+
def save_formset(self, request, form, formset, change):
|
|
59
|
+
instances = formset.save(commit=False)
|
|
60
|
+
for instance in instances:
|
|
61
|
+
if isinstance(instance, FormFieldDefaultValue):
|
|
62
|
+
instance.is_auto_snapshot = False
|
|
63
|
+
instance.save()
|
|
64
|
+
for obj in formset.deleted_objects:
|
|
65
|
+
obj.delete()
|
|
66
|
+
formset.save_m2m()
|
formdefaults/apps.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class FormdefaultsConfig(AppConfig):
|
|
5
|
+
name = "formdefaults"
|
|
6
|
+
verbose_name = "Formularze - wartości domyślne"
|
|
7
|
+
# Pinned to AutoField to match the historical migration files:
|
|
8
|
+
# 0001 was generated with Django 3.0's then-default AutoField, and
|
|
9
|
+
# bumping to BigAutoField would surface as a spurious migration on
|
|
10
|
+
# every project that already had this app installed pre-extraction.
|
|
11
|
+
default_auto_field = "django.db.models.AutoField"
|
|
12
|
+
|
|
13
|
+
def ready(self):
|
|
14
|
+
# Connect post_migrate handler.
|
|
15
|
+
from formdefaults import signals # noqa: F401
|