Skip to content

Commit

Permalink
feat: adds HTMXConfig for plugin customization (#6)
Browse files Browse the repository at this point in the history
* feat: adds `HTMXConfig` for plugin customization

* fix: addss type annotation
  • Loading branch information
cofin authored Oct 23, 2024
1 parent 077c26a commit d1aa968
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
3 changes: 2 additions & 1 deletion litestar_htmx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from litestar_htmx.plugin import HTMXPlugin
from litestar_htmx.plugin import HTMXConfig, HTMXPlugin
from litestar_htmx.request import HTMXDetails, HTMXHeaders, HTMXRequest
from litestar_htmx.response import (
ClientRedirect,
Expand All @@ -25,6 +25,7 @@

__all__ = (
"HTMXPlugin",
"HTMXConfig",
"HTMXDetails",
"HTMXHeaders",
"HTMXRequest",
Expand Down
24 changes: 21 additions & 3 deletions litestar_htmx/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from litestar.plugins import InitPluginProtocol
Expand All @@ -24,11 +25,28 @@
from litestar.config.app import AppConfig


@dataclass
class HTMXConfig:
"""Configuration for HTMX Plugin."""

set_request_class_globally: bool = True
"""Sets the `app_config.request_class` to the `HTMXRequest` class at the application level. Defaults to True"""


class HTMXPlugin(InitPluginProtocol):
"""HTMX Plugin."""

def __init__(self) -> None:
"""Initialize the plugin."""
def __init__(self, config: HTMXConfig | None = None) -> None:
"""Initialize the plugin.
Args:
config: Configuration for flash messages, including the template engine instance.
"""
self._config = config or HTMXConfig()

@property
def config(self) -> HTMXConfig:
return self._config

def on_app_init(self, app_config: AppConfig) -> AppConfig:
"""Register the HTMX configuration.
Expand All @@ -39,7 +57,7 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
Returns:
The application configuration with the message callable registered.
"""
if app_config.request_class is None:
if self.config.set_request_class_globally and app_config.request_class is None:
app_config.request_class = HTMXRequest
app_config.signature_types = [
HTMXRequest,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ maintainers = [
name = "litestar-htmx"
readme = "README.md"
requires-python = ">=3.8, <4.0"
version = "0.2.4"
version = "0.3.0"

[build-system]
build-backend = "hatchling.build"
Expand Down
30 changes: 30 additions & 0 deletions tests/test_htmx_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from litestar.config.app import AppConfig

from litestar_htmx import HTMXConfig, HTMXPlugin, HTMXRequest

pytestmark = pytest.mark.anyio


@pytest.mark.parametrize(
"set_request_class_globally",
(True, False),
)
def test_request_class(set_request_class_globally: bool) -> None:
config = HTMXConfig(set_request_class_globally=set_request_class_globally)
plugin = HTMXPlugin(config=config)
app_config = plugin.on_app_init(AppConfig())
if set_request_class_globally:
assert app_config.request_class == HTMXRequest
else:
assert app_config.request_class is None


class CustomHTMXRequest(HTMXRequest):
"""Extra functionality."""


def test_request_class_no_override() -> None:
plugin = HTMXPlugin()
app_config = plugin.on_app_init(AppConfig(request_class=CustomHTMXRequest))
assert app_config.request_class == CustomHTMXRequest
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1aa968

Please sign in to comment.