PageRenderTime 122ms CodeModel.GetById 80ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/django/contrib/localflavor/generic/forms.py

https://code.google.com/p/mango-py/
Python | 48 lines | 32 code | 5 blank | 11 comment | 3 complexity | 9a8444da476191ee02e86c94ece3c524 MD5 | raw file
 1from django import forms
 2
 3DEFAULT_DATE_INPUT_FORMATS = (
 4    '%Y-%m-%d', '%d/%m/%Y', '%d/%m/%y', # '2006-10-25', '25/10/2006', '25/10/06'
 5    '%b %d %Y', '%b %d, %Y',            # 'Oct 25 2006', 'Oct 25, 2006'
 6    '%d %b %Y', '%d %b, %Y',            # '25 Oct 2006', '25 Oct, 2006'
 7    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006'
 8    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006'
 9)
10
11DEFAULT_DATETIME_INPUT_FORMATS = (
12    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
13    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
14    '%Y-%m-%d',              # '2006-10-25'
15    '%d/%m/%Y %H:%M:%S',     # '25/10/2006 14:30:59'
16    '%d/%m/%Y %H:%M',        # '25/10/2006 14:30'
17    '%d/%m/%Y',              # '25/10/2006'
18    '%d/%m/%y %H:%M:%S',     # '25/10/06 14:30:59'
19    '%d/%m/%y %H:%M',        # '25/10/06 14:30'
20    '%d/%m/%y',              # '25/10/06'
21)
22
23class DateField(forms.DateField):
24    """
25    A date input field which uses non-US date input formats by default.
26    """
27    def __init__(self, input_formats=None, *args, **kwargs):
28        input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
29        super(DateField, self).__init__(input_formats=input_formats, *args, **kwargs)
30
31class DateTimeField(forms.DateTimeField):
32    """
33    A date and time input field which uses non-US date and time input formats
34    by default.
35    """
36    def __init__(self, input_formats=None, *args, **kwargs):
37        input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
38        super(DateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs)
39
40class SplitDateTimeField(forms.SplitDateTimeField):
41    """
42    Split date and time input fields which use non-US date and time input
43    formats by default.
44    """
45    def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs):
46        input_date_formats = input_date_formats or DEFAULT_DATE_INPUT_FORMATS
47        super(SplitDateTimeField, self).__init__(input_date_formats=input_date_formats,
48                                                 input_time_formats=input_time_formats, *args, **kwargs)