#-*- mode: python; mode: fold -*- import re from DateTime.DateTime import DateTime from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget CPSDateTimeWidget.time_hour_default = '00' def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" datamodel = datastructure.getDataModel() field_id = self.fields[0] widget_id = self.getWidgetId() if self.view_format == 'import': #import pdb;pdb.set_trace() try: dt = datastructure[widget_id].strip() date,time,dummy = dt.split() y,m,d = date.split('/') hour,minute,second = time.split(':') except: datastructure.setError(widget_id, 'cpsschemas_err_date') return 0 if not (date): if self.is_required: datastructure[widget_id] = '' datastructure.setError(widget_id, 'cpsschemas_err_required') return 0 else: datamodel[field_id] = None return 1 else: if datastructure.has_key(widget_id + '_date'): date = datastructure[widget_id + '_date'].strip() else: date = datastructure[widget_id].strip() if datastructure.has_key(widget_id + '_hour'): hour = datastructure[widget_id + '_hour'].strip() else: hour = self.time_hour_default if datastructure.has_key(widget_id + '_minute'): minute = datastructure[widget_id + '_minute'].strip() else: minute = self.time_minutes_default if not (date): if self.is_required: datastructure[widget_id] = '' datastructure.setError(widget_id, 'cpsschemas_err_required') return 0 else: datamodel[field_id] = None return 1 #import pdb;pdb.set_trace() if self.view_format.startswith('iso8601'): if re.match(r'^[0-9]+-[0-9]{2}-[0-9]{2}', date) is not None: y, m, d = date.split('-') else: datastructure.setError(widget_id, 'cpsschemas_err_date') return 0 else: if re.match(r'^[0-9]?[0-9][/,\-,\.][0-9]?[0-9][/,\-,\.][0-9]{2,4}$', date) is not None: d, m, y = re.split(r'[/,\-,\.]',date) # always use d/m/y and use /,- or . as separator #d, m, y = date.split('/') # always use d/m/y # locale = self.translation_service.getSelectedLanguage() # if False and locale in ('en', 'hu'): # m, d, y = date.split('/') # else: else: datastructure.setError(widget_id, 'cpsschemas_err_date') return 0 try: v = DateTime(int(y), int(m), int(d), int(hour), int(minute)) except (ValueError, TypeError, DateTime.DateTimeError, DateTime.SyntaxError, DateTime.DateError): datastructure.setError(widget_id, 'cpsschemas_err_date') return 0 else: datastructure[widget_id] = v datamodel[field_id] = v return 1 CPSDateTimeWidget.validate = validate