[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: |
---|
[1828] | 42 | d, m, y = date.split('/') # always use d/m/y |
---|
| 43 | # locale = self.translation_service.getSelectedLanguage() |
---|
| 44 | # if False and locale in ('en', 'hu'): |
---|
| 45 | # m, d, y = date.split('/') |
---|
| 46 | # else: |
---|
[1804] | 47 | else: |
---|
| 48 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
| 49 | return 0 |
---|
| 50 | |
---|
| 51 | try: |
---|
| 52 | v = DateTime(int(y), int(m), int(d), int(hour), int(minute)) |
---|
| 53 | except (ValueError, TypeError, DateTime.DateTimeError, |
---|
| 54 | DateTime.SyntaxError, DateTime.DateError): |
---|
| 55 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
| 56 | return 0 |
---|
| 57 | else: |
---|
| 58 | datastructure[widget_id] = v |
---|
| 59 | datamodel[field_id] = v |
---|
| 60 | return 1 |
---|
[1828] | 61 | |
---|
[1804] | 62 | |
---|
| 63 | CPSDateTimeWidget.validate = validate |
---|
| 64 | |
---|