/
datetime.py
403 lines (335 loc) · 14.1 KB
/
datetime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# This file is part of Indico.
# Copyright (C) 2002 - 2020 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.
from __future__ import absolute_import, unicode_literals
import json
from collections import OrderedDict
from datetime import time, timedelta
import dateutil.parser
import pytz
from flask import session
from markupsafe import escape
from wtforms import Field, SelectField
from wtforms.ext.dateutil.fields import DateField, DateTimeField
from wtforms.validators import StopValidation
from indico.core.config import config
from indico.util.date_time import localize_as_utc, relativedelta
from indico.util.i18n import _, get_current_locale
from indico.web.forms.fields import JSONField
from indico.web.forms.validators import DateRange, DateTimeRange, LinkedDate, LinkedDateTime
from indico.web.forms.widgets import JinjaWidget
class TimeDeltaField(Field):
"""A field that lets the user select a simple timedelta.
It does not support mixing multiple units, but it is smart enough
to switch to a different unit to represent a timedelta that could
not be represented otherwise.
:param units: The available units. Must be a tuple containing any
any of 'seconds', 'minutes', 'hours' and 'days'.
If not specified, ``('hours', 'days')`` is assumed.
"""
widget = JinjaWidget('forms/timedelta_widget.html', single_line=True, single_kwargs=True)
# XXX: do not translate, "Minutes" is ambiguous without context
unit_names = {
'seconds': 'Seconds',
'minutes': 'Minutes',
'hours': 'Hours',
'days': 'Days'
}
magnitudes = OrderedDict([
('days', 86400),
('hours', 3600),
('minutes', 60),
('seconds', 1)
])
def __init__(self, *args, **kwargs):
self.units = kwargs.pop('units', ('hours', 'days'))
super(TimeDeltaField, self).__init__(*args, **kwargs)
@property
def best_unit(self):
"""Return the largest unit that covers the current timedelta"""
if self.data is None:
return None
seconds = int(self.data.total_seconds())
for unit, magnitude in self.magnitudes.iteritems():
if not seconds % magnitude:
return unit
return 'seconds'
@property
def choices(self):
best_unit = self.best_unit
choices = [(unit, self.unit_names[unit]) for unit in self.units]
# Add whatever unit is necessary to represent the currenet value if we have one
if best_unit and best_unit not in self.units:
choices.append((best_unit, '({})'.format(self.unit_names[best_unit])))
return choices
def process_formdata(self, valuelist):
if valuelist and len(valuelist) == 2:
value = int(valuelist[0])
unit = valuelist[1]
if unit not in self.magnitudes:
raise ValueError('Invalid unit')
self.data = timedelta(seconds=self.magnitudes[unit] * value)
def pre_validate(self, form):
if self.best_unit in self.units:
return
if self.object_data is None:
raise ValueError(_('Please choose a valid unit.'))
if self.object_data != self.data:
raise ValueError(_('Please choose a different unit or keep the previous value.'))
def _value(self):
if self.data is None:
return '', ''
else:
return int(self.data.total_seconds()) // self.magnitudes[self.best_unit], self.best_unit
class RelativeDeltaField(Field):
"""A field that lets the user select a simple timedelta.
It does not support mixing multiple units, but it is smart enough
to switch to a different unit to represent a timedelta that could
not be represented otherwise.
:param units: The available units. Must be a tuple containing any
any of 'seconds', 'minutes', 'hours' and 'days'.
If not specified, ``('hours', 'days')`` is assumed.
"""
widget = JinjaWidget('forms/timedelta_widget.html', single_line=True, single_kwargs=True)
# XXX: do not translate, "Minutes" is ambiguous without context
unit_names = {
'seconds': 'Seconds',
'minutes': 'Minutes',
'hours': 'Hours',
'days': 'Days',
'weeks': 'Weeks',
'months': 'Months',
'years': 'Years'
}
magnitudes = OrderedDict([
('years', relativedelta(years=1)),
('months', relativedelta(months=1)),
('weeks', relativedelta(weeks=1)),
('days', relativedelta(days=1)),
('hours', relativedelta(hours=1)),
('minutes', relativedelta(minutes=1)),
('seconds', relativedelta(seconds=1))
])
def __init__(self, *args, **kwargs):
self.units = kwargs.pop('units', ('hours', 'days'))
super(RelativeDeltaField, self).__init__(*args, **kwargs)
@property
def split_data(self):
if self.data is None:
return None, None
for unit in self.magnitudes:
number = getattr(self.data, unit)
if number:
return number, unit
raise ValueError('Unsupported relativedelta() unit')
@property
def choices(self):
choices = [(unit, self.unit_names[unit]) for unit in self.units]
number, unit = self.split_data
if number is not None and unit not in self.units:
# Add whatever unit is necessary to represent the currenet value if we have one
choices.append((unit, '({})'.format(self.unit_names[unit])))
return choices
def process_formdata(self, valuelist):
if valuelist and len(valuelist) == 2:
value = int(valuelist[0])
unit = valuelist[1]
if unit not in self.magnitudes:
raise ValueError('Invalid unit')
self.data = (self.magnitudes[unit] * value).normalized()
def pre_validate(self, form):
if self.object_data is None:
raise ValueError(_('Please choose a valid unit.'))
def _value(self):
if self.data is None:
return '', ''
return self.split_data
class IndicoDateField(DateField):
widget = JinjaWidget('forms/date_widget.html', single_line=True, single_kwargs=True)
def __init__(self, *args, **kwargs):
self.allow_clear = kwargs.pop('allow_clear', True)
super(IndicoDateField, self).__init__(*args, parse_kwargs={'dayfirst': True},
display_format='%d/%m/%Y', **kwargs)
@property
def earliest_date(self):
if self.flags.date_range:
for validator in self.validators:
if isinstance(validator, DateRange):
return validator.get_earliest(self.get_form(), self)
@property
def latest_date(self):
if self.flags.date_range:
for validator in self.validators:
if isinstance(validator, DateRange):
return validator.get_latest(self.get_form(), self)
@property
def linked_field(self):
validator = self.linked_date_validator
return validator.linked_field if validator else None
@property
def linked_date_validator(self):
if self.flags.linked_date:
for validator in self.validators:
if isinstance(validator, LinkedDate):
return validator
class IndicoDateTimeField(DateTimeField):
"""Friendly datetime field that handles timezones and validations.
Important: When the form has a `timezone` field it must be
declared before any `IndicoDateTimeField`. Otherwise its
value is not available in this field resulting in an error
during form submission.
"""
widget = JinjaWidget('forms/datetime_widget.html', single_line=True)
def __init__(self, *args, **kwargs):
self._timezone = kwargs.pop('timezone', None)
self.default_time = kwargs.pop('default_time', time(0, 0))
self.date_missing = False
self.time_missing = False
self.allow_clear = kwargs.pop('allow_clear', True)
super(IndicoDateTimeField, self).__init__(*args, parse_kwargs={'dayfirst': True}, **kwargs)
def pre_validate(self, form):
if self.date_missing:
raise StopValidation(_("Date must be specified"))
if self.time_missing:
raise StopValidation(_("Time must be specified"))
if self.object_data:
# Normalize datetime resolution of passed data
self.object_data = self.object_data.replace(second=0, microsecond=0)
def process_formdata(self, valuelist):
if any(valuelist):
if not valuelist[0]:
self.date_missing = True
if not valuelist[1]:
self.time_missing = True
if valuelist:
valuelist = [' '.join(valuelist).strip()]
super(IndicoDateTimeField, self).process_formdata(valuelist)
if self.data and not self.data.tzinfo:
self.data = localize_as_utc(self.data, self.timezone)
@property
def earliest_dt(self):
if self.flags.datetime_range:
for validator in self.validators:
if isinstance(validator, DateTimeRange):
return validator.get_earliest(self.get_form(), self)
@property
def latest_dt(self):
if self.flags.datetime_range:
for validator in self.validators:
if isinstance(validator, DateTimeRange):
return validator.get_latest(self.get_form(), self)
@property
def linked_datetime_validator(self):
if self.flags.linked_datetime:
for validator in self.validators:
if isinstance(validator, LinkedDateTime):
return validator
@property
def linked_field(self):
validator = self.linked_datetime_validator
return validator.linked_field if validator else None
@property
def timezone_field(self):
field = getattr(self.get_form(), 'timezone', None)
return field if isinstance(field, SelectField) else None
@property
def timezone(self):
if self._timezone:
return self._timezone
elif self.timezone_field:
return self.timezone_field.data
else:
form = self.get_form()
if form and hasattr(form, 'timezone'):
return form.timezone
return session.tzinfo.zone
@property
def tzinfo(self):
return pytz.timezone(self.timezone)
class OccurrencesField(JSONField):
"""
A field that lets you select multiple occurrences consisting of a
start date/time and a duration.
"""
widget = JinjaWidget('forms/occurrences_widget.html', single_line=True)
CAN_POPULATE = True
def __init__(self, *args, **kwargs):
self._timezone = kwargs.pop('timezone', None)
kwargs.setdefault('default', [])
super(OccurrencesField, self).__init__(*args, **kwargs)
def process_formdata(self, valuelist):
def _deserialize(occ):
try:
dt = dateutil.parser.parse('{} {}'.format(occ['date'], occ['time']))
except ValueError:
raise ValueError('Invalid date/time: {} {}'.format(escape(occ['date']), escape(occ['time'])))
if occ['duration'] < 1:
raise ValueError('Invalid duration')
return localize_as_utc(dt, self.timezone), timedelta(minutes=occ['duration'])
self.data = []
super(OccurrencesField, self).process_formdata(valuelist)
self.data = map(_deserialize, self.data)
def _value(self):
def _serialize(occ):
if isinstance(occ, dict):
# raw data from the client
return occ
dt = occ[0].astimezone(pytz.timezone(self.timezone))
return {'date': dt.date().isoformat(),
'time': dt.time().isoformat()[:-3], # hh:mm only
'duration': int(occ[1].total_seconds() // 60)}
return json.dumps(map(_serialize, self.data))
@property
def timezone_field(self):
field = getattr(self.get_form(), 'timezone', None)
return field if isinstance(field, SelectField) else None
@property
def timezone(self):
if self.timezone_field:
return self.timezone_field.data
else:
return getattr(self.get_form(), 'timezone', session.tzinfo.zone)
class IndicoTimezoneSelectField(SelectField):
def __init__(self, *args, **kwargs):
super(IndicoTimezoneSelectField, self).__init__(*args, **kwargs)
self.choices = [(v, v) for v in pytz.common_timezones]
self.default = config.DEFAULT_TIMEZONE
def process_data(self, value):
super(IndicoTimezoneSelectField, self).process_data(value)
if self.data is not None and self.data not in pytz.common_timezones_set:
self.choices.append((self.data, self.data))
class IndicoWeekDayRepetitionField(Field):
""" Field that lets you select an ordinal day of the week."""
widget = JinjaWidget('forms/week_day_repetition_widget.html', single_line=True)
WEEK_DAY_NUMBER_CHOICES = (
(1, _('first')),
(2, _('second')),
(3, _('third')),
(4, _('fourth')),
(-1, _('last'))
)
def __init__(self, *args, **kwargs):
locale = get_current_locale()
self.day_number_options = self.WEEK_DAY_NUMBER_CHOICES
self.week_day_options = [(n, locale.weekday(n, short=False)) for n in xrange(7)]
self.day_number_missing = False
self.week_day_missing = False
super(IndicoWeekDayRepetitionField, self).__init__(*args, **kwargs)
def process_formdata(self, valuelist):
self.data = ()
if any(valuelist):
if not valuelist[0]:
self.day_number_missing = True
if not valuelist[1]:
self.week_day_missing = True
if valuelist:
self.data = tuple(map(int, valuelist))
@property
def day_number_data(self):
return self.data[0] if len(self.data) > 0 else None
@property
def week_day_data(self):
return self.data[1] if len(self.data) > 1 else None