chore(root): remove DRF and django-filter dependencies
drop DRF and django-filter to make django_nepkit framework‑agnostic, simplify dependencies, and align with v0.2.2 breaking change.
This commit is contained in:
40 files changed
+3286
-1406
No files matched your search
+106
-25
@@ -1,9 +1,15 @@
|
||||
import logging
|
||||
from datetime import time as python_time
|
||||
|
||||
from django import forms
|
||||
from django.urls import reverse, NoReverseMatch
|
||||
from django.urls import NoReverseMatch, reverse
|
||||
from nepali.datetime import nepalidate
|
||||
from django_nepkit.utils import BS_DATE_FORMAT
|
||||
from nepali.number import english_to_nepali
|
||||
|
||||
from django_nepkit.conf import nepkit_settings
|
||||
from django_nepkit.utils import BS_TIME_FORMAT, format_nepali_time
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _append_css_class(attrs, class_name: str):
|
||||
@@ -23,48 +29,57 @@ class NepaliWidgetMixin:
|
||||
|
||||
attrs = kwargs.get("attrs", {}) or {}
|
||||
|
||||
# Language settings
|
||||
if self.ne:
|
||||
attrs["data-ne"] = "true"
|
||||
if self.en:
|
||||
attrs["data-en"] = "true"
|
||||
|
||||
# Pass the date format to JavaScript
|
||||
attrs["data-format"] = BS_DATE_FORMAT
|
||||
|
||||
attrs["data-format"] = self._default_format()
|
||||
self._configure_attrs(attrs)
|
||||
|
||||
kwargs["attrs"] = attrs
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def _default_format(self) -> str:
|
||||
return nepkit_settings.BS_DATE_FORMAT
|
||||
|
||||
def _configure_attrs(self, attrs):
|
||||
"""Override in subclasses or use class attributes."""
|
||||
css_class = getattr(self, "css_class", None)
|
||||
if css_class:
|
||||
_append_css_class(attrs, css_class)
|
||||
|
||||
def _resolve_url(self, url_name, attr_name):
|
||||
"""Resolve a URL name and return the URL string.
|
||||
|
||||
On failure, log a warning and return an empty string so the JS
|
||||
can degrade gracefully. The widget's data attribute is omitted
|
||||
entirely (rather than set to a broken value).
|
||||
"""
|
||||
try:
|
||||
return reverse(url_name)
|
||||
except NoReverseMatch:
|
||||
logger.warning(
|
||||
"nepkit widget %r: URL name %r not found; "
|
||||
"the corresponding select will not auto-populate. "
|
||||
"Make sure 'django_nepkit.urls' is included in your URLconf.",
|
||||
self.__class__.__name__,
|
||||
url_name,
|
||||
)
|
||||
return ""
|
||||
|
||||
def get_context(self, name, value, attrs):
|
||||
context = super().get_context(name, value, attrs)
|
||||
widget_attrs = context.get("widget", {}).get("attrs", {})
|
||||
|
||||
# Handle URLs for data fetching
|
||||
url_name = getattr(self, "_url_name", None)
|
||||
if url_name:
|
||||
try:
|
||||
widget_attrs["data-url"] = reverse(url_name)
|
||||
except NoReverseMatch:
|
||||
pass
|
||||
widget_attrs["data-url"] = self._resolve_url(url_name, "data-url")
|
||||
|
||||
# Handle HTMX dynamic updates
|
||||
if self.htmx:
|
||||
hx_url_name = getattr(self, "_hx_url_name", None)
|
||||
if hx_url_name:
|
||||
try:
|
||||
widget_attrs["hx-get"] = reverse(hx_url_name)
|
||||
widget_attrs["hx-target"] = getattr(self, "_hx_target", "")
|
||||
widget_attrs["hx-trigger"] = "change"
|
||||
except NoReverseMatch:
|
||||
pass
|
||||
widget_attrs["hx-get"] = self._resolve_url(hx_url_name, "hx-get")
|
||||
widget_attrs["hx-target"] = getattr(self, "_hx_target", "")
|
||||
widget_attrs["hx-trigger"] = "change"
|
||||
|
||||
return context
|
||||
|
||||
@@ -102,6 +117,12 @@ class MunicipalitySelectWidget(NepaliWidgetMixin, ChainedSelectWidget):
|
||||
css_class = "nepkit-municipality-select"
|
||||
_url_name = "django_nepkit:municipality-list"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if self.htmx:
|
||||
self._hx_url_name = "django_nepkit:municipality-list"
|
||||
self._hx_target = ".nepkit-municipality-select"
|
||||
|
||||
|
||||
class NepaliDatePickerWidget(NepaliWidgetMixin, forms.TextInput):
|
||||
input_type = "text"
|
||||
@@ -119,6 +140,7 @@ class NepaliDatePickerWidget(NepaliWidgetMixin, forms.TextInput):
|
||||
)
|
||||
|
||||
def _configure_attrs(self, attrs):
|
||||
super()._configure_attrs(attrs)
|
||||
classes = attrs.get("class", "")
|
||||
if "vDateField" in classes:
|
||||
classes = classes.replace("vDateField", "")
|
||||
@@ -127,15 +149,74 @@ class NepaliDatePickerWidget(NepaliWidgetMixin, forms.TextInput):
|
||||
_append_css_class(attrs, "nepkit-datepicker")
|
||||
attrs["autocomplete"] = "off"
|
||||
attrs["placeholder"] = (
|
||||
BS_DATE_FORMAT.replace("%Y", "YYYY").replace("%m", "MM").replace("%d", "DD")
|
||||
nepkit_settings.BS_DATE_FORMAT.replace("%Y", "YYYY")
|
||||
.replace("%m", "MM")
|
||||
.replace("%d", "DD")
|
||||
)
|
||||
|
||||
def format_value(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
from django_nepkit.utils import format_nepali_date
|
||||
|
||||
if self.ne and isinstance(value, nepalidate):
|
||||
if hasattr(value, "strftime_ne"):
|
||||
return value.strftime_ne(BS_DATE_FORMAT)
|
||||
try:
|
||||
if isinstance(value, nepalidate):
|
||||
if self.ne and hasattr(value, "strftime_ne"):
|
||||
return value.strftime_ne(nepkit_settings.BS_DATE_FORMAT)
|
||||
return format_nepali_date(value, nepkit_settings.BS_DATE_FORMAT)
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
except (ValueError, TypeError, AttributeError):
|
||||
logger.warning(
|
||||
"nepkit-datepicker: cannot format value %r; rendering empty", value
|
||||
)
|
||||
return ""
|
||||
|
||||
return super().format_value(value)
|
||||
|
||||
class NepaliTimeWidget(NepaliWidgetMixin, forms.TextInput):
|
||||
"""Plain text input for time values with Devanagari digit support."""
|
||||
|
||||
input_type = "text"
|
||||
css_class = "nepkit-time"
|
||||
|
||||
class Media:
|
||||
js = ("django_nepkit/js/nepali-time-init.js",)
|
||||
|
||||
def _default_format(self) -> str:
|
||||
return BS_TIME_FORMAT
|
||||
|
||||
def _configure_attrs(self, attrs):
|
||||
super()._configure_attrs(attrs)
|
||||
_append_css_class(attrs, "nepkit-time-input")
|
||||
attrs["autocomplete"] = "off"
|
||||
fmt = nepkit_settings.BS_TIME_FORMAT
|
||||
attrs["placeholder"] = (
|
||||
fmt.replace("%H", "HH")
|
||||
.replace("%I", "hh")
|
||||
.replace("%M", "mm")
|
||||
.replace("%S", "ss")
|
||||
.replace("%p", "AM/PM")
|
||||
)
|
||||
|
||||
def format_value(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, python_time):
|
||||
try:
|
||||
formatted = format_nepali_time(value, nepkit_settings.BS_TIME_FORMAT)
|
||||
except (ValueError, TypeError):
|
||||
logger.warning(
|
||||
"nepkit-time: cannot format value %r; rendering empty", value
|
||||
)
|
||||
return ""
|
||||
if self.ne and formatted:
|
||||
formatted = english_to_nepali(formatted)
|
||||
return formatted
|
||||
if isinstance(value, str):
|
||||
return value
|
||||
# Unknown type — refuse to leak a Python repr into the page.
|
||||
logger.warning(
|
||||
"nepkit-time: unexpected value type %s; rendering empty",
|
||||
type(value).__name__,
|
||||
)
|
||||
return ""
|
||||
Reference in new issue
Block a user