def test_date_field2(self): req = self.get_request() months, days = date.get_date_arrays() hours, minutes = date.get_time_arrays() simple_element = form.FormNode('test-form') simple_element['start_date'](type='fieldset', style='brief', label='start date:') simple_element['start_date']['month'](type='select', weight=0, options=months, value=8) simple_element['start_date']['day'](type='select', weight=1, options=days, value=25) simple_element['start_date']['year'](type='textfield', weight=2, size=5, value=2007) simple_element['start_date']['hour'](type='select', weight=3, options=hours, value=23) simple_element['start_date']['minute'](type='select', weight=4, options=minutes, value=40) date_element = form.FormNode('test-form') date_element['start_date'](type='datetime', label='start date:', value=datetime.datetime.fromtimestamp(1190864400)) simple_element_html = str(simple_element.render(req)) date_element_html = str(date_element.render(req)) for i in range(len(date_element_html)): if(simple_element_html[i] != date_element_html[i]): self.fail('Expecting %s (%s) but found %s (%s) at position %d' % ( simple_element_html[i], simple_element_html[i-20:i+20], date_element_html[i], date_element_html[i-20:i+20], i )) self.failUnlessEqual(date_element_html, simple_element_html, "Didn't get expected form output, got:\n%s\n instead of:\n%s" % (date_element_html, simple_element_html) )
def theme_datetime(self, form_id, element): import time attribs = element.attr('attributes', {}) months, days = date.get_date_arrays() hours, minutes = date.get_time_arrays() #value = date.convert_to_timestamp(element.attr('value', None)) value = element.attr('value', None) if(value is None): year = datetime.datetime.now().year month, day, hour, minute = (months[0], 1, '00', '00') else: month, day, year, hour, minute = date.strftime(value, '%B:%d:%Y:%H:%M').split(':') arrays = (months, days, hours, minutes) values = (month, int(day), hour, minute) names = ('month', 'day', 'hour', 'minute') dateselect = self._generate_datetime_select(element, attribs, arrays[:2], values[:2], names[:2]) year_attribs = dict( name = '%s[year]' % element.get_element_name(), size = 5, value = year, ) yearfield = tags.input(type='text', **year_attribs) timeselect = self._generate_datetime_select(element, attribs, arrays[2:], values[2:], names[2:]) return ''.join([str(x) for x in (dateselect, yearfield, timeselect)])
def test_date_field_arrays(self): """ Test the date field arrays used to build date selects. """ months, days = date.get_date_arrays() hours, minutes = date.get_time_arrays() self.failUnlessEqual(days[0], 1, 'Found broken first day: %s' % days[0]) self.failUnlessEqual(days[-1], 31, 'Found broken last day: %s' % days[-1]) self.failUnlessEqual(hours[0], '00', 'Found broken first hour: %s' % hours[0]) self.failUnlessEqual(hours[-1], '24', 'Found broken last hour: %s' % hours[-1]) self.failUnlessEqual(minutes[0], '00', 'Found broken first minute: %s' % minutes[0]) self.failUnlessEqual(minutes[-1], '59', 'Found broken last minute: %s' % minutes[-1])
def theme_time(self, form_id, element): import time attribs = element.attr('attributes', {}) hours, minutes = date.get_time_arrays() value = date.convert_to_timestamp(element.attr('value', None)) if(value is None): hour, minute = ('00', '00') else: hour, minute = time.strftime('%H:%M', time.localtime(value)).split(':') arrays = (hours, minutes) values = (hour, minute) names = ('hour', 'minute') return self._generate_datetime_select(element, attribs, arrays, values, names)
def test_date_field(self): """ Test for L{modu.editable.datatypes.date.DateField} """ from modu.editable.datatypes import date as date_datatype test_itemdef = define.itemdef( start_date = date_datatype.DateField( label = 'start date:', save_format = 'datetime', attributes = dict(basic_element=False), ) ) req = self.get_request([('test-form[start_date][date][month]',8), ('test-form[start_date][date][day]',25), ('test-form[start_date][date][year]',2007), ('test-form[start_date][date][hour]',23), ('test-form[start_date][date][minute]',40) ]) test_storable = storable.Storable('test') # +---------------------------+ # | from_unixtime(1190864400) | # +---------------------------+ # | 2007-09-26 23:40:00 | # +---------------------------+ test_storable.start_date = datetime.datetime.fromtimestamp(1190864400) self.failUnlessEqual(test_storable.start_date.year, 2007, 'Test year was calculated incorrectly as %s' % test_storable.start_date.year) months, days = date.get_date_arrays() hours, minutes = date.get_time_arrays() itemdef_form = test_itemdef.get_form(req, test_storable) reference_form = form.FormNode('test-form') reference_form['start_date'](type='fieldset', style='brief', label='start date:') reference_form['start_date']['null'](type='checkbox', text="no value", weight=-1, suffix=tags.br(), attributes=dict(onChange='enableDateField(this);')) reference_form['start_date']['date'](type='fieldset') reference_form['start_date']['date']['month'](type='select', weight=0, options=months, value=8) reference_form['start_date']['date']['day'](type='select', weight=1, options=days, value=25) reference_form['start_date']['date']['year'](type='textfield', weight=2, size=5, value=2007) reference_form['start_date']['date']['hour'](type='select', weight=3, options=hours, value=23) reference_form['start_date']['date']['minute'](type='select', weight=4, options=minutes, value=40) reference_form['start_date']( suffix = tags.script(type="text/javascript")[""" enableDateField($('#form-item-start_date input')); """], ) reference_form['save'](type='submit', value='save', weight=1000) reference_form['cancel'](type='submit', value='cancel', weight=1001) reference_form['delete'](type='submit', value='delete', weight=1002, attributes=dict(onClick="return confirm('Are you sure you want to delete this record?');")) itemdef_form_html = str(itemdef_form.render(req)) reference_form_html = str(reference_form.render(req)) for i in range(len(itemdef_form_html)): if(reference_form_html[i] != itemdef_form_html[i]): self.fail('Expecting %s (%s) but found %s (%s) at position %d' % ( reference_form_html[i], reference_form_html[i-20:i+20], itemdef_form_html[i], itemdef_form_html[i-20:i+20], i )) self.failUnlessEqual(itemdef_form_html, reference_form_html, "Didn't get expected form output, got:\n%s\n instead of:\n%s" % (itemdef_form_html, reference_form_html) ) test_storable.start_date = datetime.datetime.now() # this just loads the data, since there was # no submit button in the test post data itemdef_form.execute(req) test_itemdef['start_date'].update_storable(req, itemdef_form, test_storable) self.failUnlessEqual(test_storable.start_date, datetime.datetime.fromtimestamp(1190864400), 'Date was calculated incorrectly as %s' % test_storable.start_date)