[47] | 1 | #-*- mode: python; mode: fold -*- |
---|
[295] | 2 | |
---|
| 3 | from cgi import escape |
---|
| 4 | |
---|
[22] | 5 | from Globals import InitializeClass |
---|
[199] | 6 | ##from Products.CPSSchemas.Widget import CPSWidgetType |
---|
[295] | 7 | from Products.CMFCore.utils import getToolByName |
---|
| 8 | from Products.CPSSchemas.BasicWidgets import CPSWidget, CPSStringWidget, CPSEmailWidget |
---|
| 9 | from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget |
---|
[22] | 10 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
[199] | 11 | from Products.CPSSchemas.Widget import widgetRegistry |
---|
| 12 | ##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry |
---|
[22] | 13 | from DateTime.DateTime import DateTime |
---|
| 14 | from AccessControl import getSecurityManager |
---|
| 15 | |
---|
| 16 | from re import compile |
---|
| 17 | |
---|
| 18 | from zLOG import LOG, DEBUG |
---|
| 19 | |
---|
[295] | 20 | class 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() |
---|
[444] | 45 | res = renderHtmlTag('select', |
---|
| 46 | name='%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 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 | |
---|
| 68 | InitializeClass(CPSSelectWidgetForRecord) |
---|
| 69 | |
---|
| 70 | widgetRegistry.register(CPSSelectWidgetForRecord) |
---|
| 71 | |
---|
| 72 | ###) |
---|
| 73 | |
---|
[373] | 74 | class 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, |
---|
[444] | 96 | 'name': '%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 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 | |
---|
| 105 | InitializeClass(CPSStringWidgetForRecord) |
---|
| 106 | |
---|
| 107 | widgetRegistry.register(CPSStringWidgetForRecord) |
---|
| 108 | |
---|
[373] | 109 | ###) |
---|
| 110 | |
---|
| 111 | class CertificateCourseIdWidget(CPSStringWidget): ###( |
---|
| 112 | """ CertificateCourseId Widget""" |
---|
| 113 | meta_type = "CertificateCourseId Widget" |
---|
[444] | 114 | |
---|
[373] | 115 | def validate(self, datastructure, **kw): |
---|
| 116 | """Validate datastructure and update datamodel.""" |
---|
[444] | 117 | |
---|
[373] | 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): |
---|
[444] | 127 | err = 'Course %s already exists' % (value) |
---|
[381] | 128 | elif value not in c_ids: |
---|
[444] | 129 | err = 'Course %s does not exist' % (value) |
---|
[373] | 130 | if err: |
---|
| 131 | datastructure.setError(widget_id, err) |
---|
| 132 | else: |
---|
| 133 | datamodel = datastructure.getDataModel() |
---|
| 134 | datamodel[self.fields[0]] = value |
---|
[444] | 135 | |
---|
[373] | 136 | return not err |
---|
| 137 | |
---|
| 138 | InitializeClass(CertificateCourseIdWidget) |
---|
| 139 | |
---|
| 140 | widgetRegistry.register(CertificateCourseIdWidget) |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | ###) |
---|
| 144 | |
---|
[388] | 145 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
| 146 | """ WAeUPId Widget""" |
---|
| 147 | meta_type = "WAeUPId Widget" |
---|
[444] | 148 | |
---|
[388] | 149 | def validate(self, datastructure, **kw): |
---|
| 150 | """Validate datastructure and update datamodel.""" |
---|
[444] | 151 | |
---|
[388] | 152 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[422] | 153 | id_pat_str = r"\S" |
---|
| 154 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
[388] | 155 | if not valid: |
---|
| 156 | return 0 |
---|
| 157 | else: |
---|
| 158 | widget_id = self.getWidgetId() |
---|
| 159 | value = datastructure[widget_id].upper() |
---|
| 160 | err = 0 |
---|
[440] | 161 | if len(value.split()) > 1: |
---|
[444] | 162 | err = 'Invalid Id (Id contains space(s)' |
---|
[422] | 163 | elif hasattr(self.aq_parent,value): |
---|
[444] | 164 | err = 'An object with the Id %s already exists in this context' % (value) |
---|
[388] | 165 | if err: |
---|
| 166 | datastructure.setError(widget_id, err) |
---|
| 167 | else: |
---|
| 168 | datamodel = datastructure.getDataModel() |
---|
| 169 | datamodel[self.fields[0]] = value |
---|
[444] | 170 | |
---|
[388] | 171 | return not err |
---|
| 172 | |
---|
| 173 | InitializeClass(WAeUPIdWidget) |
---|
| 174 | |
---|
| 175 | widgetRegistry.register(WAeUPIdWidget) |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | ###) |
---|
| 179 | |
---|
[47] | 180 | class StudentIdWidget(CPSStringWidget): ###( |
---|
[22] | 181 | """ StudentId Widget""" |
---|
| 182 | meta_type = "StudentId Widget" |
---|
[57] | 183 | digits = 8 |
---|
| 184 | digits_str = "N"*digits |
---|
| 185 | letters = 2 |
---|
| 186 | letters_str = "L"*letters |
---|
[444] | 187 | |
---|
[22] | 188 | def validate(self, datastructure, **kw): |
---|
| 189 | """Validate datastructure and update datamodel.""" |
---|
[444] | 190 | |
---|
[22] | 191 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 192 | if not valid: |
---|
| 193 | return 0 |
---|
| 194 | else: |
---|
| 195 | widget_id = self.getWidgetId() |
---|
| 196 | value = datastructure[widget_id] |
---|
| 197 | err = 0 |
---|
[57] | 198 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
[444] | 199 | err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
[22] | 200 | if err: |
---|
| 201 | datastructure.setError(widget_id, err) |
---|
| 202 | else: |
---|
| 203 | datamodel = datastructure.getDataModel() |
---|
| 204 | datamodel[self.fields[0]] = value |
---|
[444] | 205 | |
---|
[22] | 206 | return not err |
---|
| 207 | |
---|
| 208 | InitializeClass(StudentIdWidget) |
---|
[199] | 209 | widgetRegistry.register(StudentIdWidget) |
---|
[22] | 210 | |
---|
[463] | 211 | ###) |
---|
[199] | 212 | |
---|
[463] | 213 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
| 214 | """ JambRegNo Widget""" |
---|
| 215 | meta_type = "JambRegNo Widget" |
---|
| 216 | digits = 8 |
---|
| 217 | digits_str = "N"*digits |
---|
| 218 | letters = 2 |
---|
| 219 | letters_str = "L"*letters |
---|
| 220 | |
---|
| 221 | def validate(self, datastructure, **kw): |
---|
| 222 | """Validate datastructure and update datamodel.""" |
---|
| 223 | |
---|
| 224 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 225 | if not valid: |
---|
| 226 | return 0 |
---|
| 227 | else: |
---|
| 228 | widget_id = self.getWidgetId() |
---|
| 229 | value = datastructure[widget_id] |
---|
| 230 | err = 0 |
---|
| 231 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 232 | err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
| 233 | else: |
---|
[472] | 234 | search = self.portal_catalog({'meta_type': 'StudentApplication', |
---|
[463] | 235 | 'jamb_reg_no': value, |
---|
| 236 | }) |
---|
[472] | 237 | if len(search) < 1: |
---|
[463] | 238 | err = 'No JAMB data for %s' % (value) |
---|
| 239 | if err: |
---|
| 240 | datastructure.setError(widget_id, err) |
---|
| 241 | else: |
---|
| 242 | datamodel = datastructure.getDataModel() |
---|
| 243 | datamodel[self.fields[0]] = value |
---|
| 244 | |
---|
| 245 | return not err |
---|
| 246 | |
---|
| 247 | InitializeClass(JambRegNoWidget) |
---|
| 248 | |
---|
| 249 | widgetRegistry.register(JambRegNoWidget) |
---|
[47] | 250 | ###) |
---|
| 251 | |
---|
[488] | 252 | class ScratchCardPin: ###( |
---|
| 253 | """the ScratchCardPin""" |
---|
| 254 | def __init__(self,prefix,batch_no,number): |
---|
| 255 | self.p = prefix |
---|
| 256 | self.b = batch_no |
---|
| 257 | self.n = number |
---|
| 258 | |
---|
| 259 | def __str__(self): |
---|
| 260 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
| 261 | ###) |
---|
| 262 | |
---|
[47] | 263 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
[22] | 264 | """ ScratchcardPin Widget""" |
---|
[199] | 265 | meta_type = "Scratchcard Pin Widget" |
---|
[488] | 266 | _properties = CPSWidget._properties + ( |
---|
| 267 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
| 268 | 'label': 'Prefix'}, |
---|
| 269 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 270 | 'label': 'Reference Field'}, |
---|
| 271 | |
---|
| 272 | ) |
---|
[204] | 273 | valid_pins = ['12345678', |
---|
| 274 | '23456789', |
---|
| 275 | '34567890', |
---|
| 276 | '45678901', |
---|
[22] | 277 | ] |
---|
[488] | 278 | prefix = '' |
---|
| 279 | reference = '' |
---|
| 280 | |
---|
| 281 | def prepare(self, datastructure, **kw): |
---|
| 282 | """Prepare datastructure from datamodel.""" |
---|
| 283 | datamodel = datastructure.getDataModel() |
---|
| 284 | v = datamodel[self.fields[0]] |
---|
| 285 | widget_id = self.getWidgetId() |
---|
| 286 | if v: |
---|
| 287 | b = '%s' % v.b |
---|
| 288 | n = '%s' % v.n |
---|
| 289 | else: |
---|
| 290 | b = n = '' |
---|
| 291 | datastructure[widget_id] = v |
---|
| 292 | datastructure[widget_id+'_b'] = b |
---|
| 293 | datastructure[widget_id+'_n'] = n |
---|
| 294 | |
---|
[22] | 295 | def validate(self, datastructure, **kw): |
---|
| 296 | """Validate datastructure and update datamodel.""" |
---|
[488] | 297 | widget_id = self.getWidgetId() |
---|
| 298 | v = datastructure[widget_id] |
---|
| 299 | err = 0 |
---|
| 300 | b = datastructure[widget_id+'_b'].strip() |
---|
| 301 | n = datastructure[widget_id+'_n'].strip() |
---|
| 302 | #import pdb; pdb.set_trace() |
---|
| 303 | if not n in self.valid_pins: |
---|
| 304 | err = 'invalid Pin' |
---|
| 305 | if err: |
---|
| 306 | datastructure.setError(widget_id, err) |
---|
| 307 | else: |
---|
| 308 | datamodel = datastructure.getDataModel() |
---|
| 309 | datamodel[self.fields[0]] = ScratchCardPin(self.prefix,b,n) |
---|
| 310 | datastructure[widget_id] = ScratchCardPin(self.prefix,b,n) |
---|
| 311 | datastructure[widget_id+'_b'] = b |
---|
| 312 | datastructure[widget_id+'_n'] = n |
---|
| 313 | #import pdb;pdb.set_trace() |
---|
| 314 | return not err |
---|
[444] | 315 | |
---|
[488] | 316 | def render(self, mode, datastructure, **kw): |
---|
| 317 | """Render in mode from datastructure.""" |
---|
| 318 | render_method = 'widget_scratch_card_pin_render' |
---|
| 319 | meth = getattr(self, render_method, None) |
---|
| 320 | if meth is None: |
---|
| 321 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 322 | % (render_method, self.getId())) |
---|
| 323 | |
---|
| 324 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
| 325 | # or create mode, because a default value has to be provided. |
---|
| 326 | #import pdb;pdb.set_trace() |
---|
| 327 | datamodel = datastructure.getDataModel() |
---|
| 328 | v = datamodel[self.fields[0]] |
---|
| 329 | if v: |
---|
| 330 | b = '%s' % v.b |
---|
| 331 | n = '%s' % v.n |
---|
[22] | 332 | else: |
---|
[488] | 333 | b = n = '' |
---|
| 334 | if mode in ['edit', 'create']: |
---|
[22] | 335 | widget_id = self.getWidgetId() |
---|
[488] | 336 | datastructure[widget_id] = v |
---|
| 337 | datastructure[widget_id+'_b'] = b |
---|
| 338 | datastructure[widget_id+'_n'] = n |
---|
| 339 | |
---|
[22] | 340 | |
---|
[488] | 341 | return meth(mode=mode, |
---|
| 342 | datastructure=datastructure, |
---|
| 343 | prefix=self.prefix, |
---|
| 344 | value=v, |
---|
| 345 | ) |
---|
| 346 | |
---|
| 347 | |
---|
[22] | 348 | InitializeClass(ScratchcardPinWidget) |
---|
[199] | 349 | widgetRegistry.register(ScratchcardPinWidget) |
---|
[22] | 350 | |
---|
[47] | 351 | |
---|
| 352 | ###) |
---|
| 353 | |
---|
[22] | 354 | ########### |
---|
| 355 | |
---|