Clarify that None means “no opinion” versus False, and adjust fallback logic to use the project default when a field lacks the ne attribute.
54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
from datetime import date as python_date
|
|
|
|
from django import forms
|
|
from django.utils.translation import gettext_lazy as _
|
|
from nepali.datetime import nepalidate
|
|
from nepali.exceptions import FormatNotMatchException
|
|
|
|
from django_nepkit.utils import BS_DATE_FORMAT, try_parse_nepali_date
|
|
from django_nepkit.validators import validate_nepali_phone_number
|
|
from django_nepkit.widgets import NepaliDatePickerWidget
|
|
|
|
# Anything ``try_parse_nepali_date`` (or ``str(value)`` coercion) might
|
|
# raise that we want to translate into a user-friendly ``ValidationError``.
|
|
_PARSE_ERRORS = (ValueError, TypeError, FormatNotMatchException)
|
|
|
|
|
|
class NepaliDateFormField(forms.DateField):
|
|
"""A form field for entering Nepali dates."""
|
|
|
|
widget = NepaliDatePickerWidget
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
kwargs.pop("max_length", None)
|
|
# Clean up arguments not needed for base Field
|
|
kwargs.pop("empty_value", None)
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def to_python(self, value):
|
|
if value in self.empty_values:
|
|
return None
|
|
if isinstance(value, nepalidate):
|
|
return value
|
|
if isinstance(value, python_date):
|
|
return nepalidate.from_date(value)
|
|
try:
|
|
parsed = try_parse_nepali_date(str(value))
|
|
if parsed is not None:
|
|
return parsed
|
|
raise ValueError("Invalid BS date format")
|
|
except _PARSE_ERRORS:
|
|
raise forms.ValidationError(
|
|
_("Enter a valid Nepali date in %(format)s format.")
|
|
% {"format": BS_DATE_FORMAT},
|
|
code="invalid",
|
|
)
|
|
|
|
|
|
class NepaliPhoneNumberFormField(forms.CharField):
|
|
"""A form field for entering Nepali phone numbers."""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.validators.append(validate_nepali_phone_number)
|