From e0be05d902860a2924fc059ecec6665ff7e29658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Tue, 3 Dec 2024 12:58:57 -0300 Subject: [PATCH] =?UTF-8?q?Criar=20form=20field=20para=20m=C3=AAs=20e=20an?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/utils/forms/__init__.py | 0 sigi/apps/utils/forms/fields.py | 15 +++++++++++++++ sigi/apps/utils/forms/widgets.py | 9 +++++++++ .../templates/utils/forms/widgets/month.html | 1 + 4 files changed, 25 insertions(+) create mode 100644 sigi/apps/utils/forms/__init__.py create mode 100644 sigi/apps/utils/forms/fields.py create mode 100644 sigi/apps/utils/forms/widgets.py create mode 100644 sigi/apps/utils/templates/utils/forms/widgets/month.html 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 @@ +