Store date(times) in ISO formats and UTC

This commit is contained in:
Raphael Michel
2019-03-27 18:43:44 +01:00
committed by johan12345
parent ef55a018f8
commit 5ec8c7ed96
3 changed files with 59 additions and 5 deletions
+1 -1
View File
@@ -263,7 +263,7 @@ class BaseQuestionsForm(forms.Form):
label=label, required=required,
help_text=help_text,
initial=dateutil.parser.parse(initial.answer).astimezone(tz) if initial and initial.answer else (
dateutil.parser.parse(default) if default else None),
dateutil.parser.parse(default).astimezone(tz) if default else None),
widget=SplitDateTimePickerWidget(time_format=get_format_without_seconds('TIME_INPUT_FORMATS')),
)
field.question = q
@@ -8,7 +8,7 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('pretixbase', '0092_auto_20180511_1224'),
('pretixbase', '0095_auto_20180604_1129'),
]
operations = [
+57 -3
View File
@@ -1,9 +1,15 @@
import copy
import datetime
import dateutil
import pytz
from django import forms
from django.core.exceptions import ValidationError
from django.db.models import Max
from django.forms.formsets import DELETION_FIELD_NAME
from django.forms.utils import ErrorList
from django.forms.utils import from_current_timezone, to_current_timezone
from django.urls import reverse
from django.utils import formats
from django.utils.translation import (
pgettext_lazy, ugettext as __, ugettext_lazy as _,
)
@@ -38,11 +44,57 @@ class AdjustableTypeField(forms.Textarea):
def __init__(self, *args, type=None, **kwargs):
super().__init__(*args, **kwargs)
def format_value(self, value):
if getattr(self, 'type') == 'W' and value:
return str(formats.localize_input(to_current_timezone(dateutil.parser.parse(value))))
elif getattr(self, 'type') == 'D' and value:
return str(formats.localize_input(dateutil.parser.parse(value).date()))
elif getattr(self, 'type') == 'T' and value:
return str(formats.localize_input(dateutil.parser.parse(value).time()))
return super().format_value(value)
def value_from_datadict(self, data, files, name):
if 'type' in data and data['type'] == 'W' and 'default_value_date' in data and 'default_value_time' in data:
return '{} {}'.format(data['default_value_date'], data['default_value_time'])
d_date = d_time = None
for format in formats.get_format('DATE_INPUT_FORMATS'):
try:
d_date = datetime.datetime.strptime(data['default_value_date'], format).date()
break
except (ValueError, TypeError):
continue
for format in formats.get_format('TIME_INPUT_FORMATS'):
try:
d_time = datetime.datetime.strptime(data['default_value_time'], format).time()
break
except (ValueError, TypeError):
continue
if d_date and d_time:
return from_current_timezone(datetime.datetime.combine(d_date, d_time)).astimezone(pytz.UTC).isoformat()
else:
return None
else:
return super().value_from_datadict(data, files, name)
val = super().value_from_datadict(data, files, name)
if 'type' in data and data['type'] == 'D' and val:
for format in formats.get_format('DATE_INPUT_FORMATS'):
try:
d_date = datetime.datetime.strptime(val, format).date()
val = d_date.isoformat()
break
except (ValueError, TypeError):
continue
elif 'type' in data and data['type'] == 'T' and val:
for format in formats.get_format('TIME_INPUT_FORMATS'):
try:
d_date = datetime.datetime.strptime(val, format).time()
val = d_date.isoformat()
break
except (ValueError, TypeError):
continue
return val
class QuestionForm(I18nModelForm):
@@ -63,6 +115,8 @@ class QuestionForm(I18nModelForm):
pk=self.instance.pk
)
self.fields['identifier'].required = False
if self.instance:
self.fields['default_value'].widget.type = self.instance.type
self.fields['help_text'].widget.attrs['rows'] = 3
def clean_dependency_question(self):