django-htmx-cbv 0.0.1.dev1__tar.gz → 0.0.1.dev2__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.
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/PKG-INFO +19 -25
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/README.md +14 -16
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/django_htmx_cbv/apps.py +2 -2
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/django_htmx_cbv/checks.py +6 -6
- django_htmx_cbv-0.0.1.dev2/django_htmx_cbv/forms.py +38 -0
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/django_htmx_cbv/middleware.py +32 -33
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/django_htmx_cbv/views.py +141 -74
- django_htmx_cbv-0.0.1.dev2/pyproject.toml +104 -0
- django_htmx_cbv-0.0.1.dev2/pyproject.toml.orig +101 -0
- django_htmx_cbv-0.0.1.dev1/LICENSE +0 -21
- django_htmx_cbv-0.0.1.dev1/django_htmx_cbv/future.py +0 -107
- django_htmx_cbv-0.0.1.dev1/pyproject.toml +0 -60
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/django_htmx_cbv/__init__.py +0 -0
- {django_htmx_cbv-0.0.1.dev1 → django_htmx_cbv-0.0.1.dev2}/django_htmx_cbv/py.typed +0 -0
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: django-htmx-cbv
|
|
3
|
-
Version: 0.0.1.
|
|
3
|
+
Version: 0.0.1.dev2
|
|
4
4
|
Summary: Django class-based views, tailored for htmx
|
|
5
|
-
License-Expression: MIT
|
|
6
|
-
License-File: LICENSE
|
|
7
5
|
Author: Geoffrey Eisenbarth
|
|
8
|
-
Author-email: geoffrey.eisenbarth@gmail.com
|
|
6
|
+
Author-email: Geoffrey Eisenbarth <geoffrey.eisenbarth@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Requires-Dist: django>=6.0.0,<7.0.0
|
|
9
|
+
Requires-Dist: django-htmx>=1.27.0,<2.0.0
|
|
9
10
|
Requires-Python: >=3.14
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.14
|
|
12
|
-
Requires-Dist: django (>=6.0.0,<7.0.0)
|
|
13
|
-
Requires-Dist: django-htmx (>=1.27.0,<2.0.0)
|
|
14
11
|
Description-Content-Type: text/markdown
|
|
15
12
|
|
|
16
13
|
# Django Class Based Views for HTMX
|
|
@@ -48,8 +45,8 @@ Add the app to `INSTALLED_APPS`:
|
|
|
48
45
|
|
|
49
46
|
```py
|
|
50
47
|
INSTALLED_APPS = [
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
# ...
|
|
49
|
+
"django_htmx_cbv",
|
|
53
50
|
]
|
|
54
51
|
```
|
|
55
52
|
|
|
@@ -57,15 +54,13 @@ Add middleware in this order:
|
|
|
57
54
|
|
|
58
55
|
```py
|
|
59
56
|
MIDDLEWARE = [
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
# Other middleware...
|
|
57
|
+
# Django middleware...
|
|
58
|
+
"django_htmx.middleware.HtmxMiddleware",
|
|
59
|
+
"django_htmx_cbv.middleware.HtmxPartialTemplateMiddleware",
|
|
60
|
+
"django_htmx_cbv.middleware.HtmxVaryMiddleware",
|
|
61
|
+
"django_htmx_cbv.middleware.HtmxMessageMiddleware",
|
|
62
|
+
"django_htmx_cbv.middleware.HttpVerbViewMiddleware",
|
|
63
|
+
# Other middleware...
|
|
69
64
|
]
|
|
70
65
|
```
|
|
71
66
|
|
|
@@ -148,11 +143,11 @@ Use `HyperFormView` or `ModelFormView` when you want a full HTMX-aware form view
|
|
|
148
143
|
|
|
149
144
|
```py
|
|
150
145
|
from django_htmx_cbv.views import (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
146
|
+
HtmxFormMixin,
|
|
147
|
+
HtmxTemplateResponseMixin,
|
|
148
|
+
HyperFormView,
|
|
149
|
+
ModelFormView,
|
|
150
|
+
ProcessHyperFormView,
|
|
156
151
|
)
|
|
157
152
|
```
|
|
158
153
|
|
|
@@ -162,4 +157,3 @@ This package is still evolving.
|
|
|
162
157
|
The current API is centered on reusable HTMX behavior for Django CBVs, with explicit middleware and template conventions.
|
|
163
158
|
I welcome input from the community on the API design and implementation;
|
|
164
159
|
I want this to be something we can all use!
|
|
165
|
-
|
|
@@ -33,8 +33,8 @@ Add the app to `INSTALLED_APPS`:
|
|
|
33
33
|
|
|
34
34
|
```py
|
|
35
35
|
INSTALLED_APPS = [
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
# ...
|
|
37
|
+
"django_htmx_cbv",
|
|
38
38
|
]
|
|
39
39
|
```
|
|
40
40
|
|
|
@@ -42,15 +42,13 @@ Add middleware in this order:
|
|
|
42
42
|
|
|
43
43
|
```py
|
|
44
44
|
MIDDLEWARE = [
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
# Other middleware...
|
|
45
|
+
# Django middleware...
|
|
46
|
+
"django_htmx.middleware.HtmxMiddleware",
|
|
47
|
+
"django_htmx_cbv.middleware.HtmxPartialTemplateMiddleware",
|
|
48
|
+
"django_htmx_cbv.middleware.HtmxVaryMiddleware",
|
|
49
|
+
"django_htmx_cbv.middleware.HtmxMessageMiddleware",
|
|
50
|
+
"django_htmx_cbv.middleware.HttpVerbViewMiddleware",
|
|
51
|
+
# Other middleware...
|
|
54
52
|
]
|
|
55
53
|
```
|
|
56
54
|
|
|
@@ -133,11 +131,11 @@ Use `HyperFormView` or `ModelFormView` when you want a full HTMX-aware form view
|
|
|
133
131
|
|
|
134
132
|
```py
|
|
135
133
|
from django_htmx_cbv.views import (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
134
|
+
HtmxFormMixin,
|
|
135
|
+
HtmxTemplateResponseMixin,
|
|
136
|
+
HyperFormView,
|
|
137
|
+
ModelFormView,
|
|
138
|
+
ProcessHyperFormView,
|
|
141
139
|
)
|
|
142
140
|
```
|
|
143
141
|
|
|
@@ -2,8 +2,8 @@ from django.apps import AppConfig
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class DjangoHtmxCbvConfig(AppConfig):
|
|
5
|
-
default_auto_field =
|
|
6
|
-
name =
|
|
5
|
+
default_auto_field = "django.db.models.BigAutoField"
|
|
6
|
+
name = "django_htmx_cbv"
|
|
7
7
|
|
|
8
8
|
def ready(self) -> None:
|
|
9
9
|
from . import checks # noqa: F401
|
|
@@ -3,16 +3,16 @@ from typing import TYPE_CHECKING
|
|
|
3
3
|
from django.conf import settings
|
|
4
4
|
from django.core.checks import Error, register
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
if TYPE_CHECKING:
|
|
8
7
|
from collections.abc import Iterable, Sequence
|
|
9
8
|
from typing import Any
|
|
9
|
+
|
|
10
10
|
from django.apps import AppConfig
|
|
11
11
|
from django.core.checks import CheckMessage
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
HTMX_MIDDLEWARE =
|
|
15
|
-
CBV_MIDDLEWARE_PREFIX =
|
|
14
|
+
HTMX_MIDDLEWARE = "django_htmx.middleware.HtmxMiddleware"
|
|
15
|
+
CBV_MIDDLEWARE_PREFIX = "django_htmx_cbv.middleware."
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
@register()
|
|
@@ -22,7 +22,7 @@ def check_htmx_middleware_order(
|
|
|
22
22
|
databases: Sequence[str] | None = None,
|
|
23
23
|
**kwargs: Any,
|
|
24
24
|
) -> Iterable[CheckMessage]:
|
|
25
|
-
middleware = list(getattr(settings,
|
|
25
|
+
middleware = list(getattr(settings, "MIDDLEWARE", []))
|
|
26
26
|
cbv_indices = [
|
|
27
27
|
index
|
|
28
28
|
for index, path in enumerate(middleware)
|
|
@@ -39,7 +39,7 @@ def check_htmx_middleware_order(
|
|
|
39
39
|
"Add 'django_htmx.middleware.HtmxMiddleware' before any "
|
|
40
40
|
"'django_htmx_cbv.middleware.*' entries in MIDDLEWARE."
|
|
41
41
|
),
|
|
42
|
-
id=
|
|
42
|
+
id="django_htmx_cbv.E001",
|
|
43
43
|
)
|
|
44
44
|
]
|
|
45
45
|
|
|
@@ -52,7 +52,7 @@ def check_htmx_middleware_order(
|
|
|
52
52
|
"Move 'django_htmx.middleware.HtmxMiddleware' above all "
|
|
53
53
|
"'django_htmx_cbv.middleware.*' entries in MIDDLEWARE."
|
|
54
54
|
),
|
|
55
|
-
id=
|
|
55
|
+
id="django_htmx_cbv.E002",
|
|
56
56
|
)
|
|
57
57
|
]
|
|
58
58
|
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING
|
|
2
|
+
|
|
3
|
+
from django import forms
|
|
4
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
5
|
+
from django.db.models import QuerySet
|
|
6
|
+
from django.utils.translation import gettext_lazy as _
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from collections.abc import Iterable
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
Choices = Iterable[tuple[str, str]] | QuerySet[Any]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class HtmxChainedForm(forms.Form):
|
|
16
|
+
"""Custom form subclass to work with HtmxChainedFormView."""
|
|
17
|
+
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
field_choices: dict[str, Choices] | None = None,
|
|
21
|
+
*args: Any,
|
|
22
|
+
**kwargs: Any,
|
|
23
|
+
) -> None:
|
|
24
|
+
super().__init__(*args, **kwargs)
|
|
25
|
+
if field_choices is not None:
|
|
26
|
+
for field_name, choices in field_choices.items():
|
|
27
|
+
field = self.fields[field_name]
|
|
28
|
+
if isinstance(field, forms.ModelChoiceField):
|
|
29
|
+
if isinstance(choices, QuerySet):
|
|
30
|
+
field.queryset = choices
|
|
31
|
+
else:
|
|
32
|
+
field.choices = choices
|
|
33
|
+
elif isinstance(field, forms.ChoiceField):
|
|
34
|
+
field.choices = choices
|
|
35
|
+
else:
|
|
36
|
+
raise ImproperlyConfigured(
|
|
37
|
+
_(f"Field '{field_name}' does not support choices.")
|
|
38
|
+
)
|
|
@@ -5,33 +5,36 @@ from django.contrib.messages import get_messages
|
|
|
5
5
|
from django.core.exceptions import ImproperlyConfigured
|
|
6
6
|
from django.http import HttpResponse, QueryDict
|
|
7
7
|
from django.http.multipartparser import MultiPartParserError
|
|
8
|
-
from django.template.loader import render_to_string
|
|
9
8
|
from django.http.response import HttpResponseRedirectBase
|
|
9
|
+
from django.template.loader import render_to_string
|
|
10
10
|
from django.utils.cache import patch_vary_headers
|
|
11
|
-
|
|
12
11
|
from django_htmx.http import HttpResponseClientRedirect
|
|
13
12
|
from django_htmx.middleware import HtmxDetails
|
|
14
13
|
|
|
15
|
-
|
|
16
14
|
if TYPE_CHECKING:
|
|
17
|
-
from
|
|
15
|
+
from collections.abc import Callable, Sequence
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from django.core.files.uploadedfile import UploadedFile
|
|
18
19
|
from django.http import HttpRequest
|
|
19
20
|
from django.template.response import SimpleTemplateResponse
|
|
21
|
+
from django.utils.datastructures import MultiValueDict
|
|
20
22
|
|
|
21
|
-
class HtmxRequest(
|
|
23
|
+
class HtmxRequest(HttpRequest):
|
|
22
24
|
htmx: HtmxDetails
|
|
23
25
|
QUERY: QueryDict
|
|
24
26
|
BODY: QueryDict
|
|
27
|
+
FILES: MultiValueDict[str, UploadedFile[Any]]
|
|
25
28
|
|
|
26
29
|
|
|
27
|
-
def _get_htmx_request(request:
|
|
28
|
-
if not isinstance(getattr(request,
|
|
30
|
+
def _get_htmx_request(request: HttpRequest) -> HtmxRequest:
|
|
31
|
+
if not isinstance(getattr(request, "htmx", None), HtmxDetails):
|
|
29
32
|
message = (
|
|
30
33
|
"django_htmx.middleware.HtmxMiddleware must be installed and run "
|
|
31
34
|
"before django_htmx_cbv middleware."
|
|
32
35
|
)
|
|
33
36
|
raise ImproperlyConfigured(message)
|
|
34
|
-
return cast(
|
|
37
|
+
return cast("HtmxRequest", request)
|
|
35
38
|
|
|
36
39
|
|
|
37
40
|
# TODO: PUSH: Make sure CSRF Protection is enabled for hx-put, patch, delete
|
|
@@ -56,7 +59,7 @@ class HtmxVaryMiddleware:
|
|
|
56
59
|
response = self.get_response(request)
|
|
57
60
|
htmx_request = _get_htmx_request(request)
|
|
58
61
|
if htmx_request.htmx:
|
|
59
|
-
patch_vary_headers(response, [
|
|
62
|
+
patch_vary_headers(response, ["HX-Request"])
|
|
60
63
|
return response
|
|
61
64
|
|
|
62
65
|
|
|
@@ -93,26 +96,25 @@ class HttpVerbViewMiddleware:
|
|
|
93
96
|
htmx_request.QUERY = request.GET.copy()
|
|
94
97
|
htmx_request.BODY = QueryDict()
|
|
95
98
|
|
|
96
|
-
if request.method in [
|
|
99
|
+
if request.method in ["GET", "DELETE"]:
|
|
97
100
|
pass
|
|
98
|
-
elif request.method ==
|
|
101
|
+
elif request.method == "POST":
|
|
99
102
|
htmx_request.BODY = request.POST.copy()
|
|
100
|
-
elif request.method in [
|
|
101
|
-
if request.content_type ==
|
|
103
|
+
elif request.method in ["PUT", "PATCH"]:
|
|
104
|
+
if request.content_type == "application/x-www-form-urlencoded":
|
|
102
105
|
htmx_request.BODY = QueryDict(request.body)
|
|
103
|
-
elif request.content_type ==
|
|
106
|
+
elif request.content_type == "multipart/form-data":
|
|
104
107
|
try:
|
|
105
108
|
body, files = request.parse_file_upload(request.META, request)
|
|
106
109
|
except MultiPartParserError as exc:
|
|
107
110
|
raise ImproperlyConfigured(str(exc)) from exc
|
|
108
111
|
htmx_request.BODY = body.copy()
|
|
109
|
-
cast(
|
|
110
|
-
cast(
|
|
112
|
+
cast("Any", request)._post = body
|
|
113
|
+
cast("Any", request)._files = files
|
|
111
114
|
|
|
112
115
|
if not htmx_request.htmx:
|
|
113
116
|
if _method := (
|
|
114
|
-
htmx_request.BODY.get(
|
|
115
|
-
or htmx_request.QUERY.get('_method')
|
|
117
|
+
htmx_request.BODY.get("_method") or htmx_request.QUERY.get("_method")
|
|
116
118
|
):
|
|
117
119
|
request.method = _method.upper()
|
|
118
120
|
|
|
@@ -122,8 +124,8 @@ class HtmxPartialTemplateMiddleware:
|
|
|
122
124
|
|
|
123
125
|
default_partial_name = getattr(
|
|
124
126
|
settings,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
+
"DEFAULT_PARTIAL_NAME",
|
|
128
|
+
"main",
|
|
127
129
|
)
|
|
128
130
|
|
|
129
131
|
def __init__(
|
|
@@ -146,9 +148,9 @@ class HtmxPartialTemplateMiddleware:
|
|
|
146
148
|
) -> SimpleTemplateResponse:
|
|
147
149
|
htmx_request = _get_htmx_request(request)
|
|
148
150
|
if htmx_request.htmx:
|
|
149
|
-
if retarget_id := response.get(
|
|
150
|
-
partial_name = retarget_id.strip(
|
|
151
|
-
elif partial_name := response.get(
|
|
151
|
+
if retarget_id := response.get("HX-Retarget"):
|
|
152
|
+
partial_name = retarget_id.strip("#")
|
|
153
|
+
elif partial_name := response.get("HX-Partial-Name", ""):
|
|
152
154
|
# Allow the view to specify the partial name
|
|
153
155
|
pass
|
|
154
156
|
else:
|
|
@@ -160,13 +162,10 @@ class HtmxPartialTemplateMiddleware:
|
|
|
160
162
|
if isinstance(template_name, str):
|
|
161
163
|
template_names = [template_name]
|
|
162
164
|
else:
|
|
163
|
-
template_names = list(cast(
|
|
165
|
+
template_names = list(cast("Sequence[str]", template_name))
|
|
164
166
|
|
|
165
167
|
response.template_name = [
|
|
166
|
-
f
|
|
167
|
-
if '#' not in name
|
|
168
|
-
else name
|
|
169
|
-
for name in template_names
|
|
168
|
+
f"{name}#{partial_name}" if "#" not in name else name for name in template_names
|
|
170
169
|
]
|
|
171
170
|
return response
|
|
172
171
|
|
|
@@ -196,8 +195,8 @@ class HtmxMessageMiddleware:
|
|
|
196
195
|
|
|
197
196
|
messages_template = getattr(
|
|
198
197
|
settings,
|
|
199
|
-
|
|
200
|
-
|
|
198
|
+
"MESSAGES_TEMPLATE_NAME",
|
|
199
|
+
"messages.html",
|
|
201
200
|
)
|
|
202
201
|
|
|
203
202
|
def __init__(
|
|
@@ -210,13 +209,13 @@ class HtmxMessageMiddleware:
|
|
|
210
209
|
|
|
211
210
|
response = self.get_response(request)
|
|
212
211
|
|
|
213
|
-
if
|
|
212
|
+
if "HX-Request" not in request.headers:
|
|
214
213
|
# Not an HTMX request, so full page rendering will pick up the message
|
|
215
214
|
return response
|
|
216
215
|
elif 300 <= response.status_code < 400:
|
|
217
216
|
# Ignore redirections because HTMX cannot read the body
|
|
218
217
|
return response
|
|
219
|
-
elif
|
|
218
|
+
elif "HX-Redirect" in response.headers:
|
|
220
219
|
# Ignore client-side redirection because HTMX drops OOB swabs
|
|
221
220
|
return response
|
|
222
221
|
|
|
@@ -224,7 +223,7 @@ class HtmxMessageMiddleware:
|
|
|
224
223
|
if messages := get_messages(request):
|
|
225
224
|
html = render_to_string(
|
|
226
225
|
template_name=self.messages_template,
|
|
227
|
-
context={
|
|
226
|
+
context={"messages": messages},
|
|
228
227
|
request=request,
|
|
229
228
|
)
|
|
230
229
|
if response.status_code == 204:
|
|
@@ -3,22 +3,28 @@ from typing import TYPE_CHECKING, cast
|
|
|
3
3
|
from django.contrib import messages
|
|
4
4
|
from django.contrib.messages.views import SuccessMessageMixin
|
|
5
5
|
from django.core.exceptions import (
|
|
6
|
-
|
|
6
|
+
BadRequest,
|
|
7
|
+
ImproperlyConfigured,
|
|
8
|
+
PermissionDenied,
|
|
7
9
|
)
|
|
10
|
+
from django.forms import ChoiceField
|
|
8
11
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
12
|
+
from django.utils.translation import gettext_lazy as _
|
|
9
13
|
from django.views.generic.base import TemplateResponseMixin, View
|
|
10
14
|
from django.views.generic.detail import SingleObjectMixin
|
|
11
15
|
from django.views.generic.edit import FormMixin, ModelFormMixin
|
|
12
16
|
|
|
13
|
-
|
|
14
17
|
if TYPE_CHECKING:
|
|
15
|
-
from collections.abc import Mapping
|
|
16
|
-
from typing import Any, Protocol
|
|
18
|
+
from collections.abc import Iterable, Mapping, Sequence
|
|
19
|
+
from typing import Any, Protocol
|
|
20
|
+
|
|
17
21
|
from django import forms
|
|
18
|
-
from django.db.models import Model
|
|
22
|
+
from django.db.models import Model, QuerySet
|
|
19
23
|
from django.http import HttpRequest, QueryDict
|
|
20
24
|
from django_htmx.middleware import HtmxDetails
|
|
21
25
|
|
|
26
|
+
Choices = Iterable[tuple[str, str]] | QuerySet[Any]
|
|
27
|
+
|
|
22
28
|
class HtmxRequest(Protocol):
|
|
23
29
|
htmx: HtmxDetails
|
|
24
30
|
method: str
|
|
@@ -26,15 +32,14 @@ if TYPE_CHECKING:
|
|
|
26
32
|
QUERY: QueryDict
|
|
27
33
|
BODY: QueryDict
|
|
28
34
|
|
|
29
|
-
class FormClassWithBaseFields[
|
|
35
|
+
class FormClassWithBaseFields[F: forms.BaseForm](Protocol):
|
|
30
36
|
base_fields: Mapping[str, object]
|
|
31
37
|
|
|
32
38
|
def __call__(
|
|
33
39
|
self,
|
|
34
40
|
*args: Any,
|
|
35
41
|
**kwargs: Any,
|
|
36
|
-
) ->
|
|
37
|
-
...
|
|
42
|
+
) -> F: ...
|
|
38
43
|
|
|
39
44
|
class SupportsSetup(Protocol):
|
|
40
45
|
def setup(
|
|
@@ -42,12 +47,10 @@ if TYPE_CHECKING:
|
|
|
42
47
|
request: HttpRequest,
|
|
43
48
|
*args: Any,
|
|
44
49
|
**kwargs: Any,
|
|
45
|
-
) -> None:
|
|
46
|
-
...
|
|
50
|
+
) -> None: ...
|
|
47
51
|
|
|
48
52
|
class SupportsContextData(Protocol):
|
|
49
|
-
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
|
|
50
|
-
...
|
|
53
|
+
def get_context_data(self, **kwargs: Any) -> dict[str, Any]: ...
|
|
51
54
|
|
|
52
55
|
|
|
53
56
|
class HtmxTemplateResponseMixin(TemplateResponseMixin):
|
|
@@ -56,19 +59,19 @@ class HtmxTemplateResponseMixin(TemplateResponseMixin):
|
|
|
56
59
|
def _finalize_htmx_response(
|
|
57
60
|
self,
|
|
58
61
|
response: HttpResponse,
|
|
59
|
-
**context:
|
|
62
|
+
**context: Any,
|
|
60
63
|
) -> HttpResponse:
|
|
61
|
-
request = cast(
|
|
64
|
+
request = cast("HtmxRequest", self.request)
|
|
62
65
|
if request.htmx:
|
|
63
|
-
context_mixin = cast(
|
|
66
|
+
context_mixin = cast("SupportsContextData", self)
|
|
64
67
|
rendered_context = context_mixin.get_context_data(**context)
|
|
65
68
|
response = self.render_to_response(rendered_context)
|
|
66
69
|
return response
|
|
67
70
|
|
|
68
71
|
|
|
69
|
-
class HtmxFormMixin[
|
|
72
|
+
class HtmxFormMixin[F: forms.BaseForm](
|
|
70
73
|
HtmxTemplateResponseMixin,
|
|
71
|
-
FormMixin[
|
|
74
|
+
FormMixin[F],
|
|
72
75
|
):
|
|
73
76
|
"""Mixin for facilitating HTMX submission of forms.
|
|
74
77
|
|
|
@@ -78,7 +81,7 @@ class HtmxFormMixin[_F: forms.BaseForm](
|
|
|
78
81
|
|
|
79
82
|
"""
|
|
80
83
|
|
|
81
|
-
def form_valid(self, form:
|
|
84
|
+
def form_valid(self, form: F) -> HttpResponse:
|
|
82
85
|
"""The POST/Redirect/GET pattern is not necessary with HTMX.
|
|
83
86
|
|
|
84
87
|
Notes
|
|
@@ -90,10 +93,10 @@ class HtmxFormMixin[_F: forms.BaseForm](
|
|
|
90
93
|
response = super().form_valid(form)
|
|
91
94
|
return self._finalize_htmx_response(response, form=form)
|
|
92
95
|
|
|
93
|
-
def form_invalid(self, form:
|
|
96
|
+
def form_invalid(self, form: F) -> HttpResponse:
|
|
94
97
|
"""Retarget form errors."""
|
|
95
98
|
response = super().form_invalid(form)
|
|
96
|
-
request = cast(
|
|
99
|
+
request = cast("HtmxRequest", self.request)
|
|
97
100
|
|
|
98
101
|
if request.htmx:
|
|
99
102
|
if request.htmx.trigger is None:
|
|
@@ -102,32 +105,34 @@ class HtmxFormMixin[_F: forms.BaseForm](
|
|
|
102
105
|
"with an HTML id and matching partial block name."
|
|
103
106
|
)
|
|
104
107
|
raise ImproperlyConfigured(message)
|
|
105
|
-
response[
|
|
106
|
-
response[
|
|
108
|
+
response["HX-Retarget"] = f"#{request.htmx.trigger}"
|
|
109
|
+
response["HX-Reswap"] = "outerHTML"
|
|
107
110
|
return response
|
|
108
111
|
|
|
109
112
|
|
|
110
|
-
class HyperFormMixin[
|
|
113
|
+
class HyperFormMixin[F: forms.BaseForm](HtmxFormMixin[F]):
|
|
111
114
|
"""Add support for all HTTP Verbs to Django's FormMixin."""
|
|
112
115
|
|
|
113
|
-
http_method_names: Sequence[str] = (
|
|
116
|
+
http_method_names: Sequence[str] = ("get", "post", "put", "patch", "delete")
|
|
114
117
|
|
|
115
118
|
def get_initial(self) -> dict[str, Any]:
|
|
116
119
|
initial = super().get_initial()
|
|
117
120
|
|
|
118
121
|
assert self.form_class is not None
|
|
119
|
-
request = cast(
|
|
120
|
-
form_class = cast(
|
|
122
|
+
request = cast("HtmxRequest", self.request)
|
|
123
|
+
form_class = cast("FormClassWithBaseFields[F]", self.form_class)
|
|
121
124
|
|
|
122
|
-
form_method = getattr(form_class,
|
|
125
|
+
form_method = getattr(form_class, "method", "").upper()
|
|
123
126
|
# TODO: Or if self.http_method_names != ['get']?
|
|
124
|
-
if form_method !=
|
|
127
|
+
if form_method != "GET" and request.QUERY:
|
|
125
128
|
# Pass relevant URL params to the form as initial values
|
|
126
|
-
initial.update(
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
initial.update(
|
|
130
|
+
{
|
|
131
|
+
key: value
|
|
132
|
+
for key, value in request.QUERY.dict().items()
|
|
133
|
+
if key in form_class.base_fields
|
|
134
|
+
}
|
|
135
|
+
)
|
|
131
136
|
return initial
|
|
132
137
|
|
|
133
138
|
def get_form_kwargs(self) -> dict[str, Any]:
|
|
@@ -143,29 +148,33 @@ class HyperFormMixin[_F: forms.BaseForm](HtmxFormMixin[_F]):
|
|
|
143
148
|
`self.get_initial()`.
|
|
144
149
|
|
|
145
150
|
"""
|
|
146
|
-
request = cast(
|
|
151
|
+
request = cast("HtmxRequest", self.request)
|
|
147
152
|
|
|
148
153
|
kwargs = super().get_form_kwargs()
|
|
149
154
|
# TODO: or if self.http_method_names == ['get']?
|
|
150
|
-
if getattr(self.form_class,
|
|
151
|
-
if data := (request.QUERY or kwargs.get(
|
|
152
|
-
kwargs.update(
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
155
|
+
if getattr(self.form_class, "method", "").upper() == "GET":
|
|
156
|
+
if data := (request.QUERY or kwargs.get("initial")):
|
|
157
|
+
kwargs.update(
|
|
158
|
+
{
|
|
159
|
+
"data": data,
|
|
160
|
+
"files": request.FILES,
|
|
161
|
+
}
|
|
162
|
+
)
|
|
156
163
|
else:
|
|
157
164
|
# Must avoid binding the form by passing an empty dict to `data`
|
|
158
165
|
pass
|
|
159
|
-
elif request.method !=
|
|
160
|
-
kwargs.update(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
166
|
+
elif request.method != "GET":
|
|
167
|
+
kwargs.update(
|
|
168
|
+
{
|
|
169
|
+
"data": request.BODY,
|
|
170
|
+
"files": request.FILES,
|
|
171
|
+
}
|
|
172
|
+
)
|
|
164
173
|
return kwargs
|
|
165
174
|
|
|
166
|
-
def form_valid(self, form:
|
|
175
|
+
def form_valid(self, form: F) -> HttpResponse:
|
|
167
176
|
"""Proper response handling per HTTP verb."""
|
|
168
|
-
if self.request.method ==
|
|
177
|
+
if self.request.method == "GET":
|
|
169
178
|
message = (
|
|
170
179
|
"Views that inherit from HyperFormMixin must implement "
|
|
171
180
|
"a custom `form_valid()` for forms with [method=get]."
|
|
@@ -174,7 +183,7 @@ class HyperFormMixin[_F: forms.BaseForm](HtmxFormMixin[_F]):
|
|
|
174
183
|
return super().form_valid(form)
|
|
175
184
|
|
|
176
185
|
|
|
177
|
-
class ProcessHyperFormView[
|
|
186
|
+
class ProcessHyperFormView[F: forms.BaseForm](HyperFormMixin[F], View):
|
|
178
187
|
"""Render a form on GET and facilitate processing on any verb."""
|
|
179
188
|
|
|
180
189
|
def get(
|
|
@@ -244,15 +253,32 @@ class ProcessHyperFormView[_F: forms.BaseForm](HyperFormMixin[_F], View):
|
|
|
244
253
|
raise ImproperlyConfigured(message)
|
|
245
254
|
|
|
246
255
|
|
|
247
|
-
|
|
256
|
+
# TODO: What's the point if all the functionality is in ProcessHyperFormView?
|
|
257
|
+
class HyperFormView[F: forms.BaseForm](ProcessHyperFormView[F]):
|
|
248
258
|
"""A view for displaying a form and rendering a template response."""
|
|
259
|
+
|
|
249
260
|
pass
|
|
250
261
|
|
|
251
262
|
|
|
252
|
-
class SingleObjectPermissionMixin[
|
|
263
|
+
class SingleObjectPermissionMixin[M: Model](SingleObjectMixin[M]):
|
|
253
264
|
"""Adds a permisssions check to SingleObjectMixin."""
|
|
254
265
|
|
|
255
|
-
object:
|
|
266
|
+
object: M | None
|
|
267
|
+
kwargs: dict[str, Any]
|
|
268
|
+
|
|
269
|
+
def get_object_or_none(
|
|
270
|
+
self,
|
|
271
|
+
queryset: QuerySet[M] | None = None,
|
|
272
|
+
) -> M | None:
|
|
273
|
+
"""Resolve the object to operate on, or None when creating.
|
|
274
|
+
|
|
275
|
+
Returns the pk/slug object when the URL identifies one, otherwise None.
|
|
276
|
+
Subclasses may override to resolve the object by other means, such as
|
|
277
|
+
the request user, while still returning None to signal object creation.
|
|
278
|
+
"""
|
|
279
|
+
if self.pk_url_kwarg in self.kwargs or self.slug_url_kwarg in self.kwargs:
|
|
280
|
+
return self.get_object(queryset)
|
|
281
|
+
return None
|
|
256
282
|
|
|
257
283
|
def setup(
|
|
258
284
|
self,
|
|
@@ -260,16 +286,8 @@ class SingleObjectPermissionMixin[_M: Model](SingleObjectMixin[_M]):
|
|
|
260
286
|
*args: Any,
|
|
261
287
|
**kwargs: Any,
|
|
262
288
|
) -> None:
|
|
263
|
-
cast(
|
|
264
|
-
|
|
265
|
-
self.pk_url_kwarg in kwargs
|
|
266
|
-
or self.slug_url_kwarg in kwargs
|
|
267
|
-
):
|
|
268
|
-
# Update or delete object
|
|
269
|
-
self.object = self.get_object(*args, **kwargs)
|
|
270
|
-
else:
|
|
271
|
-
# Create object
|
|
272
|
-
self.object = None
|
|
289
|
+
cast("SupportsSetup", super()).setup(request, *args, **kwargs)
|
|
290
|
+
self.object = self.get_object_or_none()
|
|
273
291
|
|
|
274
292
|
# Verify the User has CRUD permissions
|
|
275
293
|
if not self.has_permission(request, self.object):
|
|
@@ -278,7 +296,7 @@ class SingleObjectPermissionMixin[_M: Model](SingleObjectMixin[_M]):
|
|
|
278
296
|
def has_permission(
|
|
279
297
|
self,
|
|
280
298
|
request: HttpRequest,
|
|
281
|
-
obj:
|
|
299
|
+
obj: M | None,
|
|
282
300
|
) -> bool:
|
|
283
301
|
message = (
|
|
284
302
|
"SingleObjectPermissionMixin requires that subclasses define a "
|
|
@@ -288,11 +306,11 @@ class SingleObjectPermissionMixin[_M: Model](SingleObjectMixin[_M]):
|
|
|
288
306
|
raise ImproperlyConfigured(message)
|
|
289
307
|
|
|
290
308
|
|
|
291
|
-
class ModelFormView[
|
|
292
|
-
SuccessMessageMixin[
|
|
293
|
-
SingleObjectPermissionMixin[
|
|
294
|
-
ModelFormMixin[
|
|
295
|
-
HyperFormView[
|
|
309
|
+
class ModelFormView[M: Model, F: forms.ModelForm[Any]](
|
|
310
|
+
SuccessMessageMixin[F],
|
|
311
|
+
SingleObjectPermissionMixin[M],
|
|
312
|
+
ModelFormMixin[M, F],
|
|
313
|
+
HyperFormView[F],
|
|
296
314
|
):
|
|
297
315
|
"""Combines Django's CreateView, UpdateView, and DeleteView via HTMX.
|
|
298
316
|
|
|
@@ -313,10 +331,10 @@ class ModelFormView[_M: Model, _F: forms.ModelForm[Any]](
|
|
|
313
331
|
|
|
314
332
|
"""
|
|
315
333
|
|
|
316
|
-
http_method_names: Sequence[str] = (
|
|
334
|
+
http_method_names: Sequence[str] = ("get", "post", "patch", "put", "delete")
|
|
317
335
|
|
|
318
|
-
def form_valid(self, form:
|
|
319
|
-
if self.request.method !=
|
|
336
|
+
def form_valid(self, form: F) -> HttpResponse:
|
|
337
|
+
if self.request.method != "PATCH":
|
|
320
338
|
return super().form_valid(form)
|
|
321
339
|
|
|
322
340
|
self.object = form.save(commit=False)
|
|
@@ -339,7 +357,7 @@ class ModelFormView[_M: Model, _F: forms.ModelForm[Any]](
|
|
|
339
357
|
request: HttpRequest,
|
|
340
358
|
*args: Any,
|
|
341
359
|
**kwargs: Any,
|
|
342
|
-
|
|
360
|
+
) -> HttpResponse:
|
|
343
361
|
"""Delete the object or the requested relationships.
|
|
344
362
|
|
|
345
363
|
Notes
|
|
@@ -349,12 +367,12 @@ class ModelFormView[_M: Model, _F: forms.ModelForm[Any]](
|
|
|
349
367
|
|
|
350
368
|
"""
|
|
351
369
|
assert self.object is not None
|
|
352
|
-
htmx_request = cast(
|
|
370
|
+
htmx_request = cast("HtmxRequest", request)
|
|
353
371
|
|
|
354
372
|
field_names = set(f.name for f in self.object._meta.get_fields())
|
|
355
373
|
param_names = set(htmx_request.QUERY)
|
|
356
374
|
|
|
357
|
-
if
|
|
375
|
+
if related_names := (field_names & param_names):
|
|
358
376
|
# Do not delete the primary object, only the related data
|
|
359
377
|
for name in related_names:
|
|
360
378
|
field = self.object._meta.get_field(name)
|
|
@@ -379,9 +397,58 @@ class ModelFormView[_M: Model, _F: forms.ModelForm[Any]](
|
|
|
379
397
|
if message := self.get_success_message({}):
|
|
380
398
|
messages.error(request, message)
|
|
381
399
|
|
|
382
|
-
if cast(
|
|
400
|
+
if cast("HtmxRequest", self.request).htmx:
|
|
383
401
|
# No need to redirect, just render the normal GET response
|
|
384
402
|
response = self.render_to_response(self.get_context_data())
|
|
385
403
|
else:
|
|
386
404
|
response = HttpResponseRedirect(self.get_success_url())
|
|
387
405
|
return response
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
class HtmxChainedFormView[F: forms.BaseForm](ProcessHyperFormView[F]):
|
|
409
|
+
"""Facilitates dynamic/chained form fields."""
|
|
410
|
+
|
|
411
|
+
def get_field_choices(self) -> dict[str, Choices]:
|
|
412
|
+
raise ImproperlyConfigured(
|
|
413
|
+
_("HtmxChainedFormView requires an implementation of `get_field_choices()`.")
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
def get_form_kwargs(self) -> dict[str, Any]:
|
|
417
|
+
"""Override to provide `field_choices` kwarg.
|
|
418
|
+
|
|
419
|
+
Notes
|
|
420
|
+
-----
|
|
421
|
+
Django Form instances that are going to be used with this view must
|
|
422
|
+
allow a `field_choices` kwarg in their __init__ method.
|
|
423
|
+
|
|
424
|
+
"""
|
|
425
|
+
kwargs = super().get_form_kwargs()
|
|
426
|
+
if hasattr(self.request, "htmx") and self.request.htmx:
|
|
427
|
+
kwargs["field_choices"] = self.get_field_choices()
|
|
428
|
+
return kwargs
|
|
429
|
+
|
|
430
|
+
def _render_chained_field(self, form: F, target: str) -> HttpResponse:
|
|
431
|
+
"""Return the HTML for the chained field matching the HTMX target id."""
|
|
432
|
+
form_field_id_to_name = {
|
|
433
|
+
bound_field.id_for_label: bound_field.name
|
|
434
|
+
for bound_field in form
|
|
435
|
+
if isinstance(bound_field.field, ChoiceField)
|
|
436
|
+
}
|
|
437
|
+
try:
|
|
438
|
+
field_name = form_field_id_to_name[target]
|
|
439
|
+
except KeyError:
|
|
440
|
+
raise ImproperlyConfigured(
|
|
441
|
+
_(f"HTMX target '{target}' does not match any form field IDs.")
|
|
442
|
+
)
|
|
443
|
+
return HttpResponse(str(form[field_name]))
|
|
444
|
+
|
|
445
|
+
def get(
|
|
446
|
+
self,
|
|
447
|
+
request: HttpRequest,
|
|
448
|
+
*args: Any,
|
|
449
|
+
**kwargs: Any,
|
|
450
|
+
) -> HttpResponse:
|
|
451
|
+
if hasattr(request, "htmx") and request.htmx:
|
|
452
|
+
form = self.get_form()
|
|
453
|
+
return self._render_chained_field(form, request.htmx.target)
|
|
454
|
+
return super().get(request, *args, **kwargs)
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-htmx-cbv"
|
|
3
|
+
version = "0.0.1.dev2"
|
|
4
|
+
description = "Django class-based views, tailored for htmx"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
requires-python = ">=3.14"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"django>=6.0.0,<7.0.0",
|
|
10
|
+
"django-htmx>=1.27.0,<2.0.0",
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
[[project.authors]]
|
|
14
|
+
name = "Geoffrey Eisenbarth"
|
|
15
|
+
email = "geoffrey.eisenbarth@gmail.com"
|
|
16
|
+
|
|
17
|
+
[dependency-groups]
|
|
18
|
+
dev = [
|
|
19
|
+
"ipython>=9.12.0,<10.0.0",
|
|
20
|
+
"ipdb>=0.13.13,<0.14.0",
|
|
21
|
+
"ruff>=0.17.6",
|
|
22
|
+
"mypy>=2.4.1",
|
|
23
|
+
"django-stubs>=6.0.2,<7.0.0",
|
|
24
|
+
"django-stubs-ext>=6.0.2,<7.0.0",
|
|
25
|
+
"pytest>=9.0.3,<10.0.0",
|
|
26
|
+
"pytest-django>=4.12.0,<5.0.0",
|
|
27
|
+
"codecov>=2.1.13,<3.0.0",
|
|
28
|
+
"parameterized>=0.9.0,<0.10.0",
|
|
29
|
+
"beautifulsoup4>=4.14.3,<5.0.0",
|
|
30
|
+
"pre-commit>=4.5.1,<5.0.0",
|
|
31
|
+
"coverage>=7.13.5",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["uv_build>=0.12.12,<0.13.0"]
|
|
36
|
+
build-backend = "uv_build"
|
|
37
|
+
|
|
38
|
+
[tool.uv.build-backend]
|
|
39
|
+
module-root = ""
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 88
|
|
43
|
+
indent-width = 2
|
|
44
|
+
target-version = "py314"
|
|
45
|
+
exclude = [
|
|
46
|
+
"*/migrations/*",
|
|
47
|
+
"manage.py",
|
|
48
|
+
"settings.py",
|
|
49
|
+
"local_settings.py",
|
|
50
|
+
"vendor.py",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[tool.ruff.lint]
|
|
54
|
+
select = [
|
|
55
|
+
"E",
|
|
56
|
+
"W",
|
|
57
|
+
"F",
|
|
58
|
+
"I",
|
|
59
|
+
"UP",
|
|
60
|
+
"RUF100",
|
|
61
|
+
"DJ",
|
|
62
|
+
]
|
|
63
|
+
ignore = [
|
|
64
|
+
"E111",
|
|
65
|
+
"E114",
|
|
66
|
+
"E731",
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
[tool.ruff.format]
|
|
70
|
+
quote-style = "double"
|
|
71
|
+
indent-style = "space"
|
|
72
|
+
|
|
73
|
+
[tool.pytest.ini_options]
|
|
74
|
+
DJANGO_SETTINGS_MODULE = "project.settings"
|
|
75
|
+
pythonpath = ["tests/project"]
|
|
76
|
+
|
|
77
|
+
[tool.coverage.run]
|
|
78
|
+
branch = true
|
|
79
|
+
source = ["django_htmx_cbv"]
|
|
80
|
+
|
|
81
|
+
[tool.coverage.report]
|
|
82
|
+
show_missing = true
|
|
83
|
+
|
|
84
|
+
[tool.mypy]
|
|
85
|
+
python_version = "3.14"
|
|
86
|
+
plugins = ["mypy_django_plugin.main"]
|
|
87
|
+
strict = true
|
|
88
|
+
ignore_missing_imports = true
|
|
89
|
+
packages = [
|
|
90
|
+
"django_htmx_cbv",
|
|
91
|
+
"tests",
|
|
92
|
+
]
|
|
93
|
+
exclude = [
|
|
94
|
+
"future.py",
|
|
95
|
+
"tests/project/manage.py",
|
|
96
|
+
"tests/project/project",
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
[[tool.mypy.overrides]]
|
|
100
|
+
module = ["tests.project.test_middleware"]
|
|
101
|
+
disallow_untyped_decorators = false
|
|
102
|
+
|
|
103
|
+
[tool.django-stubs]
|
|
104
|
+
django_settings_module = "tests.project.project.settings"
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-htmx-cbv"
|
|
3
|
+
version = "0.0.1.dev2"
|
|
4
|
+
description = "Django class-based views, tailored for htmx"
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Geoffrey Eisenbarth",email = "geoffrey.eisenbarth@gmail.com"}
|
|
7
|
+
]
|
|
8
|
+
license = "MIT"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"django>=6.0.0,<7.0.0",
|
|
13
|
+
"django-htmx>=1.27.0,<2.0.0",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[dependency-groups]
|
|
17
|
+
dev = [
|
|
18
|
+
"ipython>=9.12.0,<10.0.0",
|
|
19
|
+
"ipdb>=0.13.13,<0.14.0",
|
|
20
|
+
"ruff>=0.17.6",
|
|
21
|
+
"mypy>=2.4.1",
|
|
22
|
+
"django-stubs>=6.0.2,<7.0.0",
|
|
23
|
+
"django-stubs-ext>=6.0.2,<7.0.0",
|
|
24
|
+
"pytest>=9.0.3,<10.0.0",
|
|
25
|
+
"pytest-django>=4.12.0,<5.0.0",
|
|
26
|
+
"codecov>=2.1.13,<3.0.0",
|
|
27
|
+
"parameterized>=0.9.0,<0.10.0",
|
|
28
|
+
"beautifulsoup4>=4.14.3,<5.0.0",
|
|
29
|
+
"pre-commit>=4.5.1,<5.0.0",
|
|
30
|
+
"coverage>=7.13.5",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["uv_build>=0.12.12,<0.13.0"]
|
|
36
|
+
build-backend = "uv_build"
|
|
37
|
+
|
|
38
|
+
[tool.uv.build-backend]
|
|
39
|
+
module-root = ""
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 88
|
|
43
|
+
indent-width = 2
|
|
44
|
+
target-version = "py314"
|
|
45
|
+
exclude = [
|
|
46
|
+
"*/migrations/*",
|
|
47
|
+
"manage.py",
|
|
48
|
+
"settings.py",
|
|
49
|
+
"local_settings.py",
|
|
50
|
+
"vendor.py",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[tool.ruff.lint]
|
|
54
|
+
select = [
|
|
55
|
+
"E", # pycodestyle errors
|
|
56
|
+
"W", # pycodestyle warnings
|
|
57
|
+
"F", # pyflakes (unused imports, undefined names, etc.)
|
|
58
|
+
"I", # isort (automatically sorts and organizes imports)
|
|
59
|
+
"UP", # pyupgrade (modernizes Python syntax)
|
|
60
|
+
"RUF100", # flags unused noqa comments
|
|
61
|
+
"DJ", # flake8-django
|
|
62
|
+
]
|
|
63
|
+
ignore = [
|
|
64
|
+
"E111", # Indentation is not a multiple of four
|
|
65
|
+
"E114", # Comment indentation is not a multiple of four
|
|
66
|
+
"E731", # Do not assign a lambda expression, use a def
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
[tool.ruff.format]
|
|
70
|
+
quote-style = "double"
|
|
71
|
+
indent-style = "space"
|
|
72
|
+
|
|
73
|
+
[tool.pytest.ini_options]
|
|
74
|
+
DJANGO_SETTINGS_MODULE = "project.settings"
|
|
75
|
+
pythonpath = ["tests/project"]
|
|
76
|
+
|
|
77
|
+
[tool.coverage.run]
|
|
78
|
+
branch = true
|
|
79
|
+
source = ["django_htmx_cbv"]
|
|
80
|
+
|
|
81
|
+
[tool.coverage.report]
|
|
82
|
+
show_missing = true
|
|
83
|
+
|
|
84
|
+
[tool.mypy]
|
|
85
|
+
python_version = "3.14"
|
|
86
|
+
plugins = ["mypy_django_plugin.main"]
|
|
87
|
+
strict = true
|
|
88
|
+
ignore_missing_imports = true
|
|
89
|
+
packages = ["django_htmx_cbv", "tests"]
|
|
90
|
+
exclude = [
|
|
91
|
+
"future.py",
|
|
92
|
+
"tests/project/manage.py",
|
|
93
|
+
"tests/project/project",
|
|
94
|
+
]
|
|
95
|
+
|
|
96
|
+
[[tool.mypy.overrides]]
|
|
97
|
+
module = ["tests.project.test_middleware"]
|
|
98
|
+
disallow_untyped_decorators = false
|
|
99
|
+
|
|
100
|
+
[tool.django-stubs]
|
|
101
|
+
django_settings_module = "tests.project.project.settings"
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Geoffrey B. Eisenbarth
|
|
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.
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
from typing import TYPE_CHECKING
|
|
2
|
-
|
|
3
|
-
from django.core.exceptions import ImproperlyConfigured
|
|
4
|
-
from django.db.models import QuerySet
|
|
5
|
-
from django.forms import Form
|
|
6
|
-
from django.http import HttpResponse
|
|
7
|
-
from django.utils.translation import gettext_lazy as _
|
|
8
|
-
|
|
9
|
-
from django_htmx_cbv.views import ProcessHyperFormView
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if TYPE_CHECKING:
|
|
13
|
-
from typing import Any
|
|
14
|
-
from django import forms
|
|
15
|
-
from django.http import HttpRequest
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
# forms.py
|
|
19
|
-
class HtmxChainedForm(Form):
|
|
20
|
-
"""Custom form subclass to work with HtmxChainedFormView."""
|
|
21
|
-
def __init__(
|
|
22
|
-
self,
|
|
23
|
-
options: dict[str, Any] | None = None,
|
|
24
|
-
*args: Any,
|
|
25
|
-
**kwargs: Any,
|
|
26
|
-
) -> None:
|
|
27
|
-
super().__init__(*args, **kwargs)
|
|
28
|
-
|
|
29
|
-
# Set options on form
|
|
30
|
-
if options is not None:
|
|
31
|
-
for field_name, choices in options.items():
|
|
32
|
-
form_field = self.fields[field_name]
|
|
33
|
-
if hasattr(form_field, 'queryset'):
|
|
34
|
-
if isinstance(choices, QuerySet):
|
|
35
|
-
form_field.queryset = choices
|
|
36
|
-
else:
|
|
37
|
-
message = _(
|
|
38
|
-
f'The choices for {field_name} must be a QuerySet!'
|
|
39
|
-
)
|
|
40
|
-
raise TypeError(message)
|
|
41
|
-
elif hasattr(form_field, 'choices'):
|
|
42
|
-
if isinstance(choices, list):
|
|
43
|
-
form_field.choices = choices
|
|
44
|
-
else:
|
|
45
|
-
message = _(
|
|
46
|
-
f'The choices for {field_name} must be a list of tuples!'
|
|
47
|
-
)
|
|
48
|
-
raise TypeError(message)
|
|
49
|
-
self.fields[field_name] = form_field
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
# views.py
|
|
53
|
-
class HtmxChainedFormView[_F: forms.BaseForm](ProcessHyperFormView[_F]):
|
|
54
|
-
"""Allows dynamic chained form fields."""
|
|
55
|
-
|
|
56
|
-
def get_options(self) -> dict[str, Any]:
|
|
57
|
-
message = (
|
|
58
|
-
"HtmxChainedFormView requires an implementation "
|
|
59
|
-
"of `get_options()` that returns a dictionary "
|
|
60
|
-
"of the form {field_name: [choices]}. If form "
|
|
61
|
-
"values are required, they should be specified "
|
|
62
|
-
"via `hx-include`."
|
|
63
|
-
)
|
|
64
|
-
raise ImproperlyConfigured(message)
|
|
65
|
-
|
|
66
|
-
def get_form_kwargs(self) -> dict[str, Any]:
|
|
67
|
-
"""Override to provide `options` kwarg.
|
|
68
|
-
|
|
69
|
-
Notes
|
|
70
|
-
-----
|
|
71
|
-
Django Form instances that are going to be used with this view must
|
|
72
|
-
allow a `options` kwarg in their __init__ method.
|
|
73
|
-
|
|
74
|
-
"""
|
|
75
|
-
kwargs = super().get_form_kwargs()
|
|
76
|
-
if self.request.htmx:
|
|
77
|
-
kwargs['options'] = self.get_options()
|
|
78
|
-
return kwargs
|
|
79
|
-
|
|
80
|
-
def get(
|
|
81
|
-
self,
|
|
82
|
-
request: HttpRequest,
|
|
83
|
-
*args: Any,
|
|
84
|
-
**kwargs: Any,
|
|
85
|
-
) -> HttpResponse:
|
|
86
|
-
"""Override to determine what needs to be rendered."""
|
|
87
|
-
# TODO PUSH: Find a proper way to get field ids? (See 'id_' below too)
|
|
88
|
-
form_class = self.get_form_class()
|
|
89
|
-
form_field_ids = [
|
|
90
|
-
f'id_{field_name}'
|
|
91
|
-
for field_name, field in form_class.base_fields.items()
|
|
92
|
-
if hasattr(field, 'choices')
|
|
93
|
-
] + ['id_choices']
|
|
94
|
-
|
|
95
|
-
if request.htmx.target in form_field_ids:
|
|
96
|
-
form_class = self.get_form_class()
|
|
97
|
-
kwargs = self.get_form_kwargs()
|
|
98
|
-
form = form_class(**kwargs)
|
|
99
|
-
|
|
100
|
-
html = "".join(
|
|
101
|
-
map(str, (form[field_name] for field_name in kwargs['options']))
|
|
102
|
-
)
|
|
103
|
-
response = HttpResponse(html)
|
|
104
|
-
else:
|
|
105
|
-
response = HttpResponse(status=204)
|
|
106
|
-
|
|
107
|
-
return response
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
[project]
|
|
2
|
-
name = "django-htmx-cbv"
|
|
3
|
-
version = "0.0.1.dev1"
|
|
4
|
-
description = "Django class-based views, tailored for htmx"
|
|
5
|
-
authors = [
|
|
6
|
-
{name = "Geoffrey Eisenbarth",email = "geoffrey.eisenbarth@gmail.com"}
|
|
7
|
-
]
|
|
8
|
-
license = "MIT"
|
|
9
|
-
readme = "README.md"
|
|
10
|
-
requires-python = ">=3.14"
|
|
11
|
-
dependencies = [
|
|
12
|
-
"django (>=6.0.0,<7.0.0)",
|
|
13
|
-
"django-htmx (>=1.27.0,<2.0.0)",
|
|
14
|
-
]
|
|
15
|
-
|
|
16
|
-
[dependency-groups]
|
|
17
|
-
dev = [
|
|
18
|
-
"ipython (>=9.12.0,<10.0.0)",
|
|
19
|
-
"ipdb (>=0.13.13,<0.14.0)",
|
|
20
|
-
"flake8 (>=7.3.0,<8.0.0)",
|
|
21
|
-
"flake8-type-checking (>=3.2.0,<4.0.0)",
|
|
22
|
-
"mypy (>=1.20.1,<2.0.0)",
|
|
23
|
-
"django-stubs (>=6.0.2,<7.0.0)",
|
|
24
|
-
"django-stubs-ext (>=6.0.2,<7.0.0)",
|
|
25
|
-
"pytest (>=9.0.3,<10.0.0)",
|
|
26
|
-
"pytest-django (>=4.12.0,<5.0.0)",
|
|
27
|
-
"codecov (>=2.1.13,<3.0.0)",
|
|
28
|
-
"parameterized (>=0.9.0,<0.10.0)",
|
|
29
|
-
"beautifulsoup4 (>=4.14.3,<5.0.0)",
|
|
30
|
-
"pre-commit (>=4.5.1,<5.0.0)"
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
[build-system]
|
|
35
|
-
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
36
|
-
build-backend = "poetry.core.masonry.api"
|
|
37
|
-
|
|
38
|
-
[tool.pytest.ini_options]
|
|
39
|
-
DJANGO_SETTINGS_MODULE = "project.settings"
|
|
40
|
-
pythonpath = ["tests/project"]
|
|
41
|
-
|
|
42
|
-
[tool.coverage.report]
|
|
43
|
-
show_missing = true
|
|
44
|
-
|
|
45
|
-
[tool.mypy]
|
|
46
|
-
python_version = "3.14"
|
|
47
|
-
plugins = ["mypy_django_plugin.main"]
|
|
48
|
-
mypy_path = ["tests/project"]
|
|
49
|
-
ignore_missing_imports = true
|
|
50
|
-
strict = true
|
|
51
|
-
exclude = [
|
|
52
|
-
"manage.py",
|
|
53
|
-
"settings.py",
|
|
54
|
-
"future.py",
|
|
55
|
-
"tests/project/",
|
|
56
|
-
]
|
|
57
|
-
files = ["django_htmx_cbv/"]
|
|
58
|
-
|
|
59
|
-
[tool.django-stubs]
|
|
60
|
-
django_settings_module = "project.settings"
|
|
File without changes
|
|
File without changes
|