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