Projet

Général

Profil

0001-widgets-use-date-input-for-dates-41605.patch

Voir les différences:

Subject: [PATCH] widgets: use "date" input for dates (#41605)

 src/authentic2/attribute_kinds.py |  3 +++
 src/authentic2/forms/widgets.py   | 34 ++++++++++++++++++++++++++++---
 2 files changed, 34 insertions(+), 3 deletions(-)
src/authentic2/attribute_kinds.py
82 82

  
83 83
class BirthdateWidget(DateWidget):
84 84
    def __init__(self, *args, **kwargs):
85
        attrs = kwargs.setdefault('attrs', {})
85 86
        options = kwargs.setdefault('options', {})
86 87
        options['endDate'] = '-1d'
87 88
        options['startDate'] = '1900-01-01'
89
        attrs['min'] = '1900-01-01'
90
        attrs['max'] = (datetime.date.today() - datetime.timedelta(days=1)).isoformat()
88 91
        super(BirthdateWidget, self).__init__(*args, **kwargs)
89 92

  
90 93

  
src/authentic2/forms/widgets.py
26 26
import json
27 27
import re
28 28
import uuid
29
import datetime
29 30

  
30 31
import django
31 32
from django.forms.widgets import DateTimeInput, DateInput, TimeInput, ClearableFileInput
......
34 35
from django.utils.formats import get_language, get_format
35 36
from django.utils.safestring import mark_safe
36 37
from django.utils.translation import ugettext_lazy as _
38
from django.utils.encoding import force_text
37 39

  
38 40
from gadjo.templatetags.gadjo import xstatic
39 41

  
......
79 81
       </script>
80 82
       """
81 83

  
84
BOOTSTRAP_DATE_INPUT_TEMPLATE = """
85
      %(rendered_widget)s
86
      %(clear_button)s
87
      <span class="add-on"><i class="icon-th"></i></span>
88
      <span class="%(id)s helptext">%(help_text)s</span>
89
       <script type="text/javascript">
90
           if ($("#%(id)s").attr('type') != "date") {
91
               $("#%(id)s").datetimepicker({%(options)s});
92
               var date = new Date($("#%(id)s").val());
93
               $("#%(id)s").val(date.toLocaleDateString('%(language)s'));
94
           } else {
95
               $(".%(id)s.helptext").hide();
96
           }
97
       </script>
98
       """
99

  
82 100
CLEAR_BTN_TEMPLATE = """<span class="add-on"><i class="icon-remove"></i></span>"""
83 101

  
84 102

  
......
98 116
    glyphicon = None
99 117
    help_text = None
100 118

  
119
    render_template = BOOTSTRAP_INPUT_TEMPLATE
120

  
101 121
    def __init__(self, attrs=None, options=None, usel10n=None):
102 122

  
103 123
        if attrs is None:
......
145 165
        if not help_text:
146 166
            help_text = u'%s %s' % (_('Format:'), self.options['format'])
147 167

  
148
        return mark_safe(BOOTSTRAP_INPUT_TEMPLATE % dict(
168
        return mark_safe(self.render_template % dict(
149 169
            id=id,
150 170
            rendered_widget=rendered_widget,
151 171
            clear_button=CLEAR_BTN_TEMPLATE if self.options.get('clearBtn') else '',
152 172
            glyphicon=self.glyphicon,
173
            language=get_language(),
153 174
            options=js_options,
154 175
            help_text=help_text))
155 176

  
......
179 200
    DateWidget is the corresponding widget for Date field, it renders only the date section of
180 201
    datetime picker.
181 202
    """
182

  
183 203
    format_name = 'DATE_INPUT_FORMATS'
184 204
    glyphicon = 'glyphicon-calendar'
205
    input_type = 'date'
206
    render_template = BOOTSTRAP_DATE_INPUT_TEMPLATE
185 207

  
186 208
    def __init__(self, attrs=None, options=None, usel10n=None):
187 209

  
......
191 213
        # Set the default options to show only the datepicker object
192 214
        options['startView'] = options.get('startView', 2)
193 215
        options['minView'] = options.get('minView', 2)
194
        options['format'] = options.get('format', self.get_format())
195 216

  
217
        options['format'] = options.get('format', self.get_format())
196 218
        super(DateWidget, self).__init__(attrs, options, usel10n)
197 219

  
220
    def format_value(self, value):
221
        if value is not None:
222
            if isinstance(value, datetime.datetime):
223
                return force_text(value.isoformat())
224
            return value
225

  
198 226

  
199 227
class TimeWidget(PickerWidgetMixin, TimeInput):
200 228
    """
201
-