[1804] | 1 | #-*- mode: python; mode: fold -*- |
---|
| 2 | import re |
---|
| 3 | from DateTime.DateTime import DateTime |
---|
| 4 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
[1828] | 5 | CPSDateTimeWidget.time_hour_default = '00' |
---|
[1804] | 6 | |
---|
| 7 | def validate(self, datastructure, **kw): |
---|
| 8 | """Validate datastructure and update datamodel.""" |
---|
| 9 | datamodel = datastructure.getDataModel() |
---|
| 10 | field_id = self.fields[0] |
---|
| 11 | widget_id = self.getWidgetId() |
---|
[3988] | 12 | |
---|
| 13 | if self.view_format == 'import': |
---|
| 14 | try: |
---|
| 15 | dt = datastructure[widget_id].strip() |
---|
| 16 | date,time,dummy = dt.split() |
---|
| 17 | y,m,d = date.split('/') |
---|
| 18 | hour,minute,second = time.split(':') |
---|
| 19 | except: |
---|
[1804] | 20 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
| 21 | return 0 |
---|
[3988] | 22 | if not (date): |
---|
| 23 | if self.is_required: |
---|
| 24 | datastructure[widget_id] = '' |
---|
| 25 | datastructure.setError(widget_id, 'cpsschemas_err_required') |
---|
| 26 | return 0 |
---|
| 27 | else: |
---|
| 28 | datamodel[field_id] = None |
---|
| 29 | return 1 |
---|
[1804] | 30 | else: |
---|
[3988] | 31 | if datastructure.has_key(widget_id + '_date'): |
---|
| 32 | date = datastructure[widget_id + '_date'].strip() |
---|
[1804] | 33 | else: |
---|
[3988] | 34 | date = datastructure[widget_id].strip() |
---|
| 35 | if datastructure.has_key(widget_id + '_hour'): |
---|
| 36 | hour = datastructure[widget_id + '_hour'].strip() |
---|
| 37 | else: |
---|
| 38 | hour = self.time_hour_default |
---|
| 39 | if datastructure.has_key(widget_id + '_minute'): |
---|
| 40 | minute = datastructure[widget_id + '_minute'].strip() |
---|
| 41 | else: |
---|
| 42 | minute = self.time_minutes_default |
---|
[1804] | 43 | |
---|
[3988] | 44 | if not (date): |
---|
| 45 | if self.is_required: |
---|
| 46 | datastructure[widget_id] = '' |
---|
| 47 | datastructure.setError(widget_id, 'cpsschemas_err_required') |
---|
| 48 | return 0 |
---|
| 49 | else: |
---|
| 50 | datamodel[field_id] = None |
---|
| 51 | return 1 |
---|
| 52 | |
---|
| 53 | #import pdb;pdb.set_trace() |
---|
| 54 | if self.view_format.startswith('iso8601'): |
---|
| 55 | if re.match(r'^[0-9]+-[0-9]{2}-[0-9]{2}', date) is not None: |
---|
| 56 | y, m, d = date.split('-') |
---|
| 57 | else: |
---|
| 58 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
| 59 | return 0 |
---|
| 60 | else: |
---|
| 61 | if re.match(r'^[0-9]?[0-9][/,\-,\.][0-9]?[0-9][/,\-,\.][0-9]{2,4}$', date) is not None: |
---|
| 62 | d, m, y = re.split(r'[/,\-,\.]',date) # always use d/m/y and use /,- or . as separator |
---|
| 63 | #d, m, y = date.split('/') # always use d/m/y |
---|
| 64 | # locale = self.translation_service.getSelectedLanguage() |
---|
| 65 | # if False and locale in ('en', 'hu'): |
---|
| 66 | # m, d, y = date.split('/') |
---|
| 67 | # else: |
---|
| 68 | else: |
---|
| 69 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
| 70 | return 0 |
---|
| 71 | |
---|
[1804] | 72 | try: |
---|
| 73 | v = DateTime(int(y), int(m), int(d), int(hour), int(minute)) |
---|
| 74 | except (ValueError, TypeError, DateTime.DateTimeError, |
---|
| 75 | DateTime.SyntaxError, DateTime.DateError): |
---|
| 76 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
| 77 | return 0 |
---|
| 78 | else: |
---|
| 79 | datastructure[widget_id] = v |
---|
| 80 | datamodel[field_id] = v |
---|
| 81 | return 1 |
---|
[1828] | 82 | |
---|
[1804] | 83 | |
---|
| 84 | CPSDateTimeWidget.validate = validate |
---|
| 85 | |
---|