Login

NonTimeSplitDateTimeField

Author:
jturnbull
Posted:
April 2, 2009
Language:
Python
Version:
1.0
Tags:
datetime time splitdatetimefield
Score:
1 (after 1 ratings)

Variation on SplitDateTimeField that does not enforce the Time to be required, accepting a default instead.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class NonTimeSplitDateTimeField(forms.SplitDateTimeField):
    
    """Variation on SplitDateTimeField that does not enforce the Time to be
    required, accepting a default instead.
    
    example usage:
    
    class MonkeyForm(forms.ModelForm):

        start_date = NonTimeSplitDateTimeField(
            default_time=datetime.time(00,00,00),
            widget=widgets.AdminSplitDateTime(), required=False )
        end_date = NonTimeSplitDateTimeField(
            default_time=datetime.time(11,59,59),
            widget=widgets.AdminSplitDateTime(), required=False )
    
    """
    
    
    def __init__(self, default_time, *args, **kwargs):
        self.default_time = default_time
        return super(NonTimeSplitDateTimeField, self).__init__(
            *args, **kwargs)
        
        
    def compress(self, data_list):
        if data_list:
            # Raise a validation error if date is empty
            # (possible if SplitDateTimeField has required=False).
            if data_list[0] in EMPTY_VALUES:
                raise ValidationError(self.error_messages['invalid_date'])
            if data_list[1] in EMPTY_VALUES:
                # Rather than raising an error (as super does) use the
                # default time passed in the constructor
                data_list[1] = self.default_time
            return datetime.datetime.combine(*data_list)
        return None

More like this

  1. Timing Django Requests by metajack 7 years ago
  2. ceil time entry to nearest quarter hour by rennat 6 years, 8 months ago
  3. Create breakpoints to time code at by menendez 7 years ago
  4. Request time logging middleware by mpasternacki 5 years, 7 months ago
  5. Time toggle on mouseover template filter by soniiic 6 years, 2 months ago

Comments

Please login first before commenting.