source: WAeUP_SRP/trunk/PatchExtendedWidgetsCPSDateTimeWidget.py @ 1821

Last change on this file since 1821 was 1804, checked in by joachim, 17 years ago

fix for #205, add more widgets for import_data

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1#-*- mode: python; mode: fold -*-
2import re
3from DateTime.DateTime import DateTime
4from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget
5
6def 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
61CPSDateTimeWidget.validate = validate
62
Note: See TracBrowser for help on using the repository browser.