Browse Source

Criar form field para mês e ano

pull/184/head
Sesóstris Vieira 3 weeks ago
parent
commit
e0be05d902
  1. 0
      sigi/apps/utils/forms/__init__.py
  2. 15
      sigi/apps/utils/forms/fields.py
  3. 9
      sigi/apps/utils/forms/widgets.py
  4. 1
      sigi/apps/utils/templates/utils/forms/widgets/month.html

0
sigi/apps/utils/forms/__init__.py

15
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()

9
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"

1
sigi/apps/utils/templates/utils/forms/widgets/month.html

@ -0,0 +1 @@
<input type="month" name="{{ widget.name }}" {% if widget.value != None %} value="{{ widget.value|date:'Y-m' }}"{% endif %} {% include "django/forms/widgets/attrs.html" %} style="width: max-content;">
Loading…
Cancel
Save