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

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

change date format for pume screening date

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