use dry principle possible and added example project

This commit is contained in:
sankalp committed 2026-01-29 01:37:41 +05:45
1 parent 2bb6458c8b
commit 1a1dc3bee4
46 files changed
+3521 -772

No files matched your search

+57 -13
View File
@@ -1,5 +1,18 @@
from __future__ import annotations
from typing import Any, Optional, Type
from nepali.datetime import nepalidate, nepalidatetime
from rest_framework import serializers
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.utils import try_parse_nepali_date, try_parse_nepali_datetime
# --------------------------------------------------
# Base Serializer Field
@@ -9,34 +22,65 @@ from rest_framework import serializers
class BaseNepaliBSField(serializers.Field):
"""
Base DRF field for Nepali (BS) Date / DateTime.
- **Input**: BS string, or an already-parsed `nepalidate`/`nepalidatetime`
- **Output**: formatted BS string (configurable)
"""
format = None
nepali_type = None
error_messages = {"invalid": "Invalid Bikram Sambat format. Expected {format}."}
format: str = ""
nepali_type: Type[object] = object
def to_representation(self, value):
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, **kwargs: Any) -> None:
"""
Args:
format: Optional `strftime` format used for representation.
If not provided, uses the class default.
"""
if format is not None:
self.format = format
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 to_representation(self, value: Any) -> Optional[str]:
if value is None:
return None
if isinstance(value, self.nepali_type):
return value.strftime(self.format)
return value.strftime(self.format) # type: ignore[attr-defined]
# Fallback: DB string
# If DB returns string, try to normalize it.
if isinstance(value, str):
parsed = self._parse(value)
if parsed is not None:
return parsed.strftime(self.format) # type: ignore[attr-defined]
# Fallback: best-effort stringify (keeps behavior non-breaking)
return str(value)
def to_internal_value(self, data):
def to_internal_value(self, data: Any):
if data in (None, ""):
return None
if isinstance(data, self.nepali_type):
return data
if isinstance(data, str):
try:
return self.nepali_type.strptime(data.strip(), self.format)
except Exception:
self.fail("invalid", format=self.format)
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)