예제 #1
0
     ')'
 ),
 F=array_index(MONTHS, '%(x)s.getMonth()'),
 g='%(x)s.getHours()%%12',
 G='%(x)s.getHours()',
 h=zero_padded_two_digit('%(x)s.getHours()%%12'),
 H=zero_padded_two_digit('%(x)s.getHours()'),
 i=zero_padded_two_digit('%(x)s.getMinutes()'),
 I=NotImplemented,  # TODO daylight saving
 j='%(x)s.getDate()',
 l=array_index(WEEKDAYS, '%(x)s.getDay()', cycle=True),
 L=NotImplemented,  # TODO leepyear boolean
 m=zero_padded_two_digit('%(x)s.getMonth()+1'),
 M=array_index(MONTHS_3, '%(x)s.getMonth()', capitalize=True),
 n='%(x)s.getMonth()+1',
 N=array_index(MONTHS_AP.values(), '%(x)s.getMonth()'),
 o=NotImplemented,  # TODO year matching iso week number
 O=NotImplemented,  # TODO difference to GMT in hours
 # P = this expression is built below in build_formatchar_P()
 r=NotImplemented,  # TODO iso
 s=zero_padded_two_digit('%(x)s.getSeconds()'),
 S='(11<=%(x)s.getDate()&&%(x)s.getDate()<=13?"th":%(x)s.getDate()%%10==1?"st":(%(x)s.getDate()%%10==2?"nd":(%(x)s.getDate()%%10==3?"rd":"th")))',
 t=NotImplemented,  # TODO number of days in month
 T=NotImplemented,  # TODO timezone of this machine
 u=zero_padded('%(x)s.getMilliseconds()*1000', 6),
 U='Math.floor(%(x)s.getTime()/1000)',
 w='%(x)s.getDay()',
 W=NotImplemented,  # TODO iso week of year
 y=zero_padded_two_digit('%(x)s.getFullYear()%%100'),
 Y='%(x)s.getFullYear()',
 z=NotImplemented,  # TODO day of year
    def __init__(self, attrs=None, format=None, datepicker_class='datepicker',
                 params=None):
        params = params or {}
        attrs = attrs or {}
        class_attr = attrs.get('class', datepicker_class)

        if datepicker_class not in class_attr:
            attrs.update({
                'class': '{0} {1}'.format(class_attr, datepicker_class),
                'readonly': True,
            })
        else:
            attrs.update({
                'class': 'datepicker',
                'readonly': True,
            })

        super(DatePickerWidget, self).__init__(attrs, format)

        self.params = {
            'changeMonth': True,
            'changeYear': True,
            'yearRange': '2000:2050',
        }

        self.regional = {
            'closeText': _('Close'),
            'prevText': _('Previous'),
            'nextText': _('Next'),
            'currentText': _('Current'),
            'monthNames': map(unicode, MONTHS_ALT.values()),
            'monthNamesShort': map(unicode, MONTHS_AP.values()),
            'dayNames': map(unicode, WEEKDAYS.values()),
            'dayNamesShort': map(unicode, WEEKDAYS_ABBR.values()),
            'dayNamesMin': map(unicode, WEEKDAYS_ABBR.values()),
            'isRTL': get_language_bidi(),
            # weekHeader
            # dateFormat
            # firstDay
            # showMonthAfterYear
            # yearSuffix
        }

        # Update the datepicker parameters
        self.params.update(params)

        pattern = re.compile(
            r'(?<!\w)(' + '|'.join(PYTHON_TO_DATEPICKER_FORMAT.keys()) + r')\b'
        )

        if params.get('beforeShowDay'):
            raise NotImplementedError(
                u'beforeShowDay is not supported. Please add the function to '
                u'the template.'
            )

        if not params.get('dateFormat'):
            self.params.update({
                'dateFormat': pattern.sub(
                    lambda x: PYTHON_TO_DATEPICKER_FORMAT[x.group()],
                    self.format
                )
            })

        import datetime
        min_date = self.params.get('minDate')
        if min_date:
            if isinstance(min_date, datetime.date):
                self.params.update({
                    'minDate': self.params['minDate'].strftime(
                        format=self.format
                    )
                })
            elif isinstance(min_date, datetime.datetime):
                self.params.update({
                    'maxDate': min_date.strftime(
                        format=self.format
                    )
                })

        max_date = self.params.get('maxDate')
        if max_date:
            if isinstance(max_date, datetime.date):
                self.params.update({
                    'maxDate': max_date.strftime(
                        format=self.format
                    )
                })
            elif isinstance(max_date, datetime.datetime):
                self.params.update({
                    'maxDate': max_date.strftime(
                        format=self.format
                    )
                })