documented CompositeDateField

--HG--
extra : convert_revision : svn%3A41726ec3-264d-0410-9c23-a9f1637257cc/trunk%4091
This commit is contained in:
adrian.sampson 2008-08-25 08:01:34 +00:00
parent 22795c7124
commit 1560259c84

View file

@ -310,12 +310,28 @@ class MediaField(object):
self._storedata(obj, out)
class CompositeDateField(object):
"""
A MediaFile field for conveniently accessing the year, month, and day fields
as a datetime.date object. Allows both getting and setting of the component
fields.
"""
def __init__(self, year_field, month_field, day_field):
"""
Create a new date field from the indicated MediaFields for the component
values.
"""
self.year_field = year_field
self.month_field = month_field
self.day_field = day_field
def __get__(self, obj, owner):
"""
Return a datetime.date object whose components indicating the smallest
valid date whose components are at least as large as the three component
fields (that is, if year == 1999, month == 0, and day == 0, then
date == datetime.date(1999, 1, 1)). If the components indicate an invalid
date (e.g., if month == 47), datetime.date.min is returned.
"""
try:
return datetime.date(max(self.year_field.__get__(obj, owner),
datetime.MINYEAR),
@ -324,7 +340,12 @@ class CompositeDateField(object):
)
except ValueError: # Out of range values.
return datetime.date.min
def __set__(self, obj, val):
"""
Set the year, month, and day fields to match the components of the
provided datetime.date object.
"""
self.year_field.__set__(obj, val.year)
self.month_field.__set__(obj, val.month)
self.day_field.__set__(obj, val.day)