1 | #-*- mode: python; mode: fold -*- |
---|
2 | import re |
---|
3 | from DateTime.DateTime import DateTime |
---|
4 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
5 | |
---|
6 | def validate(self, datastructure, **kw): |
---|
7 | """Validate datastructure and update datamodel.""" |
---|
8 | datamodel = datastructure.getDataModel() |
---|
9 | field_id = self.fields[0] |
---|
10 | widget_id = self.getWidgetId() |
---|
11 | if datastructure.has_key(widget_id + '_date'): |
---|
12 | date = datastructure[widget_id + '_date'].strip() |
---|
13 | else: |
---|
14 | date = datastructure[widget_id].strip() |
---|
15 | if datastructure.has_key(widget_id + '_hour'): |
---|
16 | hour = datastructure[widget_id + '_hour'].strip() |
---|
17 | else: |
---|
18 | hour = self.time_hour_default |
---|
19 | if datastructure.has_key(widget_id + '_minute'): |
---|
20 | minute = datastructure[widget_id + '_minute'].strip() |
---|
21 | else: |
---|
22 | minute = self.time_minutes_default |
---|
23 | |
---|
24 | if not (date): |
---|
25 | if self.is_required: |
---|
26 | datastructure[widget_id] = '' |
---|
27 | datastructure.setError(widget_id, 'cpsschemas_err_required') |
---|
28 | return 0 |
---|
29 | else: |
---|
30 | datamodel[field_id] = None |
---|
31 | return 1 |
---|
32 | |
---|
33 | if self.view_format.startswith('iso8601'): |
---|
34 | if re.match(r'^[0-9]+-[0-9]{2}-[0-9]{2}', date) is not None: |
---|
35 | y, m, d = date.split('-') |
---|
36 | else: |
---|
37 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
38 | return 0 |
---|
39 | else: |
---|
40 | if re.match(r'^[0-9]?[0-9]/[0-9]?[0-9]/[0-9]{2,4}$', date) is not None: |
---|
41 | locale = self.translation_service.getSelectedLanguage() |
---|
42 | if locale in ('en', 'hu'): |
---|
43 | m, d, y = date.split('/') |
---|
44 | else: |
---|
45 | d, m, y = date.split('/') |
---|
46 | else: |
---|
47 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
48 | return 0 |
---|
49 | |
---|
50 | try: |
---|
51 | v = DateTime(int(y), int(m), int(d), int(hour), int(minute)) |
---|
52 | except (ValueError, TypeError, DateTime.DateTimeError, |
---|
53 | DateTime.SyntaxError, DateTime.DateError): |
---|
54 | datastructure.setError(widget_id, 'cpsschemas_err_date') |
---|
55 | return 0 |
---|
56 | else: |
---|
57 | datastructure[widget_id] = v |
---|
58 | datamodel[field_id] = v |
---|
59 | return 1 |
---|
60 | |
---|
61 | CPSDateTimeWidget.validate = validate |
---|
62 | |
---|