From dcf2c3b2d08654e8cc2c86522444af826c251870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 29 Nov 2018 14:27:23 +0100 Subject: [PATCH] misc: switch date(/time) widget to template rendering (#28460) --- wcs/qommon/form.py | 58 +++++-------------- .../templates/qommon/forms/widgets/date.html | 24 ++++++++ 2 files changed, 40 insertions(+), 42 deletions(-) create mode 100644 wcs/qommon/templates/qommon/forms/widgets/date.html diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 400fc019b..890c30869 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -885,34 +885,30 @@ class WcsExtraStringWidget(StringWidget): class DateWidget(StringWidget): '''StringWidget which checks the value entered is a correct date''' + template_name = 'qommon/forms/widgets/date.html' + minimum_date = None maximum_date = None content_extra_css_class = 'date' def __init__(self, name, value=None, **kwargs): - self.minimum_date = None - - if kwargs.get('minimum_date'): - self.minimum_date = misc.get_as_datetime(kwargs.get('minimum_date')).timetuple()[:3] - del kwargs['minimum_date'] - if kwargs.get('maximum_date'): - self.maximum_date = misc.get_as_datetime(kwargs.get('maximum_date')).timetuple()[:3] - del kwargs['maximum_date'] - if kwargs.get('minimum_is_future'): + minimum_date = kwargs.pop('minimum_date', None) + if minimum_date: + self.minimum_date = misc.get_as_datetime(minimum_date).timetuple()[:3] + maximum_date = kwargs.pop('maximum_date', None) + if maximum_date: + self.maximum_date = misc.get_as_datetime(maximum_date).timetuple()[:3] + if kwargs.pop('minimum_is_future', False): if kwargs.get('date_can_be_today'): self.minimum_date = time.localtime() else: self.minimum_date = (datetime.datetime.today() + datetime.timedelta(1)).timetuple() - if kwargs.get('date_in_the_past'): + if kwargs.pop('date_in_the_past', False): if kwargs.get('date_can_be_today'): self.maximum_date = time.localtime() else: self.maximum_date = (datetime.datetime.today() - datetime.timedelta(1)).timetuple() - if 'minimum_is_future' in kwargs: - del kwargs['minimum_is_future'] - if 'date_in_the_past' in kwargs: - del kwargs['date_in_the_past'] if 'date_can_be_today' in kwargs: del kwargs['date_can_be_today'] @@ -967,46 +963,24 @@ class DateWidget(StringWidget): get_response().add_css_include('datetimepicker.css') - def render_content(self): - self.attrs['id'] = 'form_%s' % self.name - - if self.attrs.get('readonly'): - return StringWidget.render_content(self) - - self.attrs['class'] = 'date-pick' - date_format = self.get_format_string().replace('%Y', 'yyyy').replace( + def date_format(self): + return self.get_format_string().replace('%Y', 'yyyy').replace( '%m', 'mm').replace('%d', 'dd').replace('%H', 'hh').replace( '%M', 'ii').replace('%S', 'ss') - self.attrs['data-date-format'] = date_format - self.attrs['data-min-view'] = '0' - self.attrs['data-start-view'] = '2' - if not 'hh' in date_format: - # if the date format doesn't contain the time, set widget not to go - # into the time pages - self.attrs['data-min-view'] = '2' - if not self.value: - # if there's no value we set the initial view to be the view of - # decades, it's more appropriate to select a far away date. - self.attrs['data-start-view'] = '4' - - if self.minimum_date: - start_date = date_format.replace( + def start_date(self): + return self.date_format().replace( 'yyyy', '%04d' % self.minimum_date[0]).replace( 'mm', '%02d' % self.minimum_date[1]).replace( 'dd', '%02d' % self.minimum_date[2]).replace( 'hh', '00').replace('ii', '00').replace('ss', '00') - self.attrs['data-start-date'] = start_date - if self.maximum_date: - end_date = date_format.replace( + def end_date(self): + return self.date_format().replace( 'yyyy', '%04d' % self.maximum_date[0]).replace( 'mm', '%02d' % self.maximum_date[1]).replace( 'dd', '%02d' % self.maximum_date[2]).replace( 'hh', '00').replace('ii', '00').replace('ss', '00') - self.attrs['data-end-date'] = end_date - - return StringWidget.render_content(self) class DateTimeWidget(DateWidget): diff --git a/wcs/qommon/templates/qommon/forms/widgets/date.html b/wcs/qommon/templates/qommon/forms/widgets/date.html new file mode 100644 index 000000000..485eb5452 --- /dev/null +++ b/wcs/qommon/templates/qommon/forms/widgets/date.html @@ -0,0 +1,24 @@ +{% extends "qommon/forms/widget.html" %} + +{% block widget-control %} + +{% endblock %} -- 2.20.0.rc1