diff --git a/sigi/apps/utils/forms/__init__.py b/sigi/apps/utils/forms/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/utils/forms/fields.py b/sigi/apps/utils/forms/fields.py new file mode 100644 index 0000000..a0dda5a --- /dev/null +++ b/sigi/apps/utils/forms/fields.py @@ -0,0 +1,15 @@ +import datetime +from django.forms import fields +from django.utils.translation import gettext_lazy as _ +from sigi.apps.utils.forms import widgets + + +class MonthField(fields.DateField): + widget = widgets.MonthInput + input_formats = ["%Y-%m", "%m-%Y"] + default_error_messages = { + "invalid": _("Enter a valid month."), + } + + def strptime(self, value, format): + return datetime.datetime.strptime(value, format).date() diff --git a/sigi/apps/utils/forms/widgets.py b/sigi/apps/utils/forms/widgets.py new file mode 100644 index 0000000..51564e6 --- /dev/null +++ b/sigi/apps/utils/forms/widgets.py @@ -0,0 +1,9 @@ +from django.forms import widgets + + +class MonthInput(widgets.DateTimeBaseInput): + template_name = "utils/forms/widgets/month.html" + + def __init__(self, attrs=None, format=None): + super().__init__(attrs) + self.format = format or "%Y-%m" diff --git a/sigi/apps/utils/templates/utils/forms/widgets/month.html b/sigi/apps/utils/templates/utils/forms/widgets/month.html new file mode 100644 index 0000000..08d3441 --- /dev/null +++ b/sigi/apps/utils/templates/utils/forms/widgets/month.html @@ -0,0 +1 @@ +