widgets.py 2.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from django.forms.widgets import Input 
from django.forms.utils import flatatt 
from django.utils.safestring import mark_safe 
from django.template import Context, Template 
from django.template.loader import get_template 
from django.conf import settings 
from django.utils.translation import ugettext_lazy as _, get_language_bidi 
from django.utils.dates import ( 
    WEEKDAYS, 
    WEEKDAYS_ABBR, 
    MONTHS, 
    MONTHS_3, 
    MONTHS_AP, 
    MONTHS_ALT 
)
root's avatar
root committed
16

17 18 19 20 21 22 23 24 25
def list2str(str_iterable):
    """
    Utility function to return a string representing a list of string 

    :params str_iterable: An iterable object where each element is of type str
    :returns: A representation of the iterable as a list (e.g '["a", "b"]')
    """
    return '["' + '", "'.join(str_iterable) + '"]'
    
root's avatar
root committed
26
class DateTimePicker(Input):
27 28 29
    is_localized = False
    def render(self, name, value, attrs=None): 
        super().render(name, value, attrs) 
root's avatar
root committed
30
        flat_attrs = flatatt(attrs)
31
        context = Context({
32
            'name': name,
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
            'attrs': flat_attrs, 
            'id': attrs['id'], 
            'closeText': _("Close"), 
            'currentText': _("Today"),
            'dayNames': mark_safe(list2str((str(item[1]) for item in WEEKDAYS.items()))),
            'dayNamesMin': mark_safe(list2str((str(item[1]) for item in WEEKDAYS_ABBR.items()))), 
            'dayNamesShort': mark_safe(list2str((str(item[1]) for item in WEEKDAYS_ABBR.items()))), 
            'firstDay': mark_safe('"' + str(WEEKDAYS[settings.FIRST_DAY_OF_WEEK]) + '"'), 
            'isRTL': str(get_language_bidi()).lower(), 
            'monthNames': mark_safe(list2str((str(item[1]) for item in MONTHS.items()))),
            'monthNamesShort': mark_safe(list2str((str(item[1]) for item in MONTHS_3.items()))), 
            'nextText': mark_safe('"' + str(_('Next')) + '"'), 
            'prevText': mark_safe('"' + str(_('Previous')) + '"'), 
            'weekHeader': mark_safe('"' + str(_('Wk')) + '"' ),
        }) 
        template = get_template('users/datetimepicker.html')
        return template.render(context)