mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-06-08 15:42:58 +02:00
refactor: move some fields and widgets around
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import datetime
|
||||
|
||||
from django import forms
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from apps.common.widgets.month_year import MonthYearWidget
|
||||
|
||||
|
||||
class MonthYearModelField(models.DateField):
|
||||
def to_python(self, value):
|
||||
if value is None or isinstance(value, datetime.date):
|
||||
return value
|
||||
|
||||
try:
|
||||
# Parse the input as year-month
|
||||
date = datetime.datetime.strptime(value, "%Y-%m")
|
||||
# Set the day to 1
|
||||
return date.replace(day=1).date()
|
||||
except ValueError:
|
||||
raise ValidationError("Invalid date format. Use YYYY-MM.")
|
||||
|
||||
def formfield(self, **kwargs):
|
||||
kwargs["widget"] = MonthYearWidget
|
||||
kwargs["form_class"] = MonthYearFormField
|
||||
return super().formfield(**kwargs)
|
||||
|
||||
|
||||
class MonthYearFormField(forms.DateField):
|
||||
widget = MonthYearWidget
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.input_formats = ["%Y-%m"]
|
||||
|
||||
def to_python(self, value):
|
||||
if value in self.empty_values:
|
||||
return None
|
||||
if isinstance(value, datetime.datetime):
|
||||
return value.date()
|
||||
try:
|
||||
date = datetime.datetime.strptime(value, "%Y-%m")
|
||||
return date.replace(day=1).date()
|
||||
except ValueError:
|
||||
raise ValidationError(_("Invalid date format. Use YYYY-MM."))
|
||||
|
||||
def prepare_value(self, value):
|
||||
if isinstance(value, datetime.date):
|
||||
return value.strftime("%Y-%m")
|
||||
return value
|
||||
@@ -0,0 +1,71 @@
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
from django import forms
|
||||
from django.utils.formats import get_format, number_format
|
||||
|
||||
|
||||
def convert_to_decimal(value: str):
|
||||
# Remove any whitespace
|
||||
value = value.strip()
|
||||
|
||||
# Get the thousand and decimal separators from Django's localization settings
|
||||
thousands_sep = get_format("THOUSAND_SEPARATOR")
|
||||
decimal_sep = get_format("DECIMAL_SEPARATOR")
|
||||
|
||||
# Remove thousands separators and replace decimal separator with '.'
|
||||
value = value.replace(thousands_sep, "")
|
||||
value = value.replace(decimal_sep, ".")
|
||||
|
||||
# Convert to Decimal
|
||||
if value:
|
||||
return Decimal(value)
|
||||
return None
|
||||
|
||||
|
||||
class ArbitraryDecimalDisplayNumberInput(forms.TextInput):
|
||||
"""A widget for displaying and inputing decimal numbers with the least amount of trailing zeros possible. You
|
||||
must set this on your Form's __init__ method."""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.decimal_places = kwargs.pop("decimal_places", None)
|
||||
self.type = "text"
|
||||
super().__init__(*args, **kwargs)
|
||||
self.attrs.update(
|
||||
{
|
||||
"x-mask:dynamic": f"$money($input, '{get_format('DECIMAL_SEPARATOR')}', "
|
||||
f"'{get_format('THOUSAND_SEPARATOR')}', '30')"
|
||||
}
|
||||
)
|
||||
|
||||
def format_value(self, value):
|
||||
if value is not None and isinstance(value, (Decimal, float, str)):
|
||||
try:
|
||||
# Convert to Decimal if it's a float or string
|
||||
if isinstance(value, float):
|
||||
value = Decimal(value)
|
||||
elif isinstance(value, str):
|
||||
value = Decimal(convert_to_decimal(value))
|
||||
|
||||
# Remove trailing zeros
|
||||
value = value.normalize()
|
||||
|
||||
# Format the number using Django's localization
|
||||
formatted_value = number_format(
|
||||
value,
|
||||
force_grouping=False,
|
||||
decimal_pos=self.decimal_places,
|
||||
)
|
||||
|
||||
return formatted_value
|
||||
except (InvalidOperation, ValueError):
|
||||
# If there's an error in conversion, return the original value
|
||||
return value
|
||||
return value
|
||||
|
||||
def value_from_datadict(self, data, files, name):
|
||||
value = super().value_from_datadict(data, files, name)
|
||||
if value is not None:
|
||||
# Remove any non-numeric characters except for the decimal point
|
||||
value = convert_to_decimal(value)
|
||||
|
||||
return value
|
||||
@@ -0,0 +1,16 @@
|
||||
from datetime import datetime, date
|
||||
|
||||
from django import forms
|
||||
|
||||
|
||||
class MonthYearWidget(forms.DateInput):
|
||||
"""
|
||||
Custom widget to display a month-year picker.
|
||||
"""
|
||||
|
||||
input_type = "month" # Set the input type to 'month'
|
||||
|
||||
def format_value(self, value):
|
||||
if isinstance(value, (datetime, date)):
|
||||
return value.strftime("%Y-%m")
|
||||
return value
|
||||
Reference in New Issue
Block a user