Beispiel #1
0
	def theme_date(self, form_id, element):
		import time
		attribs = element.attr('attributes', {})
		
		months, days = date.get_date_arrays()
		
		value = element.attr('value', None)
		
		if(value is None):
			year = datetime.datetime.now().year
			month, day = (months[0], 1)
		else:
			if(isinstance(value, (int, float))):
				value = datetime.datetime.utcfromtimestamp(value)
			
			month, day, year = date.strftime(value, '%B:%d:%Y').split(':')
		
		arrays = (months, days)
		values = (month, int(day))
		names = ('month', 'day')
		
		monthday = self._generate_datetime_select(element, attribs, arrays, values, names)
		
		yearfield = '%s[year]' % element.get_element_name()
		return str(monthday) + str(tags.input(type='text', size=5, value=year, name=yearfield))
Beispiel #2
0
	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) )
Beispiel #3
0
	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)])
Beispiel #4
0
	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])
Beispiel #5
0
	def theme_date_loader(self, form, form_data):
		import time, datetime
		
		months, days = date.get_date_arrays()
		
		try:
			value = datetime.date(int(form_data['year'].value), int(form_data['month'].value) + 1, int(form_data['day'].value) + 1)
		except:
			try:
				max_day = 30
				if(int(form_data['month'].value) == 2):
					max_day = 28
				value = datetime.date(int(form_data['year'].value), int(form_data['month'].value) + 1, max_day)
			except:
				value = datetime.date.today()
		form(value=value)
Beispiel #6
0
	def theme_datetime_loader(self, form, form_data):
		import time, datetime
		
		months, days = date.get_date_arrays()
		
		try:
			value = datetime.datetime(int(form_data['year'].value), int(form_data['month'].value) + 1, int(form_data['day'].value) + 1,
										int(form_data['hour'].value), int(form_data['minute'].value))
		except ValueError, e:
			try:
				max_day = 30
				if(int(form_data['month'].value) == 2):
					max_day = 28
				value = datetime.datetime(int(form_data['year'].value), int(form_data['month'].value) + 1, max_day,
											int(form_data['hour'].value), int(form_data['minute'].value))
			except ValueError, e:
				value = datetime.datetime.now()
Beispiel #7
0
	def get_element(self, req, style, storable):
		"""
		@see: L{modu.editable.define.definition.get_element()}
		"""
		value = getattr(storable, self.get_column_name(), None)
		if(isinstance(value, (int, long, float))):
			value = datetime.datetime.utcfromtimestamp(value)
		
		if(style == 'search'):
			frm = form.FormNode(self.name)
			frm['from'] = self.get_form_element(req, '_detail', storable)(
				prefix='<div>from date:',
				suffix=tags.br() + '</div>',
			)
			frm['to'] = self.get_form_element(req, '_detail', storable)(
				prefix='<div>to date:',
				suffix='</div>',
			)
			return frm
		elif(style == 'listing' or (style == 'detail' and self.get('read_only', False))):
			if(value):
				output = date.strftime(value, self.get('format_string', '%B %d, %Y at %I:%M%p'))
			else:
				output = ''
			frm = form.FormNode(self.name)
			frm(type='label', value=output)
			return frm
		
		current_year = datetime.datetime.now().year
		if(value is not None):
			current_year = getattr(value, 'year', current_year)
		
		start_year = self.get('start_year', current_year - 2)
		end_year = self.get('end_year', current_year + 5)
		
		months, days = date.get_date_arrays()
		
		frm = form.FormNode(self.name)
		frm(type='fieldset', style='brief')
		
		frm['null'](type='checkbox', text="no value", weight=-1, suffix=tags.br(), 
			attributes=dict(onChange='enableDateField(this);'))
		
		assets.activate_jquery(req)
		req.content.report('header', tags.script(type='text/javascript')["""
			function enableDateField(checkboxField){
				var formItem = $(checkboxField).parent().parent();
				if($(checkboxField).attr('checked')){
					formItem.children(':enabled').attr('disabled', true);
				}
				else{
					formItem.children(':disabled').attr('disabled', false);
				}
			}
		"""])
		
		attribs = {}
		
		if(value is None):
			frm['null'](checked=True)
			#attribs['disabled'] = None
			
			if(self.get('default_now', False)):
				value = datetime.datetime.now()
				frm['null'](checked=False)
		
		frm['date'](
			type		= self.get('style', 'datetime'),
			value		= value,
			attributes	= attribs,
			suffix		= tags.script(type="text/javascript")["""
				enableDateField($('#form-item-%s input'));
			""" % self.name],
		)
		
		frm.validate = self.validate

		return frm
Beispiel #8
0
	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)