source: WAeUP_SRP/branches/joachim-azax-branch/PatchExtendedWidgetsCPSDateTimeWidget.py @ 14240

Last change on this file since 14240 was 1828, checked in by joachim, 18 years ago

fix for #232

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