33from collections .abc import Callable
44from typing import Any
55
6- from plain .http import Response
6+ from plain .http import ResponseBase
77from plain .utils .cache import patch_vary_headers
8+ from plain .views import TemplateView
89
910from .templates import render_template_fragment
1011
1112
12- class HTMXViewMixin :
13- """Mixin for View classes to add HTMX-specific functionality."""
13+ class HTMXView ( TemplateView ) :
14+ """View with HTMX-specific functionality."""
1415
1516 def render_template (self ) -> str :
16- # These methods are provided by the View base class
17- template = self .get_template () # type: ignore[attr-defined]
18- context = self .get_template_context () # type: ignore[attr-defined]
17+ template = self .get_template ()
18+ context = self .get_template_context ()
1919
2020 if self .is_htmx_request () and self .get_htmx_fragment_name ():
2121 return render_template_fragment (
@@ -26,20 +26,20 @@ def render_template(self) -> str:
2626
2727 return template .render (context )
2828
29- def get_response (self ) -> Response :
30- response = super ().get_response () # type: ignore[misc]
29+ def get_response (self ) -> ResponseBase :
30+ response = super ().get_response ()
3131 # Tell browser caching to also consider the fragment header,
3232 # not just the url/cookie.
3333 patch_vary_headers (
3434 response , ["HX-Request" , "Plain-HX-Fragment" , "Plain-HX-Action" ]
3535 )
3636 return response
3737
38- def get_request_handler (self ) -> Callable [... , Any ]:
39- if self .is_htmx_request ():
38+ def get_request_handler (self ) -> Callable [[] , Any ] | None :
39+ if self .is_htmx_request () and self . request . method :
4040 # You can use an htmx_{method} method on views
4141 # (or htmx_{method}_{action} for specific actions)
42- method = f"htmx_{ self .request .method .lower ()} " # type: ignore[attr-defined]
42+ method = f"htmx_{ self .request .method .lower ()} "
4343
4444 if action := self .get_htmx_action_name ():
4545 # If an action is specified, we throw an error if
@@ -52,14 +52,14 @@ def get_request_handler(self) -> Callable[..., Any]:
5252 # to a regular post method if it's not found
5353 return handler
5454
55- return super ().get_request_handler () # type: ignore[misc]
55+ return super ().get_request_handler ()
5656
5757 def is_htmx_request (self ) -> bool :
58- return self .request .headers .get ("HX-Request" ) == "true" # type: ignore[attr-defined]
58+ return self .request .headers .get ("HX-Request" ) == "true"
5959
6060 def get_htmx_fragment_name (self ) -> str :
6161 # A custom header that we pass with the {% htmxfragment %} tag
62- return self .request .headers .get ("Plain-HX-Fragment" , "" ) # type: ignore[attr-defined]
62+ return self .request .headers .get ("Plain-HX-Fragment" , "" )
6363
6464 def get_htmx_action_name (self ) -> str :
65- return self .request .headers .get ("Plain-HX-Action" , "" ) # type: ignore[attr-defined]
65+ return self .request .headers .get ("Plain-HX-Action" , "" )
0 commit comments