def clean(self): """ Validates that the input can be converted to a date. Returns a Python datetime.date object. """ super(DateField, self).clean() if self.is_empty(): return fredtypes.NullDate() if isinstance(self.value, datetime.datetime): return self.value.date() if isinstance(self.value, datetime.date): return self.value for format in self.input_formats: try: return datetime.date(*time.strptime(self.value, format)[:3]) except ValueError: continue raise ValidationError(_(u'Enter a valid date.'))
def test_encode_double_nested_oldstyle_class(self): """ Nested class gets encoded OK. Note: We're using old-style classes, because that's what omniORBpy does. """ rec = recoder.DaphneCorbaRecode("utf-8") p_ent = TestDaphneCorbaRecoder.Foo( 1, TestDaphneCorbaRecoder.Bar( 2, TestDaphneCorbaRecoder.Bar(3, fredtypes.NullDate(), "5"), 6.0), fredtypes.NullInt()) exp = TestDaphneCorbaRecoder.Foo( 1, TestDaphneCorbaRecoder.Bar( 2, TestDaphneCorbaRecoder.Bar(3, fredcorba.ccReg.DateType(0, 0, 0), "5"), 6.0), 0) res = rec.encode(p_ent) requiring(res).equal_to(exp)
def test_encode_nested_date(self): rec = recoder.DaphneCorbaRecode("utf-8") entity = fredcorba.Registry.Registrar.Detail( id=19, ico=u'', dic=u'', varSymb=u'', vat=False, handle=u'NEW REG', name=u'name 1', organization=u'', street1=u'', street2=u'', street3=u'', city=u'', stateorprovince=u'state', postalcode=u'', country=u'CZ', telephone=u'', fax=u'', email=u'', url=u'', credit=u'0.00', unspec_credit=u"0.00", access=[], zones=[ fredcorba.ccReg.AdminZoneAccess(id=0, name=u'cz', fromDate=datetime.date( 2009, 12, 11), toDate=fredtypes.NullDate()) ], hidden=False) expected = fredcorba.Registry.Registrar.Detail( id=19, ico='', dic='', varSymb='', vat=False, handle='NEW REG', name='name 1', organization='', street1='', street2='', street3='', city='', stateorprovince='state', postalcode='', country='CZ', telephone='', fax='', email='', url='', unspec_credit=u"0.00", credit='0.00', access=[], zones=[ fredcorba.ccReg.AdminZoneAccess( id=0, name='cz', fromDate=fredcorba.ccReg.DateType(day=11, month=12, year=2009), toDate='') ], hidden=False) res = rec.encode(entity) requiring(res).is_not_none() requiring(expected.__dict__['zones'][0].fromDate.__dict__).\ equal_to(res.__dict__['zones'][0].fromDate.__dict__)
def corba_to_date(corba_date): if corba_date.year == 0: # empty date is in corba = DateType(0, 0, 0) return fredtypes.NullDate() return datetime.date(corba_date.year, corba_date.month, corba_date.day)
def resolve_value(self, value): if value != fredtypes.Null(): return value #recoder.corba_to_date(value) else: return fredtypes.NullDate()