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 | |
---|
13 | if self.view_format == 'import': |
---|
14 | #import pdb;pdb.set_trace() |
---|
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 |
---|
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 |
---|
34 | else: |
---|
35 | #import pdb;pdb.set_trace() |
---|
36 | if datastructure.has_key(widget_id + '_date'): |
---|
37 | date = datastructure[widget_id + '_date'].strip() |
---|
38 | else: |
---|
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 |
---|
48 | |
---|
49 | if not (date) or date == 'None': |
---|
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 | |
---|
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 |
---|
87 | |
---|
88 | |
---|
89 | CPSDateTimeWidget.validate = validate |
---|
90 | |
---|