Files
django-nepkit/django_nepkit/serializers.py
T
sankalp 1338f9e5fe refactor: global project refinement, DRY consolidation, and documentation overhaul
This commit represents a comprehensive improvement of the library's architecture, developer experience, and documentation accuracy.

Core Refactoring:
- apply DRY principle to models.py by introducing BaseNepaliBSField and BaseLocationField
- standardize widget logic in widgets.py using NepaliWidgetMixin
- centralize admin filtering logic into BaseNepaliDateFilter

New Features & API:
- add NepaliMonthFilter for Django Admin and NepaliDateMonthFilter for DRF
- implementation of HTMX-driven location chaining for dynamic address selects
- extend demo project with comprehensive DRF search, ordering, and filter examples

Configuration & Sync:
- implement global NEPKIT settings in conf.py (DEFAULT_LANGUAGE, ADMIN_DATEPICKER, etc.)
- universalize BS_DATE_FORMAT/BS_DATETIME_FORMAT across the entire stack
- synchronize frontend datepicker initialization with backend date formats

Documentation & DX:
- complete rewrite of README.md into high-energy, factual, English-only guides
- project-wide audit and simplification of comments/docstrings for beginner accessibility
- expanded 'Technical Design' and 'FAQ' sections covering data integrity and storage
- added docs/migration_guide.py for bulk AD-to-BS data transitions

Quality & Maintenance:
- fixed all E402 module-level import errors across the library
- verified codebase with project-wide ruff checks (all checks passed)
2026-01-30 17:00:33 +05:45

128 lines
3.6 KiB
Python

from __future__ import annotations
from typing import Any, Optional, Type
from nepali.datetime import nepalidate, nepalidatetime
try:
from rest_framework import serializers
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
"django-nepkit DRF support is optional. Install with `django-nepkit[drf]` "
"to use `django_nepkit.serializers`."
) from e
from django_nepkit.conf import nepkit_settings
from django_nepkit.utils import (
BS_DATE_FORMAT,
BS_DATETIME_FORMAT,
try_parse_nepali_date,
try_parse_nepali_datetime,
)
class BaseNepaliBSField(serializers.Field):
"""
Handles Nepali (BS) dates.
- Input: A date string (like '2080-01-01') or a date object.
- Output: A formatted string for your API.
"""
format: str = ""
nepali_type: Type[object] = object
default_error_messages = {
"invalid": "Invalid Bikram Sambat value. Expected format: {format}.",
"invalid_type": "Invalid type. Expected a string.",
}
def __init__(
self,
*,
format: Optional[str] = None,
ne: Optional[bool] = None,
en: Optional[bool] = None,
**kwargs: Any,
) -> None:
"""
Args:
format: Optional `strftime` format used for representation.
If not provided, uses the class default.
ne: If True, output in Devanagari script. If None, uses DEFAULT_LANGUAGE.
en: If True, output in English. If None, derived from ne.
"""
if format is not None:
self.format = format
default_lang = nepkit_settings.DEFAULT_LANGUAGE
if ne is None:
self.ne = default_lang == "ne"
else:
self.ne = ne
if en is None:
self.en = not self.ne
else:
self.en = en
super().__init__(**kwargs)
def _parse(self, value: str):
if self.nepali_type is nepalidate:
return try_parse_nepali_date(value)
if self.nepali_type is nepalidatetime:
return try_parse_nepali_datetime(value)
return None
def _format_value(self, value: Any) -> str:
"""Format value using strftime or strftime_ne (Devanagari) based on self.ne."""
if self.ne and hasattr(value, "strftime_ne"):
return value.strftime_ne(self.format) # type: ignore[attr-defined]
return value.strftime(self.format) # type: ignore[attr-defined]
def to_representation(self, value: Any) -> Optional[str]:
if value is None:
return None
if isinstance(value, self.nepali_type):
return self._format_value(value)
# Convert string dates to Nepali date objects
if isinstance(value, str):
parsed = self._parse(value)
if parsed is not None:
return self._format_value(parsed)
return str(value)
def to_internal_value(self, data: Any):
if data in (None, ""):
return None
if isinstance(data, self.nepali_type):
return data
if not isinstance(data, str):
self.fail("invalid_type")
parsed = self._parse(data)
if parsed is not None:
return parsed
self.fail("invalid", format=self.format)
class NepaliDateSerializerField(BaseNepaliBSField):
"""API field for Nepali Dates."""
format = BS_DATE_FORMAT
nepali_type = nepalidate
class NepaliDateTimeSerializerField(BaseNepaliBSField):
"""API field for Nepali Date and Time."""
format = BS_DATETIME_FORMAT
nepali_type = nepalidatetime