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