1 | #-*- mode: python; mode: fold -*- |
---|
2 | import re |
---|
3 | from DateTime.DateTime import DateTime |
---|
4 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
5 | CPSDateTimeWidget.time_hour_default = '00' |
---|
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 |
---|
33 | # import pdb;pdb.set_trace() |
---|
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: |
---|
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 |
---|
44 | # locale = self.translation_service.getSelectedLanguage() |
---|
45 | # if False and locale in ('en', 'hu'): |
---|
46 | # m, d, y = date.split('/') |
---|
47 | # else: |
---|
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 |
---|
62 | |
---|
63 | |
---|
64 | CPSDateTimeWidget.validate = validate |
---|
65 | |
---|