Skip to content
Snippets Groups Projects
Unverified Commit 9a02136d authored by RunasSudo's avatar RunasSudo
Browse files

Add start/stop at form fields

parent 41c96ac7
No related branches found
No related tags found
No related merge requests found
......@@ -24,7 +24,7 @@ class SplitDateTimeField(fields.MultiValueField):
"""
if data_list:
if not (data_list[0] and data_list[1]):
raise forms.ValidationError("Field is missing data.")
return None
return datetime.datetime.combine(*data_list)
return None
......@@ -23,13 +23,11 @@ class ElectionForm(forms.Form):
if settings.ALLOW_ELECTION_INFO_URL:
election_info_url = forms.CharField(required=False, initial="", label="Election Info Download URL", help_text="the URL of a PDF document that contains extra election information, e.g. candidate bios and statements")
class ElectionTimesForm(forms.Form):
# times
voting_starts_at = SplitDateTimeField(help_text = 'UTC date and time when voting begins',
widget=SplitSelectDateTimeWidget)
widget=SplitSelectDateTimeWidget, required=False)
voting_ends_at = SplitDateTimeField(help_text = 'UTC date and time when voting ends',
widget=SplitSelectDateTimeWidget)
widget=SplitSelectDateTimeWidget, required=False)
class EmailVotersForm(forms.Form):
......
......@@ -157,8 +157,8 @@ class SelectTimeWidget(Widget):
elif meridiem.lower().startswith('a') and int(h) == 12:
h = 0
if (int(h) == 0 or h) and m and s:
return '%s:%s:%s' % (h, m, s)
if (int(h) == 0 or h) and m:
return '%s:%s' % (h, m)
return data.get(name, None)
......@@ -174,6 +174,12 @@ class SplitSelectDateTimeWidget(MultiWidget):
widgets = (SelectDateWidget(attrs=attrs, years=years), SelectTimeWidget(attrs=attrs, hour_step=hour_step, minute_step=minute_step, twelve_hr=twelve_hr))
super(SplitSelectDateTimeWidget, self).__init__(widgets, attrs)
# See https://stackoverflow.com/questions/4324676/django-multiwidget-subclass-not-calling-decompress
def value_from_datadict(self, data, files, name):
if data.get(name, None) is None:
return [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
return self.decompress(data.get(name, None))
def decompress(self, value):
if value:
return [value.date(), value.time().replace(microsecond=0)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment