Files
django-nepkit/django_nepkit/widgets.py
T

113 lines
3.4 KiB
Python

from django import forms
from django.urls import reverse_lazy
from nepali.datetime import nepalidate
def _append_css_class(attrs, class_name: str):
"""
Django-idiomatic helper to append a CSS class without clobbering existing ones.
"""
existing = (attrs.get("class") or "").strip()
attrs["class"] = (f"{existing} {class_name}").strip() if existing else class_name
return attrs
class NepaliWidgetMixin:
def __init__(self, *args, **kwargs):
self.ne = kwargs.pop("ne", False)
en_value = kwargs.pop("en", True)
if self.ne:
self.en = False
else:
self.en = en_value
attrs = kwargs.get("attrs", {}) or {}
# Add data attributes
if self.ne:
attrs["data-ne"] = "true"
if self.en:
attrs["data-en"] = "true"
# Hook for subclasses to add specific classes/attrs
self._configure_attrs(attrs)
kwargs["attrs"] = attrs
super().__init__(*args, **kwargs)
def _configure_attrs(self, attrs):
"""Override in subclasses to add specific classes or attributes."""
pass
class ChainedSelectWidget(forms.Select):
class Media:
js = (
"django_nepkit/js/nepal-data.js",
"django_nepkit/js/address-chaining.js",
)
class ProvinceSelectWidget(NepaliWidgetMixin, ChainedSelectWidget):
def _configure_attrs(self, attrs):
_append_css_class(attrs, "nepkit-province-select")
class DistrictSelectWidget(NepaliWidgetMixin, ChainedSelectWidget):
def _configure_attrs(self, attrs):
_append_css_class(attrs, "nepkit-district-select")
try:
url = reverse_lazy("django_nepkit:district-list")
str(url) # Force evaluation to catch NoReverseMatch
attrs["data-url"] = url
except Exception:
pass
class MunicipalitySelectWidget(NepaliWidgetMixin, ChainedSelectWidget):
def _configure_attrs(self, attrs):
_append_css_class(attrs, "nepkit-municipality-select")
try:
url = reverse_lazy("django_nepkit:municipality-list")
str(url) # Force evaluation to catch NoReverseMatch
attrs["data-url"] = url
except Exception:
pass
class NepaliDatePickerWidget(NepaliWidgetMixin, forms.TextInput):
input_type = "text"
class Media:
css = {
"all": (
"https://nepalidatepicker.sajanmaharjan.com.np/v5/nepali.datepicker/css/nepali.datepicker.v5.0.6.min.css",
)
}
js = (
"https://code.jquery.com/jquery-3.5.1.slim.min.js",
"https://nepalidatepicker.sajanmaharjan.com.np/v5/nepali.datepicker/js/nepali.datepicker.v5.0.6.min.js",
"django_nepkit/js/nepali-datepicker-init.js",
)
def _configure_attrs(self, attrs):
# Clean up Django admin class if present
classes = attrs.get("class", "")
if "vDateField" in classes:
classes = classes.replace("vDateField", "")
attrs["class"] = classes
_append_css_class(attrs, "nepkit-datepicker")
attrs["autocomplete"] = "off"
attrs["placeholder"] = "YYYY-MM-DD"
def format_value(self, value):
if value is None:
return None
if self.ne and isinstance(value, nepalidate):
if hasattr(value, "strftime_ne"):
return value.strftime_ne("%Y-%m-%d")
return super().format_value(value)