feat: add Nepali currency support, number-to-words conversion, and REST API enhancements

- Implement NepaliCurrencyField for model-level currency handling.
- Add format_nepali_currency utility with Lakhs/Crores comma formatting and decimal support.
- Implement number_to_nepali_words and english_to_nepali_unicode helper functions.
- Add nepali_currency, nepali_words, and nepali_unicode template filters.
- Enhance REST API (DRF) support:
    - Added NepaliCurrencySerializerField for formatted currency output.
    - Added NepaliLocalizedSerializerMixin for automatic Devanagari field generation.
    - Added flexible NepaliCurrencyRangeFilter and NepaliDateRangeFilter.
- Integrated automatic currency formatting into NepaliModelAdmin.
- Expanded the example demo app with a Transaction model and updated filters.
- Added comprehensive unit tests for formatting, models, and API components.
- Updated documentation (README.md and todo.md) to reflect new features.
This commit is contained in:
sankalp committed 2026-01-30 20:06:39 +05:45
1 parent 12de34e354
commit c6301686cc
10 files changed
+295 -13

No files matched your search

+83
View File
@@ -16,6 +16,7 @@ from django_nepkit.conf import nepkit_settings
from django_nepkit.utils import (
BS_DATE_FORMAT,
BS_DATETIME_FORMAT,
format_nepali_currency,
try_parse_nepali_date,
try_parse_nepali_datetime,
)
@@ -125,3 +126,85 @@ class NepaliDateTimeSerializerField(BaseNepaliBSField):
format = BS_DATETIME_FORMAT
nepali_type = nepalidatetime
class NepaliCurrencySerializerField(serializers.Field):
"""
API field for Nepali Currency.
Formats decimal values with Nepali-style commas.
"""
def __init__(self, currency_symbol="Rs.", ne=None, **kwargs):
self.currency_symbol = currency_symbol
self.ne = ne
super().__init__(**kwargs)
def to_representation(self, value):
if value is None:
return None
ne = self.ne
if ne is None:
ne = self.context.get("ne", nepkit_settings.DEFAULT_LANGUAGE == "ne")
return format_nepali_currency(value, currency_symbol=self.currency_symbol, ne=ne)
def to_internal_value(self, data):
# We don't support converting back from formatted string to decimal here
# Users should send raw decimal/float values for input.
return data
class NepaliLocalizedSerializerMixin:
"""
A mixin for ModelSerializer that automatically adds localized counterparts
for eligible fields if `ne=True` is passed in the context.
Eligible fields: NepaliDateField, NepaliDateTimeField, NepaliCurrencyField.
Usage:
class MySerializer(NepaliLocalizedSerializerMixin, serializers.ModelSerializer):
...
"""
def to_representation(self, instance):
ret = super().to_representation(instance) # type: ignore[misc]
ne = self.context.get("ne", False)
if not ne:
return ret
from django_nepkit.models import (
NepaliCurrencyField,
NepaliDateField,
NepaliDateTimeField,
)
model = getattr(self.Meta, "model", None) # type: ignore[attr-defined]
if not model:
return ret
for field_name, value in list(ret.items()):
try:
model_field = model._meta.get_field(field_name)
localized_name = f"{field_name}_ne"
if localized_name in ret:
continue
if isinstance(model_field, NepaliDateField):
raw_val = getattr(instance, field_name)
if hasattr(raw_val, "strftime_ne"):
ret[localized_name] = raw_val.strftime_ne(BS_DATE_FORMAT)
elif isinstance(model_field, NepaliDateTimeField):
raw_val = getattr(instance, field_name)
if hasattr(raw_val, "strftime_ne"):
ret[localized_name] = raw_val.strftime_ne(BS_DATETIME_FORMAT)
elif isinstance(model_field, NepaliCurrencyField):
ret[localized_name] = format_nepali_currency(
value, currency_symbol="", ne=True
)
except Exception:
continue
return ret