[47] | 1 | #-*- mode: python; mode: fold -*- |
---|
[990] | 2 | # $Id: Widgets.py 1836 2007-05-30 10:41:15Z henrik $ |
---|
[295] | 3 | from cgi import escape |
---|
[502] | 4 | from types import * |
---|
[22] | 5 | from Globals import InitializeClass |
---|
[199] | 6 | ##from Products.CPSSchemas.Widget import CPSWidgetType |
---|
[295] | 7 | from Products.CMFCore.utils import getToolByName |
---|
[1772] | 8 | from Products.CPSSchemas.BasicWidgets import CPSBooleanWidget, CPSWidget, CPSStringWidget, CPSEmailWidget,CPSImageWidget |
---|
[295] | 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 |
---|
[502] | 15 | from Products.WAeUP_SRP.Students import getStudentByRegNo |
---|
[1747] | 16 | from Products.WAeUP_SRP.Academics import makeCertificateCode |
---|
[22] | 17 | from re import compile |
---|
[952] | 18 | import logging |
---|
[22] | 19 | |
---|
[952] | 20 | #from zLOG import LOG, DEBUG |
---|
[22] | 21 | |
---|
[295] | 22 | class CPSSelectWidgetForRecord(CPSSelectWidget): ###( |
---|
| 23 | """Select widget. with record names""" |
---|
| 24 | meta_type = 'Select Widget for Records' |
---|
| 25 | |
---|
| 26 | field_types = ('CPS String Field',) |
---|
| 27 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 28 | |
---|
| 29 | _properties = CPSSelectWidget._properties + ( |
---|
| 30 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 31 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 32 | ) |
---|
| 33 | |
---|
| 34 | def render(self, mode, datastructure, **kw): |
---|
| 35 | """Render in mode from datastructure.""" |
---|
| 36 | value = datastructure[self.getWidgetId()] |
---|
| 37 | vocabulary = self._getVocabulary(datastructure) |
---|
| 38 | portal = getToolByName(self, 'portal_url').getPortalObject() |
---|
| 39 | cpsmcat = portal.translation_service |
---|
| 40 | if mode == 'view': |
---|
| 41 | if self.translated: |
---|
| 42 | return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore')) |
---|
| 43 | else: |
---|
| 44 | return escape(vocabulary.get(value, value)) |
---|
| 45 | elif mode == 'edit': |
---|
| 46 | html_widget_id = self.getHtmlWidgetId() |
---|
[444] | 47 | res = renderHtmlTag('select', |
---|
| 48 | name='%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 49 | id=html_widget_id) |
---|
| 50 | in_selection = 0 |
---|
| 51 | for k, v in vocabulary.items(): |
---|
| 52 | if self.translated: |
---|
| 53 | kw = {'value': k, |
---|
| 54 | 'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore') |
---|
| 55 | } |
---|
| 56 | else: |
---|
| 57 | kw = {'value': k, 'contents': v} |
---|
| 58 | if value == k: |
---|
| 59 | kw['selected'] = 'selected' |
---|
| 60 | in_selection = 1 |
---|
| 61 | res += renderHtmlTag('option', **kw) |
---|
| 62 | if value and not in_selection: |
---|
| 63 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 64 | 'selected': 'selected'} |
---|
| 65 | res += renderHtmlTag('option', **kw) |
---|
| 66 | res += '</select>' |
---|
| 67 | return res |
---|
| 68 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 69 | |
---|
| 70 | InitializeClass(CPSSelectWidgetForRecord) |
---|
| 71 | |
---|
| 72 | widgetRegistry.register(CPSSelectWidgetForRecord) |
---|
| 73 | |
---|
| 74 | ###) |
---|
| 75 | |
---|
[373] | 76 | class CPSStringWidgetForRecord(CPSStringWidget): ###( |
---|
[295] | 77 | """String widget.""" |
---|
| 78 | meta_type = 'String Widget For Record' |
---|
| 79 | |
---|
| 80 | field_types = ('CPS String Field',) |
---|
| 81 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 82 | _properties = CPSStringWidget._properties + ( |
---|
| 83 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 84 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 85 | ) |
---|
| 86 | |
---|
| 87 | def render(self, mode, datastructure, **kw): |
---|
| 88 | """Render in mode from datastructure.""" |
---|
| 89 | value = datastructure[self.getWidgetId()] |
---|
| 90 | if mode == 'view': |
---|
| 91 | return escape(value) |
---|
| 92 | elif mode == 'edit': |
---|
| 93 | # XXX TODO should use an other name than kw ! |
---|
| 94 | # XXX change this everywhere |
---|
| 95 | html_widget_id = self.getHtmlWidgetId() |
---|
| 96 | kw = {'type': 'text', |
---|
| 97 | 'id' : html_widget_id, |
---|
[444] | 98 | 'name': '%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 99 | 'value': escape(value), |
---|
| 100 | 'size': self.display_width, |
---|
| 101 | } |
---|
| 102 | if self.size_max: |
---|
| 103 | kw['maxlength'] = self.size_max |
---|
| 104 | return renderHtmlTag('input', **kw) |
---|
| 105 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 106 | |
---|
| 107 | InitializeClass(CPSStringWidgetForRecord) |
---|
| 108 | |
---|
| 109 | widgetRegistry.register(CPSStringWidgetForRecord) |
---|
| 110 | |
---|
[373] | 111 | ###) |
---|
| 112 | |
---|
| 113 | class CertificateCourseIdWidget(CPSStringWidget): ###( |
---|
| 114 | """ CertificateCourseId Widget""" |
---|
| 115 | meta_type = "CertificateCourseId Widget" |
---|
[444] | 116 | |
---|
[373] | 117 | def validate(self, datastructure, **kw): |
---|
| 118 | """Validate datastructure and update datamodel.""" |
---|
[444] | 119 | |
---|
[373] | 120 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 121 | if not valid: |
---|
| 122 | return 0 |
---|
| 123 | else: |
---|
| 124 | widget_id = self.getWidgetId() |
---|
| 125 | value = datastructure[widget_id].upper() |
---|
| 126 | err = 0 |
---|
| 127 | c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] |
---|
[381] | 128 | if hasattr(self.aq_parent,value): |
---|
[1793] | 129 | err = 'Course %s already exists' % (value) |
---|
[381] | 130 | elif value not in c_ids: |
---|
[1793] | 131 | err = 'Course %s does not exist' % (value) |
---|
[373] | 132 | if err: |
---|
| 133 | datastructure.setError(widget_id, err) |
---|
| 134 | else: |
---|
| 135 | datamodel = datastructure.getDataModel() |
---|
| 136 | datamodel[self.fields[0]] = value |
---|
[444] | 137 | |
---|
[373] | 138 | return not err |
---|
| 139 | |
---|
| 140 | InitializeClass(CertificateCourseIdWidget) |
---|
| 141 | |
---|
| 142 | widgetRegistry.register(CertificateCourseIdWidget) |
---|
[551] | 143 | ###) |
---|
[373] | 144 | |
---|
[551] | 145 | class CourseIdWidget(CPSStringWidget): ###( |
---|
| 146 | """ CourseId Widget""" |
---|
| 147 | meta_type = "CourseId 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 | res = self.portal_catalog(meta_type= "Course",id = value) |
---|
| 160 | if len(res) > 0: |
---|
[1793] | 161 | err = 'Course %s already exists' % (value) |
---|
[551] | 162 | if err: |
---|
| 163 | datastructure.setError(widget_id, err) |
---|
| 164 | else: |
---|
| 165 | datamodel = datastructure.getDataModel() |
---|
| 166 | datamodel[self.fields[0]] = value |
---|
| 167 | |
---|
| 168 | return not err |
---|
| 169 | |
---|
| 170 | InitializeClass(CourseIdWidget) |
---|
| 171 | |
---|
| 172 | widgetRegistry.register(CourseIdWidget) |
---|
| 173 | |
---|
| 174 | |
---|
[373] | 175 | ###) |
---|
| 176 | |
---|
[1820] | 177 | class WAeUPStudyModeWidget(CPSSelectWidget): ###( |
---|
| 178 | """WAeUP StudyMode widget.""" |
---|
| 179 | meta_type = 'WAeUP StudyMode Widget' |
---|
| 180 | vocabulary = 'entry_modes' |
---|
| 181 | |
---|
| 182 | def _getStudyModes(self): |
---|
| 183 | voc = getattr(self.portal_vocabularies,self.vocabulary) |
---|
| 184 | d = {} |
---|
| 185 | for k,v in voc.items(): |
---|
| 186 | d[k] = v |
---|
| 187 | return d |
---|
| 188 | |
---|
| 189 | def validate(self, datastructure, **kw): |
---|
| 190 | """Validate datastructure and update datamodel.""" |
---|
| 191 | widget_id = self.getWidgetId() |
---|
| 192 | value = datastructure[widget_id] |
---|
| 193 | try: |
---|
| 194 | v = str(value) |
---|
| 195 | except ValueError: |
---|
| 196 | datastructure.setError(widget_id, "'%s' not a valid session key" % value) |
---|
| 197 | return 0 |
---|
| 198 | studymodes = self._getStudyModes() |
---|
| 199 | if not value: |
---|
[1836] | 200 | v = value = 'ume_ft' |
---|
[1820] | 201 | #import pdb;pdb.set_trace() |
---|
| 202 | if not studymodes.has_key(value): |
---|
| 203 | datastructure.setError(widget_id, "'%s' not a valid session key" % v) |
---|
| 204 | return 0 |
---|
| 205 | if self.is_required and not len(v): |
---|
| 206 | datastructure.setError(widget_id, "session key required") |
---|
| 207 | return 0 |
---|
| 208 | |
---|
| 209 | datamodel = datastructure.getDataModel() |
---|
| 210 | datamodel[self.fields[0]] = v |
---|
| 211 | return 1 |
---|
| 212 | |
---|
| 213 | def render(self, mode, datastructure, **kw): |
---|
| 214 | """Render in mode from datastructure.""" |
---|
| 215 | value = datastructure[self.getWidgetId()] |
---|
| 216 | studymodes = self._getStudyModes() |
---|
| 217 | if mode == 'view': |
---|
| 218 | return escape(studymodes.get(value, value)) |
---|
| 219 | elif mode == 'edit': |
---|
| 220 | html_widget_id = self.getHtmlWidgetId() |
---|
| 221 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 222 | in_selection = 0 |
---|
| 223 | vocabulary_items = studymodes.items() |
---|
| 224 | if self.sorted: |
---|
| 225 | vocabulary_items.sort(key=operator.itemgetter(1)) |
---|
| 226 | for k, v in vocabulary_items: |
---|
| 227 | kw = {'value': k, 'contents': v} |
---|
| 228 | if value == k: |
---|
| 229 | kw['selected'] = 'selected' |
---|
| 230 | in_selection = 1 |
---|
| 231 | res += renderHtmlTag('option', **kw) |
---|
| 232 | if value and not in_selection: |
---|
| 233 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 234 | 'selected': 'selected'} |
---|
| 235 | res += renderHtmlTag('option', **kw) |
---|
| 236 | res += '</select>' |
---|
| 237 | return res |
---|
| 238 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 239 | |
---|
| 240 | InitializeClass(WAeUPStudyModeWidget) |
---|
| 241 | |
---|
| 242 | widgetRegistry.register(WAeUPStudyModeWidget) |
---|
| 243 | |
---|
| 244 | ###) |
---|
| 245 | |
---|
[1804] | 246 | class WAeUPSessionWidget(CPSSelectWidget): ###( |
---|
| 247 | """WAeUP Session widget.""" |
---|
| 248 | meta_type = 'WAeUP Session Widget' |
---|
| 249 | |
---|
| 250 | def _getSessions(self): |
---|
| 251 | current_year = DateTime().year() |
---|
| 252 | d = {'xx': 'unknown'} |
---|
| 253 | for y in range(current_year - 9,current_year + 1): |
---|
| 254 | d['%s' % str(y)[-2:]] = '%4d/%4d' % (y,y+1) |
---|
| 255 | return d |
---|
| 256 | |
---|
| 257 | def validate(self, datastructure, **kw): |
---|
| 258 | """Validate datastructure and update datamodel.""" |
---|
| 259 | widget_id = self.getWidgetId() |
---|
| 260 | value = datastructure[widget_id] |
---|
| 261 | try: |
---|
| 262 | v = str(value) |
---|
| 263 | except ValueError: |
---|
| 264 | datastructure.setError(widget_id, "'%s' not a valid session key" % value) |
---|
| 265 | return 0 |
---|
[1805] | 266 | |
---|
[1804] | 267 | if len(v) == 1: |
---|
| 268 | v = value = '0%c' % v |
---|
| 269 | elif not value: |
---|
| 270 | v = value = self.getSessionId()[-2:] |
---|
| 271 | #import pdb;pdb.set_trace() |
---|
| 272 | sessions = self._getSessions() |
---|
| 273 | if not sessions.has_key(value): |
---|
| 274 | datastructure.setError(widget_id, "'%s' not a valid session key" % v) |
---|
| 275 | return 0 |
---|
| 276 | if self.is_required and not len(v): |
---|
| 277 | datastructure.setError(widget_id, "session key required") |
---|
| 278 | return 0 |
---|
| 279 | |
---|
| 280 | datamodel = datastructure.getDataModel() |
---|
| 281 | datamodel[self.fields[0]] = v |
---|
| 282 | return 1 |
---|
| 283 | |
---|
| 284 | def render(self, mode, datastructure, **kw): |
---|
| 285 | """Render in mode from datastructure.""" |
---|
| 286 | value = datastructure[self.getWidgetId()] |
---|
| 287 | sessions = self._getSessions() |
---|
| 288 | if mode == 'view': |
---|
| 289 | return escape(sessions.get(value, value)) |
---|
| 290 | elif mode == 'edit': |
---|
| 291 | html_widget_id = self.getHtmlWidgetId() |
---|
| 292 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 293 | in_selection = 0 |
---|
| 294 | vocabulary_items = sessions.items() |
---|
| 295 | if self.sorted: |
---|
| 296 | vocabulary_items.sort(key=operator.itemgetter(1)) |
---|
| 297 | for k, v in vocabulary_items: |
---|
| 298 | kw = {'value': k, 'contents': v} |
---|
| 299 | if value == k: |
---|
| 300 | kw['selected'] = 'selected' |
---|
| 301 | in_selection = 1 |
---|
| 302 | res += renderHtmlTag('option', **kw) |
---|
| 303 | if value and not in_selection: |
---|
| 304 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 305 | 'selected': 'selected'} |
---|
| 306 | res += renderHtmlTag('option', **kw) |
---|
| 307 | res += '</select>' |
---|
| 308 | return res |
---|
| 309 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 310 | |
---|
| 311 | InitializeClass(WAeUPSessionWidget) |
---|
| 312 | |
---|
| 313 | widgetRegistry.register(WAeUPSessionWidget) |
---|
| 314 | |
---|
| 315 | ###) |
---|
| 316 | |
---|
| 317 | class WAeUPLevelWidget(CPSSelectWidget): ###( |
---|
| 318 | """WAeUP Level Widget.""" |
---|
| 319 | meta_type = 'WAeUP Level Widget' |
---|
| 320 | |
---|
| 321 | def _getLevels(self): |
---|
| 322 | d = {'999':'unknown'} |
---|
| 323 | for y in range(100,800,100): |
---|
| 324 | d['%s' % str(y)] = 'Year %1d (%3d Level)' % (y/100,y) |
---|
| 325 | return d |
---|
| 326 | |
---|
| 327 | def validate(self, datastructure, **kw): |
---|
| 328 | """Validate datastructure and update datamodel.""" |
---|
| 329 | widget_id = self.getWidgetId() |
---|
| 330 | value = datastructure[widget_id] |
---|
| 331 | try: |
---|
| 332 | v = str(value) |
---|
| 333 | except ValueError: |
---|
| 334 | datastructure.setError(widget_id, "'%s' not a valid level key" % value) |
---|
| 335 | return 0 |
---|
[1805] | 336 | |
---|
| 337 | if not value: |
---|
[1804] | 338 | v = value = '100' |
---|
| 339 | #import pdb;pdb.set_trace() |
---|
| 340 | levels = self._getLevels() |
---|
| 341 | if not levels.has_key(value): |
---|
| 342 | datastructure.setError(widget_id, "'%s' not a valid level" % v) |
---|
| 343 | return 0 |
---|
| 344 | if self.is_required and not len(v): |
---|
| 345 | datastructure.setError(widget_id, "level key required") |
---|
| 346 | return 0 |
---|
| 347 | |
---|
| 348 | datamodel = datastructure.getDataModel() |
---|
| 349 | datamodel[self.fields[0]] = v |
---|
| 350 | return 1 |
---|
| 351 | |
---|
| 352 | def render(self, mode, datastructure, **kw): |
---|
| 353 | """Render in mode from datastructure.""" |
---|
| 354 | value = datastructure[self.getWidgetId()] |
---|
| 355 | levels = self._getLevels() |
---|
| 356 | if mode == 'view': |
---|
| 357 | return escape(levels.get(value, value)) |
---|
| 358 | elif mode == 'edit': |
---|
| 359 | html_widget_id = self.getHtmlWidgetId() |
---|
| 360 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 361 | in_selection = 0 |
---|
| 362 | vocabulary_items = levels.items() |
---|
| 363 | if self.sorted: |
---|
| 364 | vocabulary_items.sort(key=operator.itemgetter(1)) |
---|
| 365 | for k, v in vocabulary_items: |
---|
| 366 | kw = {'value': k, 'contents': v} |
---|
| 367 | if value == k: |
---|
| 368 | kw['selected'] = 'selected' |
---|
| 369 | in_selection = 1 |
---|
| 370 | res += renderHtmlTag('option', **kw) |
---|
| 371 | if value and not in_selection: |
---|
| 372 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 373 | 'selected': 'selected'} |
---|
| 374 | res += renderHtmlTag('option', **kw) |
---|
| 375 | res += '</select>' |
---|
| 376 | return res |
---|
| 377 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 378 | |
---|
| 379 | InitializeClass(WAeUPLevelWidget) |
---|
| 380 | |
---|
| 381 | widgetRegistry.register(WAeUPLevelWidget) |
---|
| 382 | |
---|
| 383 | ###) |
---|
| 384 | |
---|
| 385 | class WAeUPVerdictWidget(CPSSelectWidget): ###( |
---|
| 386 | """WAeUP Verdict widget.""" |
---|
| 387 | meta_type = 'WAeUP Verdict Widget' |
---|
| 388 | |
---|
| 389 | # XXX make a menu for the vocabulary. |
---|
| 390 | vocabulary = 'verdicts' |
---|
| 391 | |
---|
| 392 | # Associating the widget label with an input area to improve the widget |
---|
| 393 | # accessibility. |
---|
| 394 | has_input_area = True |
---|
| 395 | |
---|
| 396 | def _getVerdicts(self,datastructure): |
---|
| 397 | voc = getattr(self.portal_vocabularies,self.vocabulary) |
---|
| 398 | d = {} |
---|
| 399 | for k,v in voc.items(): |
---|
| 400 | d[k] = v |
---|
| 401 | return d |
---|
| 402 | |
---|
| 403 | def validate(self, datastructure, **kw): |
---|
| 404 | """Validate datastructure and update datamodel.""" |
---|
| 405 | widget_id = self.getWidgetId() |
---|
| 406 | value = datastructure[widget_id] |
---|
| 407 | try: |
---|
| 408 | v = str(value) |
---|
| 409 | except ValueError: |
---|
| 410 | datastructure.setError(widget_id, "'%s' not a valid verdict key" % value) |
---|
| 411 | return 0 |
---|
| 412 | #import pdb;pdb.set_trace() |
---|
| 413 | verdicts = self._getVerdicts(datastructure) |
---|
| 414 | if not value: |
---|
| 415 | v = value = verdicts['N/A'] |
---|
| 416 | if not verdicts.has_key(value): |
---|
| 417 | datastructure.setError(widget_id, "'%s' not a valid verdict key" % v) |
---|
| 418 | return 0 |
---|
| 419 | if self.is_required and not len(v): |
---|
| 420 | datastructure.setError(widget_id, "verdict required") |
---|
| 421 | return 0 |
---|
| 422 | |
---|
| 423 | datamodel = datastructure.getDataModel() |
---|
| 424 | datamodel[self.fields[0]] = v |
---|
| 425 | return 1 |
---|
| 426 | |
---|
| 427 | def render(self, mode, datastructure, **kw): |
---|
| 428 | """Render in mode from datastructure.""" |
---|
| 429 | value = datastructure[self.getWidgetId()] |
---|
| 430 | verdicts = self._getVerdicts(datastructure) |
---|
| 431 | if mode == 'view': |
---|
| 432 | return escape(verdicts.get(value, value)) |
---|
| 433 | elif mode == 'edit': |
---|
| 434 | html_widget_id = self.getHtmlWidgetId() |
---|
| 435 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 436 | in_selection = 0 |
---|
| 437 | vocabulary_items = verdicts.items() |
---|
| 438 | if self.sorted: |
---|
| 439 | vocabulary_items.sort(key=operator.itemgetter(1)) |
---|
| 440 | for k, v in vocabulary_items: |
---|
| 441 | kw = {'value': k, 'contents': v} |
---|
| 442 | if value == k: |
---|
| 443 | kw['selected'] = 'selected' |
---|
| 444 | in_selection = 1 |
---|
| 445 | res += renderHtmlTag('option', **kw) |
---|
| 446 | if value and not in_selection: |
---|
| 447 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 448 | 'selected': 'selected'} |
---|
| 449 | res += renderHtmlTag('option', **kw) |
---|
| 450 | res += '</select>' |
---|
| 451 | return res |
---|
| 452 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 453 | |
---|
| 454 | InitializeClass(WAeUPVerdictWidget) |
---|
| 455 | |
---|
| 456 | widgetRegistry.register(WAeUPVerdictWidget) |
---|
| 457 | |
---|
| 458 | ###) |
---|
| 459 | |
---|
[714] | 460 | class WAeUPReservedRoomsWidget(CPSStringWidget): ###( |
---|
| 461 | """ WAeUPReservedRooms Widget""" |
---|
| 462 | meta_type = "WAeUPReservedRooms Widget" |
---|
| 463 | |
---|
| 464 | def validate(self, datastructure, **kw): |
---|
| 465 | """Validate datastructure and update datamodel.""" |
---|
| 466 | import re |
---|
| 467 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 468 | if not valid: |
---|
| 469 | return 0 |
---|
| 470 | else: |
---|
| 471 | widget_id = self.getWidgetId() |
---|
| 472 | value = datastructure[widget_id] |
---|
| 473 | err = 0 |
---|
| 474 | try: |
---|
[926] | 475 | reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',value) |
---|
[714] | 476 | if r] |
---|
[1038] | 477 | except (ValueError,IndexError),msg: |
---|
[714] | 478 | err = str(msg) |
---|
| 479 | if err: |
---|
| 480 | datastructure.setError(widget_id, err) |
---|
| 481 | else: |
---|
| 482 | datamodel = datastructure.getDataModel() |
---|
| 483 | datamodel[self.fields[0]] = value |
---|
| 484 | return not err |
---|
| 485 | |
---|
| 486 | InitializeClass(WAeUPReservedRoomsWidget) |
---|
| 487 | |
---|
| 488 | widgetRegistry.register(WAeUPReservedRoomsWidget) |
---|
| 489 | ###) |
---|
| 490 | |
---|
[388] | 491 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
| 492 | """ WAeUPId Widget""" |
---|
| 493 | meta_type = "WAeUPId Widget" |
---|
[444] | 494 | |
---|
[388] | 495 | def validate(self, datastructure, **kw): |
---|
| 496 | """Validate datastructure and update datamodel.""" |
---|
[444] | 497 | |
---|
[388] | 498 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[422] | 499 | id_pat_str = r"\S" |
---|
| 500 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
[388] | 501 | if not valid: |
---|
| 502 | return 0 |
---|
| 503 | else: |
---|
[586] | 504 | portal_type_query = {'query':['Faculty', |
---|
| 505 | 'Department', |
---|
| 506 | 'Course', |
---|
| 507 | 'Certificate', |
---|
| 508 | 'CertificateCourse',]} |
---|
[388] | 509 | widget_id = self.getWidgetId() |
---|
| 510 | value = datastructure[widget_id].upper() |
---|
| 511 | err = 0 |
---|
[440] | 512 | if len(value.split()) > 1: |
---|
[783] | 513 | err = 'Invalid Id, Id contains space(s).' |
---|
[586] | 514 | elif self.portal_catalog(portal_type=portal_type_query,id=value): |
---|
[1718] | 515 | brain = self.portal_catalog(portal_type=portal_type_query,id=value)[0] |
---|
[1783] | 516 | err = 'An %s object with the Id %s already exists at %s.'\ |
---|
[1718] | 517 | % (brain.portal_type,value,brain.getPath()) |
---|
[388] | 518 | if err: |
---|
| 519 | datastructure.setError(widget_id, err) |
---|
| 520 | else: |
---|
| 521 | datamodel = datastructure.getDataModel() |
---|
| 522 | datamodel[self.fields[0]] = value |
---|
[444] | 523 | |
---|
[388] | 524 | return not err |
---|
| 525 | |
---|
| 526 | InitializeClass(WAeUPIdWidget) |
---|
| 527 | |
---|
| 528 | widgetRegistry.register(WAeUPIdWidget) |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | ###) |
---|
| 532 | |
---|
[1025] | 533 | class StudyCourseWidget(CPSStringWidget): ###( |
---|
| 534 | """ StudyCourse Widget""" |
---|
| 535 | meta_type = "StudyCourse Widget" |
---|
| 536 | |
---|
| 537 | def validate(self, datastructure, **kw): |
---|
| 538 | """Validate datastructure and update datamodel.""" |
---|
| 539 | #from Products.zdb import set_trace |
---|
| 540 | #set_trace() |
---|
[1747] | 541 | ## valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 542 | ## if not valid: |
---|
| 543 | ## return 0 |
---|
| 544 | widget_id = self.getWidgetId() |
---|
| 545 | value = makeCertificateCode(datastructure[widget_id]).upper() |
---|
[1025] | 546 | id_pat_str = r"\S" |
---|
| 547 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
[1747] | 548 | err = 0 |
---|
| 549 | if len(value.split()) > 1: |
---|
| 550 | err = 'Invalid Id, Id contains space(s).' |
---|
| 551 | elif not self.portal_catalog(portal_type='Certificate',id=value): |
---|
| 552 | err = 'No such certificate' |
---|
| 553 | if err: |
---|
| 554 | datastructure.setError(widget_id, err) |
---|
[1025] | 555 | else: |
---|
[1747] | 556 | datamodel = datastructure.getDataModel() |
---|
| 557 | datamodel[self.fields[0]] = value |
---|
| 558 | return not err |
---|
[1025] | 559 | |
---|
| 560 | InitializeClass(StudyCourseWidget) |
---|
| 561 | |
---|
| 562 | widgetRegistry.register(StudyCourseWidget) |
---|
| 563 | ###) |
---|
| 564 | |
---|
[463] | 565 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
| 566 | """ JambRegNo Widget""" |
---|
| 567 | meta_type = "JambRegNo Widget" |
---|
[1169] | 568 | _properties = CPSStringWidget._properties + ( |
---|
| 569 | {'id': 'catalog', 'type': 'string', 'mode': 'w', |
---|
| 570 | 'label': 'Catalog to search'}, |
---|
| 571 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 572 | 'label': 'Reference Field'}, |
---|
| 573 | ) |
---|
| 574 | catalog = "portal_pumeresults" #the catalog to search for jamb_reg_no |
---|
| 575 | reference = "" |
---|
[463] | 576 | digits = 8 |
---|
| 577 | digits_str = "N"*digits |
---|
| 578 | letters = 2 |
---|
| 579 | letters_str = "L"*letters |
---|
| 580 | def validate(self, datastructure, **kw): |
---|
| 581 | """Validate datastructure and update datamodel.""" |
---|
| 582 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[1169] | 583 | jamb_nr_catalog = getattr(self,self.catalog) |
---|
| 584 | widget_id = self.getWidgetId() |
---|
| 585 | value = datastructure[widget_id].upper() |
---|
| 586 | err = 0 |
---|
| 587 | if not valid: #or not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 588 | #err = 'Invalid number, the expected format is: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
[1793] | 589 | err = 'Invalid number' |
---|
[1169] | 590 | elif self.reference == '': |
---|
| 591 | #s = getStudentByRegNo(self,value) |
---|
| 592 | pume = jamb_nr_catalog(jamb_reg_no = value) |
---|
| 593 | if len(pume) < 1: |
---|
[1836] | 594 | err = 'No student record with registration number %s in %s.' % (value,jamb_nr_catalog.id) |
---|
[1169] | 595 | else: |
---|
| 596 | datastructure['pume'] = pume[0] |
---|
[463] | 597 | else: |
---|
[1169] | 598 | #import pdb;pdb.set_trace() |
---|
| 599 | record = datastructure[self.reference] |
---|
| 600 | #jamb_reg_no = getattr(record,widget_id) |
---|
| 601 | jamb_reg_no = record.Entryregno |
---|
| 602 | if jamb_reg_no != value: |
---|
[1783] | 603 | err = 'Registration number does not match.' |
---|
[1169] | 604 | if err: |
---|
| 605 | datastructure.setError(widget_id, err) |
---|
| 606 | else: |
---|
| 607 | datamodel = datastructure.getDataModel() |
---|
| 608 | datamodel[self.fields[0]] = value |
---|
| 609 | return not err |
---|
[463] | 610 | |
---|
| 611 | InitializeClass(JambRegNoWidget) |
---|
| 612 | |
---|
| 613 | widgetRegistry.register(JambRegNoWidget) |
---|
[47] | 614 | ###) |
---|
| 615 | |
---|
[1175] | 616 | class SecretWidget(CPSStringWidget): ###( |
---|
| 617 | """ Secret Widget""" |
---|
| 618 | meta_type = "Secret Widget" |
---|
| 619 | _properties = CPSStringWidget._properties + ( |
---|
| 620 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 621 | 'label': 'Reference Record'}, |
---|
| 622 | {'id': 'check_fields', 'type': 'tokens', 'mode': 'w', |
---|
| 623 | 'label': 'Fields to check'}, |
---|
| 624 | ) |
---|
| 625 | reference = "student" |
---|
[1747] | 626 | matric_no_catalog = 'returning_import' |
---|
[1175] | 627 | check_fields = ("Firstname", "Middlename","Lastname") |
---|
| 628 | def validate(self, datastructure, **kw): |
---|
| 629 | """Validate datastructure and update datamodel.""" |
---|
[1571] | 630 | logger = logging.getLogger('Widgets.SecretWidget.validate') |
---|
[1175] | 631 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 632 | widget_id = self.getWidgetId() |
---|
| 633 | value = datastructure[widget_id].upper() |
---|
| 634 | err = 0 |
---|
[1189] | 635 | record = datastructure.get(self.reference,None) |
---|
[1747] | 636 | #import pdb;pdb.set_trace() |
---|
[1379] | 637 | if not valid or len(value) < 2: |
---|
[1793] | 638 | err = 'Invalid string' |
---|
[1189] | 639 | elif not record or datastructure.errors: |
---|
[1177] | 640 | err = 0 |
---|
[1175] | 641 | else: |
---|
| 642 | found = False |
---|
| 643 | cvs = [] |
---|
| 644 | for field in self.check_fields: |
---|
| 645 | cv = getattr(record,field).upper() |
---|
[1243] | 646 | if len(cv.split()) > 1: |
---|
| 647 | for splited in cv.split(): |
---|
[1747] | 648 | cvs.append(splited.strip()) |
---|
[1243] | 649 | else: |
---|
| 650 | cvs.append(cv) |
---|
[1747] | 651 | for cv in cvs: |
---|
[1175] | 652 | if cv == value.upper(): |
---|
| 653 | found = True |
---|
| 654 | break |
---|
| 655 | matric_no = record.matric_no |
---|
| 656 | name = " ".join(cvs) |
---|
| 657 | if not found: |
---|
[1573] | 658 | logger.info('%(matric_no)s did not find %(value)s in %(name)s' % vars()) |
---|
| 659 | err = 'No name does match.' |
---|
[1175] | 660 | else: |
---|
[1571] | 661 | logger.info('%(matric_no)s found %(value)s in %(name)s' % vars()) |
---|
[1175] | 662 | if err: |
---|
| 663 | datastructure.setError(widget_id, err) |
---|
| 664 | else: |
---|
| 665 | datamodel = datastructure.getDataModel() |
---|
| 666 | datamodel[self.fields[0]] = value |
---|
| 667 | return not err |
---|
| 668 | |
---|
| 669 | InitializeClass(SecretWidget) |
---|
| 670 | |
---|
| 671 | widgetRegistry.register(SecretWidget) |
---|
| 672 | ###) |
---|
| 673 | |
---|
[1804] | 674 | class WAeUPSexWidget(CPSBooleanWidget): ###( |
---|
[1772] | 675 | """WAeUP sex widget.""" |
---|
| 676 | meta_type = 'WAeUP Sex Widget' |
---|
| 677 | |
---|
| 678 | def validate(self, datastructure, **kw): |
---|
| 679 | """Validate datastructure and update datamodel.""" |
---|
| 680 | value = datastructure[self.getWidgetId()] |
---|
| 681 | |
---|
| 682 | if self.render_format not in self.render_formats: |
---|
| 683 | self.render_format = 'select' |
---|
| 684 | |
---|
| 685 | female = value in ('F','f','Female','female',"True",True) |
---|
| 686 | male = value in ('M','m','Male','male','False',False) |
---|
| 687 | if not female and not male: |
---|
| 688 | datastructure.setError(self.getWidgetId(), |
---|
| 689 | "invalid sex %s" % value) |
---|
| 690 | return 0 |
---|
| 691 | elif female: |
---|
| 692 | v = True |
---|
| 693 | else: |
---|
| 694 | v = False |
---|
| 695 | datamodel = datastructure.getDataModel() |
---|
| 696 | datamodel[self.fields[0]] = v |
---|
| 697 | return 1 |
---|
| 698 | |
---|
| 699 | InitializeClass(WAeUPSexWidget) |
---|
| 700 | |
---|
| 701 | widgetRegistry.register(WAeUPSexWidget) |
---|
| 702 | |
---|
[1804] | 703 | ###) |
---|
| 704 | |
---|
[1146] | 705 | class MatricNoWidget(CPSStringWidget): ###( |
---|
| 706 | """ MatricNo Widget""" |
---|
| 707 | meta_type = "MatricNo Widget" |
---|
[1747] | 708 | |
---|
| 709 | _properties = CPSStringWidget._properties + ( |
---|
| 710 | {'id': 'matric_no_catalog', 'type': 'string', 'mode': 'w', |
---|
| 711 | 'label': 'Catalog to search for MatricNo'}, |
---|
| 712 | { 'id': 'results_catalog', 'type': 'string', 'mode': 'w', |
---|
| 713 | 'label': 'Results Catalog'}, |
---|
| 714 | ) |
---|
| 715 | matric_no_catalog = "" #the catalog to search for matric_no |
---|
[1748] | 716 | results_catalog = "results_import" #results catalog |
---|
[1747] | 717 | |
---|
[1146] | 718 | def validate(self, datastructure, **kw): |
---|
| 719 | """Validate datastructure and update datamodel.""" |
---|
[1747] | 720 | #import pdb;pdb.set_trace() |
---|
[1146] | 721 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[1571] | 722 | logger = logging.getLogger('Widgets.MatricNoWidget.validate') |
---|
[1747] | 723 | returning = getattr(self,self.matric_no_catalog) |
---|
| 724 | results = getattr(self,self.results_catalog,None) |
---|
[1146] | 725 | err = 0 |
---|
[1189] | 726 | widget_id = self.getWidgetId() |
---|
| 727 | value = datastructure[widget_id] |
---|
| 728 | if not valid or not value: |
---|
[1793] | 729 | err = 'Invalid string %s' % value |
---|
[1573] | 730 | logger.info('Invalid matric_no string %s' % value) |
---|
[1146] | 731 | else: |
---|
[1189] | 732 | value = value.upper() |
---|
[1177] | 733 | datastructure['student'] = None |
---|
[1146] | 734 | while not err: |
---|
[1151] | 735 | res = returning(matric_no = value) |
---|
[1146] | 736 | if len(res) < 1: |
---|
[1573] | 737 | logger.info('matric_no %s not found' % value) |
---|
[1836] | 738 | err = 'No student with matriculation number %s in %s' % (value, returning.id) |
---|
[1747] | 739 | break |
---|
[1146] | 740 | datastructure['student'] = res[0] |
---|
[1747] | 741 | if results is not None: |
---|
| 742 | res = results(matric_no = value) |
---|
| 743 | if len(res) < 1: |
---|
[1814] | 744 | err = 'No results for matriculation number %s' % (value) |
---|
[1747] | 745 | continue |
---|
| 746 | datastructure['results'] = res |
---|
[1146] | 747 | break |
---|
[1189] | 748 | if err: |
---|
| 749 | datastructure.setError(widget_id, err) |
---|
| 750 | else: |
---|
| 751 | datamodel = datastructure.getDataModel() |
---|
| 752 | datamodel[self.fields[0]] = value |
---|
| 753 | return not err |
---|
[1146] | 754 | |
---|
| 755 | InitializeClass(MatricNoWidget) |
---|
| 756 | |
---|
| 757 | widgetRegistry.register(MatricNoWidget) |
---|
| 758 | ###) |
---|
| 759 | |
---|
[1393] | 760 | class StudentIdWidget(CPSStringWidget): ###( |
---|
| 761 | """ StudentId Widget""" |
---|
| 762 | meta_type = "StudentId Widget" |
---|
| 763 | def validate(self, datastructure, **kw): |
---|
| 764 | """Validate datastructure and update datamodel.""" |
---|
| 765 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[1571] | 766 | logger = logging.getLogger('Widgets.StudentIdWidget.validate') |
---|
[1393] | 767 | #import pdb;pdb.set_trace() |
---|
| 768 | s_cat = self.students_catalog |
---|
| 769 | err = 0 |
---|
| 770 | widget_id = self.getWidgetId() |
---|
| 771 | value = datastructure[widget_id] |
---|
| 772 | if not valid or not value: |
---|
[1793] | 773 | err = 'Invalid Id string %s' % value |
---|
[1573] | 774 | logger.info('Invalid id string %s' % value) |
---|
[1449] | 775 | datastructure['student'] = None |
---|
[1393] | 776 | else: |
---|
| 777 | value = value.upper() |
---|
| 778 | res = s_cat(id = value) |
---|
| 779 | if not res: |
---|
[1573] | 780 | logger.info('Student id %s not found' % value) |
---|
[1793] | 781 | err = 'No student with Id %s' % (value) |
---|
[1393] | 782 | datastructure['student'] = None |
---|
| 783 | else: |
---|
| 784 | datastructure['student'] = res[0] |
---|
| 785 | if err: |
---|
| 786 | datastructure.setError(widget_id, err) |
---|
| 787 | else: |
---|
| 788 | datamodel = datastructure.getDataModel() |
---|
| 789 | datamodel[self.fields[0]] = value |
---|
| 790 | return not err |
---|
| 791 | |
---|
| 792 | InitializeClass(StudentIdWidget) |
---|
| 793 | |
---|
| 794 | widgetRegistry.register(StudentIdWidget) |
---|
| 795 | ###) |
---|
| 796 | |
---|
[1146] | 797 | class WAeUPMultilineResultsWidget(CPSStringWidget): ###( |
---|
| 798 | """ WAeUPMultilineResults Widget""" |
---|
| 799 | meta_type = "WAeUp Multiline Results Widget" |
---|
| 800 | _properties = CPSWidget._properties + ( |
---|
| 801 | {'id': 'nr_of_lines', 'type': 'int', 'mode': 'w', |
---|
| 802 | 'label': 'Nr of Lines'}, |
---|
| 803 | ) |
---|
| 804 | nr_of_lines = 5 |
---|
| 805 | def prepare(self, datastructure, **kw): ###( |
---|
| 806 | """Prepare datastructure from datamodel.""" |
---|
| 807 | datamodel = datastructure.getDataModel() |
---|
| 808 | #import pdb;pdb.set_trace() |
---|
| 809 | widget_id = self.getWidgetId() |
---|
| 810 | v = datamodel[self.fields[0]] |
---|
| 811 | if type(v) is ListType and v: |
---|
| 812 | nr_results = len(v) |
---|
| 813 | else: |
---|
| 814 | v = [] |
---|
| 815 | nr_results = 0 |
---|
| 816 | count = 1 |
---|
| 817 | for s,g in v: |
---|
| 818 | wid = "%s%02d"% (widget_id,count) |
---|
| 819 | datastructure[wid+'_s'] = s |
---|
| 820 | datastructure[wid+'_g'] = g |
---|
| 821 | count += 1 |
---|
| 822 | if nr_results < self.nr_of_lines: |
---|
| 823 | for line in range(nr_results,self.nr_of_lines): |
---|
| 824 | v.append(('','')) |
---|
| 825 | wid = "%s%02d"% (widget_id,line) |
---|
| 826 | datastructure[wid+'_s'] = '' |
---|
| 827 | datastructure[wid+'_g'] = '' |
---|
| 828 | datastructure[widget_id] = v |
---|
| 829 | datastructure[widget_id+'_s'] = '' |
---|
| 830 | datastructure[widget_id+'_g'] = '' |
---|
| 831 | ###) |
---|
| 832 | |
---|
| 833 | def validate(self, datastructure, **kw): ###( |
---|
| 834 | """Validate datastructure and update datamodel.""" |
---|
| 835 | #import pdb;pdb.set_trace() |
---|
| 836 | widget_id = self.getWidgetId() |
---|
| 837 | err = 0 |
---|
| 838 | lines = [] |
---|
| 839 | for line in range(1,30): |
---|
| 840 | wid = "%s%02d"% (widget_id,line) |
---|
| 841 | if not datastructure.has_key(wid+'_s'): |
---|
| 842 | break |
---|
| 843 | lines.append((datastructure[wid+'_s'].strip(), |
---|
| 844 | datastructure[wid+'_g'].strip())) |
---|
[1155] | 845 | |
---|
[1146] | 846 | s = datastructure[widget_id+'_s'].strip() |
---|
| 847 | g = datastructure[widget_id+'_g'].strip() |
---|
| 848 | if s and g: |
---|
| 849 | lines.append((s,g)) |
---|
| 850 | active = [] |
---|
| 851 | for s,g in lines: |
---|
| 852 | if g != "": |
---|
| 853 | active.append((s,g)) |
---|
| 854 | if err: |
---|
| 855 | datastructure.setError(widget_id, err) |
---|
| 856 | else: |
---|
| 857 | datamodel = datastructure.getDataModel() |
---|
| 858 | datamodel[self.fields[0]] = active |
---|
| 859 | return not err |
---|
| 860 | ###) |
---|
| 861 | |
---|
| 862 | def render(self, mode, datastructure, **kw): ###( |
---|
| 863 | """Render in mode from datastructure.""" |
---|
| 864 | render_method = 'widget_waeup_multiline_result_render' |
---|
| 865 | meth = getattr(self, render_method, None) |
---|
| 866 | if meth is None: |
---|
| 867 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 868 | % (render_method, self.getId())) |
---|
| 869 | #import pdb;pdb.set_trace() |
---|
| 870 | datamodel = datastructure.getDataModel() |
---|
| 871 | widget_id = self.getWidgetId() |
---|
| 872 | lines = datamodel[self.fields[0]] |
---|
| 873 | if len(lines) < self.nr_of_lines: |
---|
| 874 | for line in range(len(lines),self.nr_of_lines + 1): |
---|
| 875 | lines.append(('','')) |
---|
| 876 | datastructure[widget_id] = lines |
---|
| 877 | datastructure[widget_id+'_s'] = '' |
---|
| 878 | datastructure[widget_id+'_g'] = '' |
---|
| 879 | ## count = 1 |
---|
| 880 | ## for s,g in v: |
---|
| 881 | ## wid = "%s%02d"% (widget_id,count) |
---|
| 882 | ## count += 1 |
---|
| 883 | return meth(mode=mode, |
---|
| 884 | datastructure=datastructure, |
---|
| 885 | ) |
---|
| 886 | ###) |
---|
| 887 | |
---|
| 888 | |
---|
| 889 | InitializeClass(WAeUPMultilineResultsWidget) |
---|
| 890 | widgetRegistry.register(WAeUPMultilineResultsWidget) |
---|
| 891 | ###) |
---|
| 892 | |
---|
[794] | 893 | class WAeUPResultsWidget(CPSStringWidget): ###( |
---|
| 894 | """ WAeUPResults Widget""" |
---|
| 895 | meta_type = "WAeUp Results Widget" |
---|
| 896 | |
---|
| 897 | def prepare(self, datastructure, **kw): ###( |
---|
| 898 | """Prepare datastructure from datamodel.""" |
---|
| 899 | datamodel = datastructure.getDataModel() |
---|
| 900 | v = datamodel[self.fields[0]] |
---|
| 901 | #import pdb;pdb.set_trace() |
---|
| 902 | widget_id = self.getWidgetId() |
---|
[807] | 903 | datastructure[widget_id] = v |
---|
[794] | 904 | datastructure[widget_id+'_s'] = '' |
---|
| 905 | datastructure[widget_id+'_g'] = '' |
---|
| 906 | ###) |
---|
| 907 | |
---|
| 908 | def validate(self, datastructure, **kw): ###( |
---|
| 909 | """Validate datastructure and update datamodel.""" |
---|
[807] | 910 | #import pdb;pdb.set_trace() |
---|
[794] | 911 | widget_id = self.getWidgetId() |
---|
| 912 | v = datastructure[widget_id] |
---|
| 913 | err = 0 |
---|
| 914 | s = datastructure[widget_id+'_s'].strip() |
---|
| 915 | g = datastructure[widget_id+'_g'].strip() |
---|
[807] | 916 | while 1: |
---|
| 917 | if not s and g: |
---|
[1793] | 918 | err = "No subject grade for subject %s " % s |
---|
[807] | 919 | break |
---|
| 920 | i = 0 |
---|
| 921 | done = False |
---|
| 922 | for sv,gv in v: |
---|
| 923 | if sv == s: |
---|
| 924 | done = True |
---|
| 925 | if not g: |
---|
| 926 | v.pop(i) |
---|
| 927 | break |
---|
| 928 | v[i] = (s,g) |
---|
| 929 | break |
---|
| 930 | i += 1 |
---|
| 931 | if done: |
---|
| 932 | break |
---|
| 933 | if s and g: |
---|
| 934 | v.append((s,g)) |
---|
| 935 | break |
---|
[794] | 936 | if err: |
---|
| 937 | datastructure.setError(widget_id, err) |
---|
| 938 | else: |
---|
| 939 | datamodel = datastructure.getDataModel() |
---|
| 940 | datamodel[self.fields[0]] = v |
---|
| 941 | datastructure[widget_id+'_s'] = s |
---|
| 942 | datastructure[widget_id+'_g'] = g |
---|
| 943 | return not err |
---|
| 944 | ###) |
---|
| 945 | |
---|
| 946 | def render(self, mode, datastructure, **kw): ###( |
---|
| 947 | """Render in mode from datastructure.""" |
---|
| 948 | render_method = 'widget_waeup_result_render' |
---|
| 949 | meth = getattr(self, render_method, None) |
---|
| 950 | if meth is None: |
---|
| 951 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 952 | % (render_method, self.getId())) |
---|
| 953 | #import pdb;pdb.set_trace() |
---|
| 954 | datamodel = datastructure.getDataModel() |
---|
| 955 | widget_id = self.getWidgetId() |
---|
| 956 | datastructure[widget_id+'_s'] = '' |
---|
| 957 | datastructure[widget_id+'_g'] = '' |
---|
| 958 | return meth(mode=mode, |
---|
| 959 | datastructure=datastructure, |
---|
| 960 | ) |
---|
| 961 | ###) |
---|
| 962 | |
---|
| 963 | |
---|
| 964 | InitializeClass(WAeUPResultsWidget) |
---|
| 965 | widgetRegistry.register(WAeUPResultsWidget) |
---|
| 966 | ###) |
---|
| 967 | |
---|
[488] | 968 | class ScratchCardPin: ###( |
---|
| 969 | """the ScratchCardPin""" |
---|
| 970 | def __init__(self,prefix,batch_no,number): |
---|
[990] | 971 | if not batch_no and not number: |
---|
| 972 | s = prefix |
---|
[996] | 973 | if len(s) > 3: |
---|
[990] | 974 | prefix,batch_no,number = s[:3],s[3:-10],s[-10:] |
---|
| 975 | else: |
---|
[996] | 976 | prefix,batch_no,number = s,'','' |
---|
[488] | 977 | self.p = prefix |
---|
| 978 | self.b = batch_no |
---|
| 979 | self.n = number |
---|
| 980 | |
---|
| 981 | def __str__(self): |
---|
| 982 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
[1146] | 983 | |
---|
| 984 | def __repr__(self): |
---|
| 985 | return "%s%s%s" % (self.p,self.b,self.n) |
---|
[488] | 986 | ###) |
---|
| 987 | |
---|
[47] | 988 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
[22] | 989 | """ ScratchcardPin Widget""" |
---|
[199] | 990 | meta_type = "Scratchcard Pin Widget" |
---|
[488] | 991 | _properties = CPSWidget._properties + ( |
---|
| 992 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
| 993 | 'label': 'Prefix'}, |
---|
| 994 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 995 | 'label': 'Reference Field'}, |
---|
| 996 | ) |
---|
| 997 | prefix = '' |
---|
| 998 | reference = '' |
---|
[502] | 999 | def prepare(self, datastructure, **kw): ###( |
---|
[488] | 1000 | """Prepare datastructure from datamodel.""" |
---|
| 1001 | datamodel = datastructure.getDataModel() |
---|
| 1002 | v = datamodel[self.fields[0]] |
---|
| 1003 | widget_id = self.getWidgetId() |
---|
[742] | 1004 | #import pdb;pdb.set_trace() |
---|
[747] | 1005 | if v and type(v) is StringType: |
---|
[990] | 1006 | try: |
---|
| 1007 | p,b,n = v.split('-') |
---|
| 1008 | v = ScratchCardPin(p,b,n) |
---|
| 1009 | except ValueError: |
---|
| 1010 | v = ScratchCardPin(v,'','') |
---|
[488] | 1011 | if v: |
---|
[742] | 1012 | p = '%s' % v.p |
---|
[488] | 1013 | b = '%s' % v.b |
---|
| 1014 | n = '%s' % v.n |
---|
| 1015 | else: |
---|
[742] | 1016 | p = self.prefix |
---|
| 1017 | if p.startswith('@'): |
---|
| 1018 | p = getattr(self,self.prefix[1:])() |
---|
[488] | 1019 | b = n = '' |
---|
[742] | 1020 | v = ScratchCardPin(p,b,n) |
---|
[488] | 1021 | datastructure[widget_id] = v |
---|
[742] | 1022 | datastructure[widget_id+'_p'] = p |
---|
[488] | 1023 | datastructure[widget_id+'_b'] = b |
---|
| 1024 | datastructure[widget_id+'_n'] = n |
---|
[1376] | 1025 | ###) |
---|
[758] | 1026 | |
---|
[1169] | 1027 | def validate(self, datastructure, **kw): ###( |
---|
[22] | 1028 | """Validate datastructure and update datamodel.""" |
---|
[1571] | 1029 | s_logger = logging.getLogger('Widgets.ScratchcardPinWidget.validate') |
---|
[488] | 1030 | widget_id = self.getWidgetId() |
---|
| 1031 | v = datastructure[widget_id] |
---|
[996] | 1032 | #import pdb;pdb.set_trace() |
---|
[488] | 1033 | err = 0 |
---|
[742] | 1034 | prefix= self.prefix |
---|
| 1035 | if prefix.startswith('@'): |
---|
| 1036 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 1037 | b = datastructure[widget_id+'_b'].strip() |
---|
| 1038 | n = datastructure[widget_id+'_n'].strip() |
---|
[502] | 1039 | pins = self.portal_pins |
---|
[742] | 1040 | pin = "%(prefix)s%(b)s%(n)s" % vars() |
---|
[1376] | 1041 | pin_str = "%(prefix)s-%(b)-s%(n)s" % vars() |
---|
[816] | 1042 | do = 1 |
---|
[1025] | 1043 | s_id = str(self.portal_membership.getAuthenticatedMember()) |
---|
[1326] | 1044 | #import pdb;pdb.set_trace() |
---|
[1343] | 1045 | if self.isStaff(): |
---|
[1326] | 1046 | do = 0 |
---|
| 1047 | err ='You are not a Student. PIN neither checked nor used.' |
---|
[1571] | 1048 | s_logger.info('%s tried to use scratch card %s' % (s_id,pin_str)) |
---|
[1557] | 1049 | elif len(b) > 1 and b.find('-') > -1: |
---|
| 1050 | do = 0 |
---|
[1573] | 1051 | err = 'PIN must not contain "-"' |
---|
| 1052 | s_logger.info('%s entered invalid PIN containing "-"' % (s_id)) |
---|
[1557] | 1053 | elif n.find('-') > -1: |
---|
| 1054 | do = 0 |
---|
[1573] | 1055 | err = 'PIN must not contain "-"' |
---|
| 1056 | s_logger.info('%s entered invalid PIN containing "-"' % (s_id)) |
---|
[1376] | 1057 | elif len(n) != 10: |
---|
| 1058 | do = 0 |
---|
[1793] | 1059 | err = 'Invalid PIN length %d' % len(n) |
---|
[1573] | 1060 | s_logger.info('%s entered invalid PIN with length %d' % (s_id,len(n))) |
---|
[1326] | 1061 | elif self.reference == "": |
---|
[1030] | 1062 | ref = s_id |
---|
[635] | 1063 | else: |
---|
| 1064 | ref = datastructure[self.reference] |
---|
[843] | 1065 | if datastructure.errors: |
---|
[816] | 1066 | do = 0 |
---|
[1805] | 1067 | datastructure.setError(widget_id, 'PIN neither checked nor used.') |
---|
[1571] | 1068 | s_logger.info('%s/%s entered wrong data together with PIN %s' % (s_id,ref,pin_str)) |
---|
[816] | 1069 | while do: |
---|
| 1070 | ok = pins.searchAndSetRecord(pin,ref,prefix) |
---|
[1030] | 1071 | if ok < -2 or ok > 2: |
---|
| 1072 | err = 'Unknown error, please report!' |
---|
[1571] | 1073 | s_logger.info('%s/%s caused unknown error with PIN %s' % (s_id,ref,pin_str)) |
---|
[1030] | 1074 | break |
---|
| 1075 | elif ok == -2: |
---|
[1783] | 1076 | err = 'Service already is activated but with a different PIN.' |
---|
[1571] | 1077 | s_logger.info('%s/%s repeatedly activated service but with different PIN %s' % (s_id,ref,pin_str)) |
---|
[710] | 1078 | break |
---|
| 1079 | elif ok == -1: |
---|
[1793] | 1080 | err = 'Invalid PIN' |
---|
[1571] | 1081 | s_logger.info('%s/%s entered invalid PIN %s' % (s_id,ref,pin_str)) |
---|
[502] | 1082 | break |
---|
| 1083 | if ok == 0: |
---|
[1571] | 1084 | err = 'PIN already used' |
---|
| 1085 | s_logger.info('%s/%s entered used PIN %s' % (s_id,ref,pin_str)) |
---|
[502] | 1086 | break |
---|
| 1087 | if ok >= 1: |
---|
[710] | 1088 | #import pdb;pdb.set_trace() |
---|
[635] | 1089 | if self.isStudent(): |
---|
[992] | 1090 | if self.reference == "jamb_reg_no": |
---|
[997] | 1091 | err = "You are already logged in." |
---|
[1573] | 1092 | s_logger.info('%s/%s checked admission with PIN %s though logged in' % (s_id,ref,pin_str)) |
---|
[992] | 1093 | break |
---|
[1082] | 1094 | if ok == 1: |
---|
[1571] | 1095 | s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str)) |
---|
[1032] | 1096 | else: |
---|
[1571] | 1097 | s_logger.info('%s/%s repeatedly used PIN %s' % (s_id,ref,pin_str)) |
---|
[637] | 1098 | break |
---|
[635] | 1099 | else: |
---|
| 1100 | student = getStudentByRegNo(self,ref) |
---|
[1571] | 1101 | s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str)) |
---|
[502] | 1102 | if student is None: |
---|
[1793] | 1103 | err = "Student not found" |
---|
[1571] | 1104 | s_logger.info('%s not found in admission list' % (ref)) |
---|
[502] | 1105 | break |
---|
[648] | 1106 | s_id = student.getId() |
---|
[502] | 1107 | if ok == 2: |
---|
[990] | 1108 | if self.reference == "jamb_reg_no": |
---|
| 1109 | if hasattr(self.portal_directories.students,s_id): |
---|
| 1110 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
[1571] | 1111 | s_logger.info('%s/%s repeatedly checked admission with PIN %s' % (s_id,ref,pin_str)) |
---|
[990] | 1112 | break |
---|
| 1113 | else: |
---|
[1571] | 1114 | s_logger.info('%s/%s (non-member) repeatedly checked admission with PIN %s' % (s_id,ref,pin_str)) |
---|
[990] | 1115 | else: |
---|
[1793] | 1116 | err = "Unknown error" % s_id |
---|
[1571] | 1117 | s_logger.info('%s/%s repeatedly activated service with PIN %s' % (s_id,ref,pin_str)) |
---|
[986] | 1118 | break |
---|
| 1119 | try: |
---|
| 1120 | student.getContent().makeStudentMember(s_id,password=pin[4:]) |
---|
[1571] | 1121 | s_logger.info('%s/%s has been created using PIN %s' % (s_id,ref,pin_str)) |
---|
[986] | 1122 | except: |
---|
[989] | 1123 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
[1571] | 1124 | s_logger.info('%s/%s could not be made a member with PIN %s' % (s_id,ref,pin_str)) |
---|
[986] | 1125 | break |
---|
[502] | 1126 | break |
---|
[488] | 1127 | if err: |
---|
| 1128 | datastructure.setError(widget_id, err) |
---|
| 1129 | else: |
---|
| 1130 | datamodel = datastructure.getDataModel() |
---|
[742] | 1131 | datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n) |
---|
| 1132 | datastructure[widget_id] = ScratchCardPin(prefix,b,n) |
---|
| 1133 | datastructure[widget_id+'_p'] = prefix |
---|
[488] | 1134 | datastructure[widget_id+'_b'] = b |
---|
| 1135 | datastructure[widget_id+'_n'] = n |
---|
[502] | 1136 | datastructure['s_id'] = s_id |
---|
[488] | 1137 | return not err |
---|
[444] | 1138 | |
---|
[1169] | 1139 | ###) |
---|
| 1140 | |
---|
[502] | 1141 | def render(self, mode, datastructure, **kw): ###( |
---|
[488] | 1142 | """Render in mode from datastructure.""" |
---|
| 1143 | render_method = 'widget_scratch_card_pin_render' |
---|
| 1144 | meth = getattr(self, render_method, None) |
---|
| 1145 | if meth is None: |
---|
| 1146 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1147 | % (render_method, self.getId())) |
---|
| 1148 | |
---|
| 1149 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
| 1150 | # or create mode, because a default value has to be provided. |
---|
| 1151 | #import pdb;pdb.set_trace() |
---|
| 1152 | datamodel = datastructure.getDataModel() |
---|
| 1153 | v = datamodel[self.fields[0]] |
---|
[502] | 1154 | if v and type(v) is StringType: |
---|
[990] | 1155 | try: |
---|
| 1156 | p,b,n = v.split('-') |
---|
| 1157 | v = ScratchCardPin(p,b,n) |
---|
| 1158 | except ValueError: |
---|
[996] | 1159 | v = ScratchCardPin(self.prefix,'1',v) |
---|
| 1160 | pass |
---|
[488] | 1161 | if v: |
---|
[742] | 1162 | prefix= '%s' % v.p |
---|
[488] | 1163 | b = '%s' % v.b |
---|
| 1164 | n = '%s' % v.n |
---|
[22] | 1165 | else: |
---|
[742] | 1166 | prefix= self.prefix |
---|
| 1167 | if prefix.startswith('@'): |
---|
| 1168 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 1169 | b = n = '' |
---|
[742] | 1170 | v = ScratchCardPin(prefix,b,n) |
---|
| 1171 | widget_id = self.getWidgetId() |
---|
| 1172 | datastructure[widget_id] = v |
---|
| 1173 | datastructure[widget_id+'_p'] = prefix |
---|
| 1174 | datastructure[widget_id+'_b'] = b |
---|
| 1175 | datastructure[widget_id+'_n'] = n |
---|
[758] | 1176 | return meth(mode=mode, |
---|
| 1177 | datastructure=datastructure, |
---|
[488] | 1178 | ) |
---|
[523] | 1179 | ###) |
---|
[488] | 1180 | |
---|
| 1181 | |
---|
[22] | 1182 | InitializeClass(ScratchcardPinWidget) |
---|
[199] | 1183 | widgetRegistry.register(ScratchcardPinWidget) |
---|
[22] | 1184 | |
---|
[47] | 1185 | |
---|
| 1186 | ###) |
---|
| 1187 | |
---|
[1169] | 1188 | class WAeUPImageWidget(CPSImageWidget): ###( |
---|
[537] | 1189 | """Photo widget.""" |
---|
| 1190 | meta_type = 'WAeUP Image Widget' |
---|
| 1191 | |
---|
| 1192 | def render(self, mode, datastructure, **kw): |
---|
| 1193 | render_method = 'widget_waeup_image_render' |
---|
| 1194 | meth = getattr(self, render_method, None) |
---|
| 1195 | if meth is None: |
---|
| 1196 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1197 | % (render_method, self.getId())) |
---|
| 1198 | img_info = self.getImageInfo(datastructure) |
---|
| 1199 | return meth(mode=mode, datastructure=datastructure, **img_info) |
---|
| 1200 | |
---|
| 1201 | InitializeClass(WAeUPImageWidget) |
---|
| 1202 | |
---|
| 1203 | widgetRegistry.register(WAeUPImageWidget) |
---|
[1804] | 1204 | ###) |
---|
[537] | 1205 | |
---|
| 1206 | |
---|
[22] | 1207 | ########### |
---|
| 1208 | |
---|