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