source: WAeUP_SRP/trunk/Widgets.py @ 416

Last change on this file since 416 was 388, checked in by joachim, 18 years ago

added checking for duplicate Id's

  • Property svn:keywords set to Id
File size: 8.4 KB
RevLine 
[47]1#-*- mode: python; mode: fold -*-
[295]2
3from cgi import escape
4
[22]5from Globals import InitializeClass
[199]6##from Products.CPSSchemas.Widget import CPSWidgetType
[295]7from Products.CMFCore.utils import getToolByName
8from Products.CPSSchemas.BasicWidgets import CPSWidget, CPSStringWidget, CPSEmailWidget
9from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget
[22]10from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget
[199]11from Products.CPSSchemas.Widget import widgetRegistry
12##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry
[22]13from DateTime.DateTime import DateTime
14from AccessControl import getSecurityManager
15
16from re import compile
17
18from zLOG import LOG, DEBUG
19
[295]20class CPSSelectWidgetForRecord(CPSSelectWidget): ###(
21    """Select widget. with record names"""
22    meta_type = 'Select Widget for Records'
23
24    field_types = ('CPS String Field',)
25    field_inits = ({'is_searchabletext': 1,},)
26
27    _properties = CPSSelectWidget._properties + (
28        {'id': 'record_id', 'type': 'string', 'mode': 'w',
29         'label': 'Record Id', 'is_required' : 1},
30        )
31
32    def render(self, mode, datastructure, **kw):
33        """Render in mode from datastructure."""
34        value = datastructure[self.getWidgetId()]
35        vocabulary = self._getVocabulary(datastructure)
36        portal = getToolByName(self, 'portal_url').getPortalObject()
37        cpsmcat = portal.translation_service
38        if mode == 'view':
39            if self.translated:
40                return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore'))
41            else:
42                return escape(vocabulary.get(value, value))
43        elif mode == 'edit':
44            html_widget_id = self.getHtmlWidgetId()
45            res = renderHtmlTag('select',
46                                name='%s.%s:records' % (self.record_id,html_widget_id),
47                                id=html_widget_id)
48            in_selection = 0
49            for k, v in vocabulary.items():
50                if self.translated:
51                    kw = {'value': k,
52                          'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore')
53                          }
54                else:
55                    kw = {'value': k, 'contents': v}
56                if value == k:
57                    kw['selected'] = 'selected'
58                    in_selection = 1
59                res += renderHtmlTag('option', **kw)
60            if value and not in_selection:
61                kw = {'value': value, 'contents': 'invalid: '+ str(value),
62                      'selected': 'selected'}
63                res += renderHtmlTag('option', **kw)
64            res += '</select>'
65            return res
66        raise RuntimeError('unknown mode %s' % mode)
67
68InitializeClass(CPSSelectWidgetForRecord)
69
70widgetRegistry.register(CPSSelectWidgetForRecord)
71
72###)
73
[373]74class CPSStringWidgetForRecord(CPSStringWidget): ###(
[295]75    """String widget."""
76    meta_type = 'String Widget For Record'
77
78    field_types = ('CPS String Field',)
79    field_inits = ({'is_searchabletext': 1,},)
80    _properties = CPSStringWidget._properties + (
81        {'id': 'record_id', 'type': 'string', 'mode': 'w',
82         'label': 'Record Id', 'is_required' : 1},
83        )
84
85    def render(self, mode, datastructure, **kw):
86        """Render in mode from datastructure."""
87        value = datastructure[self.getWidgetId()]
88        if mode == 'view':
89            return escape(value)
90        elif mode == 'edit':
91            # XXX TODO should use an other name than kw !
92            # XXX change this everywhere
93            html_widget_id = self.getHtmlWidgetId()
94            kw = {'type': 'text',
95                  'id'  : html_widget_id,
96                  'name': '%s.%s:records' % (self.record_id,html_widget_id),
97                  'value': escape(value),
98                  'size': self.display_width,
99                  }
100            if self.size_max:
101                kw['maxlength'] = self.size_max
102            return renderHtmlTag('input', **kw)
103        raise RuntimeError('unknown mode %s' % mode)
104
105InitializeClass(CPSStringWidgetForRecord)
106
107widgetRegistry.register(CPSStringWidgetForRecord)
108
[373]109###)
110
111class CertificateCourseIdWidget(CPSStringWidget): ###(
112    """ CertificateCourseId Widget"""
113    meta_type = "CertificateCourseId Widget"
114   
115    def validate(self, datastructure, **kw):
116        """Validate datastructure and update datamodel."""
117       
118        valid = CPSStringWidget.validate(self, datastructure, **kw)
119        if not valid:
120            return 0
121        else:
122            widget_id = self.getWidgetId()
123            value = datastructure[widget_id].upper()
124            err = 0
125            c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})]
[381]126            if hasattr(self.aq_parent,value):
127                err = 'Course %s already exists' % (value)
128            elif value not in c_ids:
[373]129                err = 'Course %s does not exist' % (value)
130            if err:
131                datastructure.setError(widget_id, err)
132            else:
133                datamodel = datastructure.getDataModel()
134                datamodel[self.fields[0]] = value
135           
136            return not err
137
138InitializeClass(CertificateCourseIdWidget)
139
140widgetRegistry.register(CertificateCourseIdWidget)
141
142
143###)
144
[388]145class WAeUPIdWidget(CPSStringWidget): ###(
146    """ WAeUPId Widget"""
147    meta_type = "WAeUPId Widget"
148   
149    def validate(self, datastructure, **kw):
150        """Validate datastructure and update datamodel."""
151       
152        valid = CPSStringWidget.validate(self, datastructure, **kw)
153        if not valid:
154            return 0
155        else:
156            widget_id = self.getWidgetId()
157            value = datastructure[widget_id].upper()
158            err = 0
159            if hasattr(self.aq_parent,value):
160                err = 'An object with the Id %s already exists in this context' % (value)
161            if err:
162                datastructure.setError(widget_id, err)
163            else:
164                datamodel = datastructure.getDataModel()
165                datamodel[self.fields[0]] = value
166           
167            return not err
168
169InitializeClass(WAeUPIdWidget)
170
171widgetRegistry.register(WAeUPIdWidget)
172
173
174###)
175
[47]176class StudentIdWidget(CPSStringWidget): ###(
[22]177    """ StudentId Widget"""
178    meta_type = "StudentId Widget"
[57]179    digits = 8
180    digits_str = "N"*digits
181    letters = 2
182    letters_str = "L"*letters
[22]183   
184    def validate(self, datastructure, **kw):
185        """Validate datastructure and update datamodel."""
186       
187        valid = CPSStringWidget.validate(self, datastructure, **kw)
188        if not valid:
189            return 0
190        else:
191            widget_id = self.getWidgetId()
192            value = datastructure[widget_id]
193            err = 0
[57]194            if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()):
195                err = 'invalid Student Id in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str)
[22]196            if err:
197                datastructure.setError(widget_id, err)
198            else:
199                datamodel = datastructure.getDataModel()
200                datamodel[self.fields[0]] = value
201           
202            return not err
203
204InitializeClass(StudentIdWidget)
205
[199]206widgetRegistry.register(StudentIdWidget)
[22]207
[199]208##Class StudentIdWidgetType(CPSWidgetType):
209##    """Student Id String widget type."""
210##    meta_type = "StudentId  Widget Type"
211##    cls = StudentIdWidget
212
[47]213###)
214
215class ScratchcardPinWidget(CPSStringWidget): ###(
[22]216    """ ScratchcardPin Widget"""
[199]217    meta_type = "Scratchcard Pin Widget"
[204]218    valid_pins = ['12345678',
219                  '23456789',
220                  '34567890',
221                  '45678901',
[22]222                  ]
223   
224    def validate(self, datastructure, **kw):
225        """Validate datastructure and update datamodel."""
226       
227        valid = CPSStringWidget.validate(self, datastructure, **kw)
228        if not valid:
229            return 0
230        else:
231            widget_id = self.getWidgetId()
232            value = datastructure[widget_id]
233            err = 0
[204]234            #import pdb; pdb.set_trace()
[57]235            if not value in self.valid_pins:
[22]236                err = 'invalid Pin'
237            if err:
238                datastructure.setError(widget_id, err)
239            else:
240                datamodel = datastructure.getDataModel()
241                datamodel[self.fields[0]] = value
242            return not err
243
244InitializeClass(ScratchcardPinWidget)
[199]245widgetRegistry.register(ScratchcardPinWidget)
[22]246
[47]247
248###)
249
[22]250###########
251
Note: See TracBrowser for help on using the repository browser.