source: WAeUP_SRP/trunk/patches/PatchExtendedWidgetsCPSDateTimeWidget.py @ 5027

Last change on this file since 5027 was 5027, checked in by Henrik Bettermann, 15 years ago

fix syntax

  • Property svn:keywords set to Id
File size: 3.3 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   
13    if self.view_format == 'import':
14        #import pdb;pdb.set_trace()
15        if datastructure[widget_id] == 'None':
16            datastructure[widget_id] == ''
17        try:
18            dt = datastructure[widget_id].strip()
19            date,time,dummy = dt.split()
20            y,m,d = date.split('/')
21            hour,minute,second = time.split(':')
22        except:
23            datastructure.setError(widget_id, 'cpsschemas_err_date')
24            return 0
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    else:
34        if datastructure.has_key(widget_id + '_date'):
35            date = datastructure[widget_id + '_date'].strip()
36        else:
37            date = datastructure[widget_id].strip()
38        if datastructure.has_key(widget_id + '_hour'):
39            hour = datastructure[widget_id + '_hour'].strip()
40        else:
41            hour = self.time_hour_default
42        if datastructure.has_key(widget_id + '_minute'):
43            minute = datastructure[widget_id + '_minute'].strip()
44        else:
45            minute = self.time_minutes_default
46
47        if not (date):
48            if self.is_required:
49                datastructure[widget_id] = ''
50                datastructure.setError(widget_id, 'cpsschemas_err_required')
51                return 0
52            else:
53                datamodel[field_id] = None
54                return 1
55               
56        #import pdb;pdb.set_trace()
57        if self.view_format.startswith('iso8601'):
58            if re.match(r'^[0-9]+-[0-9]{2}-[0-9]{2}', date) is not None:
59                y, m, d = date.split('-')
60            else:
61                datastructure.setError(widget_id, 'cpsschemas_err_date')
62                return 0
63        else:
64            if re.match(r'^[0-9]?[0-9][/,\-,\.][0-9]?[0-9][/,\-,\.][0-9]{2,4}$', date) is not None:
65                d, m, y = re.split(r'[/,\-,\.]',date) # always use d/m/y and use /,- or . as separator
66                #d, m, y = date.split('/') # always use d/m/y
67                # locale = self.translation_service.getSelectedLanguage()
68                # if False and locale in ('en', 'hu'):
69                #     m, d, y = date.split('/')
70                # else:
71            else:
72                datastructure.setError(widget_id, 'cpsschemas_err_date')
73                return 0
74
75    try:
76        v = DateTime(int(y), int(m), int(d), int(hour), int(minute))
77    except (ValueError, TypeError, DateTime.DateTimeError,
78            DateTime.SyntaxError, DateTime.DateError):
79        datastructure.setError(widget_id, 'cpsschemas_err_date')
80        return 0
81    else:
82        datastructure[widget_id] = v
83        datamodel[field_id] = v
84        return 1
85   
86
87CPSDateTimeWidget.validate = validate
88
Note: See TracBrowser for help on using the repository browser.