Projet

Général

Profil

0001-misc-switch-date-time-widget-to-template-rendering-2.patch

Frédéric Péters, 29 novembre 2018 15:32

Télécharger (6,09 ko)

Voir les différences:

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
wcs/qommon/form.py
885 885

  
886 886
class DateWidget(StringWidget):
887 887
    '''StringWidget which checks the value entered is a correct date'''
888
    template_name = 'qommon/forms/widgets/date.html'
889

  
888 890
    minimum_date = None
889 891
    maximum_date = None
890 892
    content_extra_css_class = 'date'
891 893

  
892 894
    def __init__(self, name, value=None, **kwargs):
893
        self.minimum_date = None
894

  
895
        if kwargs.get('minimum_date'):
896
            self.minimum_date = misc.get_as_datetime(kwargs.get('minimum_date')).timetuple()[:3]
897
            del kwargs['minimum_date']
898
        if kwargs.get('maximum_date'):
899
            self.maximum_date = misc.get_as_datetime(kwargs.get('maximum_date')).timetuple()[:3]
900
            del kwargs['maximum_date']
901
        if kwargs.get('minimum_is_future'):
895
        minimum_date = kwargs.pop('minimum_date', None)
896
        if minimum_date:
897
            self.minimum_date = misc.get_as_datetime(minimum_date).timetuple()[:3]
898
        maximum_date = kwargs.pop('maximum_date', None)
899
        if maximum_date:
900
            self.maximum_date = misc.get_as_datetime(maximum_date).timetuple()[:3]
901
        if kwargs.pop('minimum_is_future', False):
902 902
            if kwargs.get('date_can_be_today'):
903 903
                self.minimum_date = time.localtime()
904 904
            else:
905 905
                self.minimum_date = (datetime.datetime.today() + datetime.timedelta(1)).timetuple()
906
        if kwargs.get('date_in_the_past'):
906
        if kwargs.pop('date_in_the_past', False):
907 907
            if kwargs.get('date_can_be_today'):
908 908
                self.maximum_date = time.localtime()
909 909
            else:
910 910
                self.maximum_date = (datetime.datetime.today() - datetime.timedelta(1)).timetuple()
911 911

  
912
        if 'minimum_is_future' in kwargs:
913
            del kwargs['minimum_is_future']
914
        if 'date_in_the_past' in kwargs:
915
            del kwargs['date_in_the_past']
916 912
        if 'date_can_be_today' in kwargs:
917 913
            del kwargs['date_can_be_today']
918 914

  
......
967 963

  
968 964
        get_response().add_css_include('datetimepicker.css')
969 965

  
970
    def render_content(self):
971
        self.attrs['id'] = 'form_%s' % self.name
972

  
973
        if self.attrs.get('readonly'):
974
            return StringWidget.render_content(self)
975

  
976
        self.attrs['class'] = 'date-pick'
977
        date_format = self.get_format_string().replace('%Y', 'yyyy').replace(
966
    def date_format(self):
967
        return self.get_format_string().replace('%Y', 'yyyy').replace(
978 968
                '%m', 'mm').replace('%d', 'dd').replace('%H', 'hh').replace(
979 969
                '%M',  'ii').replace('%S', 'ss')
980 970

  
981
        self.attrs['data-date-format'] = date_format
982
        self.attrs['data-min-view'] = '0'
983
        self.attrs['data-start-view'] = '2'
984
        if not 'hh' in date_format:
985
            # if the date format doesn't contain the time, set widget not to go
986
            # into the time pages
987
            self.attrs['data-min-view'] = '2'
988
        if not self.value:
989
            # if there's no value we set the initial view to be the view of
990
            # decades, it's more appropriate to select a far away date.
991
            self.attrs['data-start-view'] = '4'
992

  
993
        if self.minimum_date:
994
            start_date = date_format.replace(
971
    def start_date(self):
972
        return self.date_format().replace(
995 973
                    'yyyy', '%04d' % self.minimum_date[0]).replace(
996 974
                    'mm', '%02d' % self.minimum_date[1]).replace(
997 975
                    'dd', '%02d' % self.minimum_date[2]).replace(
998 976
                    'hh', '00').replace('ii', '00').replace('ss', '00')
999
            self.attrs['data-start-date'] = start_date
1000 977

  
1001
        if self.maximum_date:
1002
            end_date = date_format.replace(
978
    def end_date(self):
979
        return self.date_format().replace(
1003 980
                    'yyyy', '%04d' % self.maximum_date[0]).replace(
1004 981
                    'mm', '%02d' % self.maximum_date[1]).replace(
1005 982
                    'dd', '%02d' % self.maximum_date[2]).replace(
1006 983
                    'hh', '00').replace('ii', '00').replace('ss', '00')
1007
            self.attrs['data-end-date'] = end_date
1008

  
1009
        return StringWidget.render_content(self)
1010 984

  
1011 985

  
1012 986
class DateTimeWidget(DateWidget):
wcs/qommon/templates/qommon/forms/widgets/date.html
1
{% extends "qommon/forms/widget.html" %}
2

  
3
{% block widget-control %}
4
<input id="form_{{widget.name}}" name="{{widget.name}}"
5
  {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}"{% endfor %}
6
  {% if widget.required %}aria-required="true"{% endif %}
7
  {% if widget.value %}value="{{ widget.value }}"{% endif %}
8
  {% if not "readonly" in widget.attrs %}
9
  class="date-pick"
10
  data-date-format="{{widget.date_format}}"
11

  
12
  {# if the date format does not contain the time, set widget not to go #}
13
  {# into the time pages #}
14
  data-min-view="{% if 'hh' in widget.date_format %}0{% else %}2{% endif %}"
15

  
16
  {# if there is no value we set the initial view to be the view of decades, #}
17
  {# it is more appropriate to select a far away date. #}
18
  data-start-view="{% if widget.value %}2{% else %}4{% endif %}"
19

  
20
  {% if widget.minimum_date %}data-start-date="{{ widget.start_date }}"{% endif %}
21
  {% if widget.maximum_date %}data-end-date="{{ widget.end_date }}"{% endif %}
22
  {% endif %}
23
  >
24
{% endblock %}
0
-