[47] | 1 | #-*- mode: python; mode: fold -*- |
---|
[990] | 2 | # $Id: Widgets.py 2187 2007-09-07 15:15:36Z joachim $ |
---|
[295] | 3 | from cgi import escape |
---|
[502] | 4 | from types import * |
---|
[2110] | 5 | import Globals |
---|
[22] | 6 | from Globals import InitializeClass |
---|
[2098] | 7 | from ZPublisher.HTTPRequest import FileUpload |
---|
[199] | 8 | ##from Products.CPSSchemas.Widget import CPSWidgetType |
---|
[295] | 9 | from Products.CMFCore.utils import getToolByName |
---|
[1772] | 10 | from Products.CPSSchemas.BasicWidgets import CPSBooleanWidget, CPSWidget, CPSStringWidget, CPSEmailWidget,CPSImageWidget |
---|
[2098] | 11 | from Products.CPSSchemas.BasicWidgets import CPSFileWidget |
---|
[295] | 12 | from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget |
---|
[22] | 13 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
[199] | 14 | from Products.CPSSchemas.Widget import widgetRegistry |
---|
[2098] | 15 | from Products.CPSUtil.file import PersistableFileUpload |
---|
| 16 | from Products.CPSUtil.id import generateFileName |
---|
[199] | 17 | ##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry |
---|
[22] | 18 | from DateTime.DateTime import DateTime |
---|
| 19 | from AccessControl import getSecurityManager |
---|
[502] | 20 | from Products.WAeUP_SRP.Students import getStudentByRegNo |
---|
[1747] | 21 | from Products.WAeUP_SRP.Academics import makeCertificateCode |
---|
[2099] | 22 | #from Products.ExtFile.ExtFile import ExtFile |
---|
[2110] | 23 | import logging,os,re |
---|
[1915] | 24 | import operator |
---|
[2110] | 25 | p_home = Globals.package_home(globals()) |
---|
| 26 | i_home = Globals.INSTANCE_HOME |
---|
[22] | 27 | |
---|
[952] | 28 | #from zLOG import LOG, DEBUG |
---|
[22] | 29 | |
---|
[295] | 30 | class CPSSelectWidgetForRecord(CPSSelectWidget): ###( |
---|
| 31 | """Select widget. with record names""" |
---|
| 32 | meta_type = 'Select Widget for Records' |
---|
| 33 | |
---|
| 34 | field_types = ('CPS String Field',) |
---|
| 35 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 36 | |
---|
| 37 | _properties = CPSSelectWidget._properties + ( |
---|
| 38 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 39 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 40 | ) |
---|
| 41 | |
---|
| 42 | def render(self, mode, datastructure, **kw): |
---|
| 43 | """Render in mode from datastructure.""" |
---|
| 44 | value = datastructure[self.getWidgetId()] |
---|
| 45 | vocabulary = self._getVocabulary(datastructure) |
---|
| 46 | portal = getToolByName(self, 'portal_url').getPortalObject() |
---|
| 47 | cpsmcat = portal.translation_service |
---|
| 48 | if mode == 'view': |
---|
| 49 | if self.translated: |
---|
| 50 | return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore')) |
---|
| 51 | else: |
---|
| 52 | return escape(vocabulary.get(value, value)) |
---|
| 53 | elif mode == 'edit': |
---|
| 54 | html_widget_id = self.getHtmlWidgetId() |
---|
[444] | 55 | res = renderHtmlTag('select', |
---|
| 56 | name='%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 57 | id=html_widget_id) |
---|
| 58 | in_selection = 0 |
---|
| 59 | for k, v in vocabulary.items(): |
---|
| 60 | if self.translated: |
---|
| 61 | kw = {'value': k, |
---|
| 62 | 'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore') |
---|
| 63 | } |
---|
| 64 | else: |
---|
| 65 | kw = {'value': k, 'contents': v} |
---|
| 66 | if value == k: |
---|
| 67 | kw['selected'] = 'selected' |
---|
| 68 | in_selection = 1 |
---|
| 69 | res += renderHtmlTag('option', **kw) |
---|
| 70 | if value and not in_selection: |
---|
| 71 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 72 | 'selected': 'selected'} |
---|
| 73 | res += renderHtmlTag('option', **kw) |
---|
| 74 | res += '</select>' |
---|
| 75 | return res |
---|
| 76 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 77 | |
---|
| 78 | InitializeClass(CPSSelectWidgetForRecord) |
---|
| 79 | |
---|
| 80 | widgetRegistry.register(CPSSelectWidgetForRecord) |
---|
| 81 | |
---|
| 82 | ###) |
---|
| 83 | |
---|
[373] | 84 | class CPSStringWidgetForRecord(CPSStringWidget): ###( |
---|
[295] | 85 | """String widget.""" |
---|
| 86 | meta_type = 'String Widget For Record' |
---|
| 87 | |
---|
| 88 | field_types = ('CPS String Field',) |
---|
| 89 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 90 | _properties = CPSStringWidget._properties + ( |
---|
| 91 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 92 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 93 | ) |
---|
| 94 | |
---|
| 95 | def render(self, mode, datastructure, **kw): |
---|
| 96 | """Render in mode from datastructure.""" |
---|
| 97 | value = datastructure[self.getWidgetId()] |
---|
| 98 | if mode == 'view': |
---|
| 99 | return escape(value) |
---|
| 100 | elif mode == 'edit': |
---|
| 101 | # XXX TODO should use an other name than kw ! |
---|
| 102 | # XXX change this everywhere |
---|
| 103 | html_widget_id = self.getHtmlWidgetId() |
---|
| 104 | kw = {'type': 'text', |
---|
| 105 | 'id' : html_widget_id, |
---|
[444] | 106 | 'name': '%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 107 | 'value': escape(value), |
---|
| 108 | 'size': self.display_width, |
---|
| 109 | } |
---|
| 110 | if self.size_max: |
---|
| 111 | kw['maxlength'] = self.size_max |
---|
| 112 | return renderHtmlTag('input', **kw) |
---|
| 113 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 114 | |
---|
| 115 | InitializeClass(CPSStringWidgetForRecord) |
---|
| 116 | |
---|
| 117 | widgetRegistry.register(CPSStringWidgetForRecord) |
---|
| 118 | |
---|
[373] | 119 | ###) |
---|
| 120 | |
---|
| 121 | class CertificateCourseIdWidget(CPSStringWidget): ###( |
---|
| 122 | """ CertificateCourseId Widget""" |
---|
| 123 | meta_type = "CertificateCourseId Widget" |
---|
[444] | 124 | |
---|
[373] | 125 | def validate(self, datastructure, **kw): |
---|
| 126 | """Validate datastructure and update datamodel.""" |
---|
[444] | 127 | |
---|
[373] | 128 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 129 | if not valid: |
---|
| 130 | return 0 |
---|
| 131 | else: |
---|
| 132 | widget_id = self.getWidgetId() |
---|
| 133 | value = datastructure[widget_id].upper() |
---|
| 134 | err = 0 |
---|
| 135 | c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] |
---|
[381] | 136 | if hasattr(self.aq_parent,value): |
---|
[1891] | 137 | err = 'Course already exists' |
---|
[381] | 138 | elif value not in c_ids: |
---|
[1891] | 139 | err = 'Course does not exist' |
---|
[373] | 140 | if err: |
---|
| 141 | datastructure.setError(widget_id, err) |
---|
| 142 | else: |
---|
| 143 | datamodel = datastructure.getDataModel() |
---|
| 144 | datamodel[self.fields[0]] = value |
---|
[444] | 145 | |
---|
[373] | 146 | return not err |
---|
| 147 | |
---|
| 148 | InitializeClass(CertificateCourseIdWidget) |
---|
| 149 | |
---|
| 150 | widgetRegistry.register(CertificateCourseIdWidget) |
---|
[551] | 151 | ###) |
---|
[373] | 152 | |
---|
[551] | 153 | class CourseIdWidget(CPSStringWidget): ###( |
---|
| 154 | """ CourseId Widget""" |
---|
| 155 | meta_type = "CourseId Widget" |
---|
| 156 | |
---|
| 157 | def validate(self, datastructure, **kw): |
---|
| 158 | """Validate datastructure and update datamodel.""" |
---|
| 159 | |
---|
| 160 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 161 | if not valid: |
---|
| 162 | return 0 |
---|
| 163 | else: |
---|
| 164 | widget_id = self.getWidgetId() |
---|
| 165 | value = datastructure[widget_id].upper() |
---|
| 166 | err = 0 |
---|
| 167 | res = self.portal_catalog(meta_type= "Course",id = value) |
---|
| 168 | if len(res) > 0: |
---|
[1891] | 169 | err = 'Course already exists' |
---|
[551] | 170 | if err: |
---|
| 171 | datastructure.setError(widget_id, err) |
---|
| 172 | else: |
---|
| 173 | datamodel = datastructure.getDataModel() |
---|
| 174 | datamodel[self.fields[0]] = value |
---|
| 175 | |
---|
| 176 | return not err |
---|
| 177 | |
---|
| 178 | InitializeClass(CourseIdWidget) |
---|
| 179 | |
---|
| 180 | widgetRegistry.register(CourseIdWidget) |
---|
| 181 | |
---|
| 182 | |
---|
[373] | 183 | ###) |
---|
| 184 | |
---|
[1820] | 185 | class WAeUPStudyModeWidget(CPSSelectWidget): ###( |
---|
[1986] | 186 | """WAeUP StudyMode Widget.""" |
---|
[1820] | 187 | meta_type = 'WAeUP StudyMode Widget' |
---|
| 188 | vocabulary = 'entry_modes' |
---|
| 189 | |
---|
| 190 | def _getStudyModes(self): |
---|
| 191 | voc = getattr(self.portal_vocabularies,self.vocabulary) |
---|
| 192 | d = {} |
---|
| 193 | for k,v in voc.items(): |
---|
| 194 | d[k] = v |
---|
| 195 | return d |
---|
| 196 | |
---|
| 197 | def validate(self, datastructure, **kw): |
---|
| 198 | """Validate datastructure and update datamodel.""" |
---|
| 199 | widget_id = self.getWidgetId() |
---|
| 200 | value = datastructure[widget_id] |
---|
| 201 | try: |
---|
| 202 | v = str(value) |
---|
| 203 | except ValueError: |
---|
| 204 | datastructure.setError(widget_id, "'%s' not a valid session key" % value) |
---|
| 205 | return 0 |
---|
| 206 | studymodes = self._getStudyModes() |
---|
| 207 | if not value: |
---|
[1836] | 208 | v = value = 'ume_ft' |
---|
[1820] | 209 | #import pdb;pdb.set_trace() |
---|
| 210 | if not studymodes.has_key(value): |
---|
| 211 | datastructure.setError(widget_id, "'%s' not a valid session key" % v) |
---|
| 212 | return 0 |
---|
| 213 | if self.is_required and not len(v): |
---|
| 214 | datastructure.setError(widget_id, "session key required") |
---|
| 215 | return 0 |
---|
| 216 | |
---|
| 217 | datamodel = datastructure.getDataModel() |
---|
| 218 | datamodel[self.fields[0]] = v |
---|
| 219 | return 1 |
---|
| 220 | |
---|
| 221 | def render(self, mode, datastructure, **kw): |
---|
| 222 | """Render in mode from datastructure.""" |
---|
| 223 | value = datastructure[self.getWidgetId()] |
---|
| 224 | studymodes = self._getStudyModes() |
---|
| 225 | if mode == 'view': |
---|
| 226 | return escape(studymodes.get(value, value)) |
---|
| 227 | elif mode == 'edit': |
---|
| 228 | html_widget_id = self.getHtmlWidgetId() |
---|
| 229 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 230 | in_selection = 0 |
---|
| 231 | vocabulary_items = studymodes.items() |
---|
| 232 | if self.sorted: |
---|
| 233 | vocabulary_items.sort(key=operator.itemgetter(1)) |
---|
| 234 | for k, v in vocabulary_items: |
---|
| 235 | kw = {'value': k, 'contents': v} |
---|
| 236 | if value == k: |
---|
| 237 | kw['selected'] = 'selected' |
---|
| 238 | in_selection = 1 |
---|
| 239 | res += renderHtmlTag('option', **kw) |
---|
| 240 | if value and not in_selection: |
---|
| 241 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 242 | 'selected': 'selected'} |
---|
| 243 | res += renderHtmlTag('option', **kw) |
---|
| 244 | res += '</select>' |
---|
| 245 | return res |
---|
| 246 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 247 | |
---|
| 248 | InitializeClass(WAeUPStudyModeWidget) |
---|
| 249 | |
---|
| 250 | widgetRegistry.register(WAeUPStudyModeWidget) |
---|
| 251 | |
---|
| 252 | ###) |
---|
| 253 | |
---|
[1804] | 254 | class WAeUPSessionWidget(CPSSelectWidget): ###( |
---|
[1986] | 255 | """WAeUP Session Widget.""" |
---|
[1804] | 256 | meta_type = 'WAeUP Session Widget' |
---|
[2099] | 257 | |
---|
[1804] | 258 | def _getSessions(self): |
---|
| 259 | current_year = DateTime().year() |
---|
[1986] | 260 | d = {'-1': 'N/A'} |
---|
[1804] | 261 | for y in range(current_year - 9,current_year + 1): |
---|
| 262 | d['%s' % str(y)[-2:]] = '%4d/%4d' % (y,y+1) |
---|
| 263 | return d |
---|
| 264 | |
---|
| 265 | def validate(self, datastructure, **kw): |
---|
| 266 | """Validate datastructure and update datamodel.""" |
---|
| 267 | widget_id = self.getWidgetId() |
---|
| 268 | value = datastructure[widget_id] |
---|
| 269 | try: |
---|
| 270 | v = str(value) |
---|
| 271 | except ValueError: |
---|
| 272 | datastructure.setError(widget_id, "'%s' not a valid session key" % value) |
---|
| 273 | return 0 |
---|
[1805] | 274 | |
---|
[1804] | 275 | if len(v) == 1: |
---|
| 276 | v = value = '0%c' % v |
---|
| 277 | elif not value: |
---|
| 278 | v = value = self.getSessionId()[-2:] |
---|
| 279 | #import pdb;pdb.set_trace() |
---|
| 280 | sessions = self._getSessions() |
---|
| 281 | if not sessions.has_key(value): |
---|
| 282 | datastructure.setError(widget_id, "'%s' not a valid session key" % v) |
---|
| 283 | return 0 |
---|
| 284 | if self.is_required and not len(v): |
---|
| 285 | datastructure.setError(widget_id, "session key required") |
---|
| 286 | return 0 |
---|
| 287 | |
---|
| 288 | datamodel = datastructure.getDataModel() |
---|
| 289 | datamodel[self.fields[0]] = v |
---|
| 290 | return 1 |
---|
| 291 | |
---|
| 292 | def render(self, mode, datastructure, **kw): |
---|
| 293 | """Render in mode from datastructure.""" |
---|
| 294 | value = datastructure[self.getWidgetId()] |
---|
| 295 | sessions = self._getSessions() |
---|
| 296 | if mode == 'view': |
---|
| 297 | return escape(sessions.get(value, value)) |
---|
| 298 | elif mode == 'edit': |
---|
| 299 | html_widget_id = self.getHtmlWidgetId() |
---|
| 300 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 301 | in_selection = 0 |
---|
| 302 | vocabulary_items = sessions.items() |
---|
| 303 | if self.sorted: |
---|
[1986] | 304 | vocabulary_items.sort(key=operator.itemgetter(0)) |
---|
[1804] | 305 | for k, v in vocabulary_items: |
---|
| 306 | kw = {'value': k, 'contents': v} |
---|
| 307 | if value == k: |
---|
| 308 | kw['selected'] = 'selected' |
---|
| 309 | in_selection = 1 |
---|
| 310 | res += renderHtmlTag('option', **kw) |
---|
| 311 | if value and not in_selection: |
---|
| 312 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 313 | 'selected': 'selected'} |
---|
| 314 | res += renderHtmlTag('option', **kw) |
---|
| 315 | res += '</select>' |
---|
| 316 | return res |
---|
| 317 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 318 | |
---|
| 319 | InitializeClass(WAeUPSessionWidget) |
---|
| 320 | |
---|
| 321 | widgetRegistry.register(WAeUPSessionWidget) |
---|
| 322 | |
---|
| 323 | ###) |
---|
| 324 | |
---|
| 325 | class WAeUPLevelWidget(CPSSelectWidget): ###( |
---|
| 326 | """WAeUP Level Widget.""" |
---|
| 327 | meta_type = 'WAeUP Level Widget' |
---|
| 328 | |
---|
| 329 | def _getLevels(self): |
---|
[1986] | 330 | d = {'000':'N/A'} |
---|
[1804] | 331 | for y in range(100,800,100): |
---|
| 332 | d['%s' % str(y)] = 'Year %1d (%3d Level)' % (y/100,y) |
---|
| 333 | return d |
---|
| 334 | |
---|
| 335 | def validate(self, datastructure, **kw): |
---|
| 336 | """Validate datastructure and update datamodel.""" |
---|
| 337 | widget_id = self.getWidgetId() |
---|
| 338 | value = datastructure[widget_id] |
---|
| 339 | try: |
---|
| 340 | v = str(value) |
---|
| 341 | except ValueError: |
---|
| 342 | datastructure.setError(widget_id, "'%s' not a valid level key" % value) |
---|
| 343 | return 0 |
---|
[1805] | 344 | |
---|
| 345 | if not value: |
---|
[1804] | 346 | v = value = '100' |
---|
| 347 | #import pdb;pdb.set_trace() |
---|
| 348 | levels = self._getLevels() |
---|
| 349 | if not levels.has_key(value): |
---|
| 350 | datastructure.setError(widget_id, "'%s' not a valid level" % v) |
---|
| 351 | return 0 |
---|
| 352 | if self.is_required and not len(v): |
---|
| 353 | datastructure.setError(widget_id, "level key required") |
---|
| 354 | return 0 |
---|
| 355 | |
---|
| 356 | datamodel = datastructure.getDataModel() |
---|
| 357 | datamodel[self.fields[0]] = v |
---|
| 358 | return 1 |
---|
| 359 | |
---|
| 360 | def render(self, mode, datastructure, **kw): |
---|
| 361 | """Render in mode from datastructure.""" |
---|
| 362 | value = datastructure[self.getWidgetId()] |
---|
| 363 | levels = self._getLevels() |
---|
| 364 | if mode == 'view': |
---|
| 365 | return escape(levels.get(value, value)) |
---|
| 366 | elif mode == 'edit': |
---|
| 367 | html_widget_id = self.getHtmlWidgetId() |
---|
| 368 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 369 | in_selection = 0 |
---|
| 370 | vocabulary_items = levels.items() |
---|
| 371 | if self.sorted: |
---|
[1986] | 372 | vocabulary_items.sort(key=operator.itemgetter(0)) |
---|
[1804] | 373 | for k, v in vocabulary_items: |
---|
| 374 | kw = {'value': k, 'contents': v} |
---|
| 375 | if value == k: |
---|
| 376 | kw['selected'] = 'selected' |
---|
| 377 | in_selection = 1 |
---|
| 378 | res += renderHtmlTag('option', **kw) |
---|
| 379 | if value and not in_selection: |
---|
| 380 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 381 | 'selected': 'selected'} |
---|
| 382 | res += renderHtmlTag('option', **kw) |
---|
| 383 | res += '</select>' |
---|
| 384 | return res |
---|
| 385 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 386 | |
---|
| 387 | InitializeClass(WAeUPLevelWidget) |
---|
| 388 | |
---|
| 389 | widgetRegistry.register(WAeUPLevelWidget) |
---|
| 390 | |
---|
| 391 | ###) |
---|
| 392 | |
---|
| 393 | class WAeUPVerdictWidget(CPSSelectWidget): ###( |
---|
[1986] | 394 | """WAeUP Verdict Widget.""" |
---|
[1804] | 395 | meta_type = 'WAeUP Verdict Widget' |
---|
| 396 | |
---|
| 397 | # XXX make a menu for the vocabulary. |
---|
| 398 | vocabulary = 'verdicts' |
---|
| 399 | |
---|
| 400 | # Associating the widget label with an input area to improve the widget |
---|
| 401 | # accessibility. |
---|
| 402 | has_input_area = True |
---|
| 403 | |
---|
| 404 | def _getVerdicts(self,datastructure): |
---|
| 405 | voc = getattr(self.portal_vocabularies,self.vocabulary) |
---|
| 406 | d = {} |
---|
| 407 | for k,v in voc.items(): |
---|
| 408 | d[k] = v |
---|
| 409 | return d |
---|
| 410 | |
---|
| 411 | def validate(self, datastructure, **kw): |
---|
| 412 | """Validate datastructure and update datamodel.""" |
---|
| 413 | widget_id = self.getWidgetId() |
---|
| 414 | value = datastructure[widget_id] |
---|
| 415 | try: |
---|
| 416 | v = str(value) |
---|
| 417 | except ValueError: |
---|
| 418 | datastructure.setError(widget_id, "'%s' not a valid verdict key" % value) |
---|
| 419 | return 0 |
---|
| 420 | #import pdb;pdb.set_trace() |
---|
| 421 | verdicts = self._getVerdicts(datastructure) |
---|
| 422 | if not value: |
---|
| 423 | v = value = verdicts['N/A'] |
---|
| 424 | if not verdicts.has_key(value): |
---|
| 425 | datastructure.setError(widget_id, "'%s' not a valid verdict key" % v) |
---|
| 426 | return 0 |
---|
| 427 | if self.is_required and not len(v): |
---|
| 428 | datastructure.setError(widget_id, "verdict required") |
---|
| 429 | return 0 |
---|
| 430 | |
---|
| 431 | datamodel = datastructure.getDataModel() |
---|
| 432 | datamodel[self.fields[0]] = v |
---|
| 433 | return 1 |
---|
| 434 | |
---|
| 435 | def render(self, mode, datastructure, **kw): |
---|
| 436 | """Render in mode from datastructure.""" |
---|
| 437 | value = datastructure[self.getWidgetId()] |
---|
| 438 | verdicts = self._getVerdicts(datastructure) |
---|
| 439 | if mode == 'view': |
---|
| 440 | return escape(verdicts.get(value, value)) |
---|
| 441 | elif mode == 'edit': |
---|
| 442 | html_widget_id = self.getHtmlWidgetId() |
---|
| 443 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 444 | in_selection = 0 |
---|
| 445 | vocabulary_items = verdicts.items() |
---|
| 446 | if self.sorted: |
---|
| 447 | vocabulary_items.sort(key=operator.itemgetter(1)) |
---|
| 448 | for k, v in vocabulary_items: |
---|
| 449 | kw = {'value': k, 'contents': v} |
---|
| 450 | if value == k: |
---|
| 451 | kw['selected'] = 'selected' |
---|
| 452 | in_selection = 1 |
---|
| 453 | res += renderHtmlTag('option', **kw) |
---|
| 454 | if value and not in_selection: |
---|
| 455 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 456 | 'selected': 'selected'} |
---|
| 457 | res += renderHtmlTag('option', **kw) |
---|
| 458 | res += '</select>' |
---|
| 459 | return res |
---|
| 460 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 461 | |
---|
| 462 | InitializeClass(WAeUPVerdictWidget) |
---|
| 463 | |
---|
| 464 | widgetRegistry.register(WAeUPVerdictWidget) |
---|
| 465 | |
---|
| 466 | ###) |
---|
| 467 | |
---|
[2094] | 468 | class WAeUPLGAWidget(CPSSelectWidget): ###( |
---|
| 469 | """WAeUP LGA Widget.""" |
---|
| 470 | meta_type = 'WAeUP LGA Widget' |
---|
| 471 | _properties = CPSSelectWidget._properties + ( |
---|
| 472 | {'id': 'state_field', 'type': 'string', 'mode': 'w', |
---|
| 473 | 'label': 'Name of the state field'}, |
---|
| 474 | {'id': 'lga_field', 'type': 'string', 'mode': 'w', |
---|
| 475 | 'label': 'Name of the LGA Field (without state)'}, |
---|
| 476 | ) |
---|
| 477 | state_field = "state" |
---|
| 478 | lga_field = "lga" |
---|
| 479 | |
---|
| 480 | # XXX make a menu for the vocabulary. |
---|
| 481 | vocabulary = 'local_gov_areas' |
---|
| 482 | |
---|
| 483 | # Associating the widget label with an input area to improve the widget |
---|
| 484 | # accessibility. |
---|
| 485 | has_input_area = True |
---|
| 486 | |
---|
| 487 | def _getLGAs(self): |
---|
| 488 | voc = getattr(self.portal_vocabularies,self.vocabulary) |
---|
| 489 | states = [] |
---|
| 490 | lgas = [] |
---|
| 491 | d = {} |
---|
| 492 | for k,v in voc.items(): |
---|
[2110] | 493 | parts = v.split(' / ') |
---|
[2187] | 494 | if len(parts) == 1: |
---|
| 495 | state = parts[0].lower() |
---|
| 496 | lga = "" |
---|
| 497 | elif len(parts) == 2: |
---|
| 498 | state = parts[0].lower() |
---|
| 499 | lga = "_".join(re.split('\W+',parts[1].lower())) |
---|
| 500 | else: |
---|
[2110] | 501 | continue |
---|
[2094] | 502 | if state not in states: |
---|
| 503 | states.append(state) |
---|
[2187] | 504 | if lga not in lgas: |
---|
| 505 | lgas.append(lga) |
---|
[2094] | 506 | d[k] = v |
---|
[2110] | 507 | |
---|
[2094] | 508 | return (d,states,lgas) |
---|
| 509 | |
---|
| 510 | def validate(self, datastructure, **kw): |
---|
| 511 | """Validate datastructure and update datamodel.""" |
---|
| 512 | widget_id = self.getWidgetId() |
---|
| 513 | value = datastructure[widget_id] |
---|
| 514 | #import pdb;pdb.set_trace() |
---|
| 515 | try: |
---|
| 516 | v = str(value) |
---|
| 517 | except ValueError: |
---|
| 518 | datastructure.setError(widget_id, "'%s' not a valid lga key" % value) |
---|
| 519 | return 0 |
---|
| 520 | v = v.lower() |
---|
| 521 | combined,states,lgas = self._getLGAs() |
---|
| 522 | one_field = False |
---|
[2185] | 523 | if len(v) == 0: |
---|
| 524 | pass |
---|
| 525 | elif not self.state_field and not self.lga_field: |
---|
[2094] | 526 | one_field = True |
---|
| 527 | if v not in combined.keys(): |
---|
| 528 | datastructure.setError(widget_id, "'%s' not a valid lga key" % v) |
---|
| 529 | return 0 |
---|
| 530 | else: |
---|
| 531 | if widget_id == self.state_field: |
---|
| 532 | if v not in states: |
---|
| 533 | datastructure.setError(widget_id, "'%s' not a valid state" % v) |
---|
| 534 | return 0 |
---|
| 535 | elif widget_id == self.lga_field: |
---|
[2110] | 536 | if "_".join(re.split('\W+',v)) not in lgas: |
---|
[2094] | 537 | datastructure.setError(widget_id, "'%s' not a valid lga" % v) |
---|
| 538 | return 0 |
---|
| 539 | if datastructure[self.state_field]: |
---|
| 540 | v = datastructure[self.state_field] + '_' + v |
---|
[2099] | 541 | |
---|
[2094] | 542 | if self.is_required and not len(v): |
---|
| 543 | datastructure.setError(widget_id, "lga required") |
---|
| 544 | return 0 |
---|
| 545 | |
---|
| 546 | datamodel = datastructure.getDataModel() |
---|
| 547 | if one_field: |
---|
| 548 | datamodel[self.fields[0]] = v |
---|
| 549 | else: |
---|
| 550 | state = datastructure[self.state_field].lower() |
---|
| 551 | lga = "_".join(datastructure[self.lga_field].lower().split()) |
---|
[2099] | 552 | datamodel[self.fields[0]] = state + "_" + lga |
---|
[2094] | 553 | return 1 |
---|
| 554 | |
---|
| 555 | def render(self, mode, datastructure, **kw): |
---|
| 556 | """Render in mode from datastructure.""" |
---|
| 557 | w_id = self |
---|
| 558 | value = datastructure[self.getWidgetId()] |
---|
| 559 | lgas,x,y = self._getLGAs(datastructure) |
---|
| 560 | if mode == 'view': |
---|
| 561 | return escape(lgas.get(value, value)) |
---|
| 562 | elif mode == 'edit': |
---|
| 563 | html_widget_id = self.getHtmlWidgetId() |
---|
| 564 | res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id) |
---|
| 565 | in_selection = 0 |
---|
| 566 | vocabulary_items = lgas.items() |
---|
| 567 | # if self.sorted: |
---|
| 568 | # vocabulary_items.sort(key=operator.itemgetter(1)) |
---|
| 569 | for k, v in vocabulary_items: |
---|
| 570 | kw = {'value': k, 'contents': v} |
---|
| 571 | if value == k: |
---|
| 572 | kw['selected'] = 'selected' |
---|
| 573 | in_selection = 1 |
---|
| 574 | res += renderHtmlTag('option', **kw) |
---|
| 575 | if value and not in_selection: |
---|
| 576 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 577 | 'selected': 'selected'} |
---|
| 578 | res += renderHtmlTag('option', **kw) |
---|
| 579 | res += '</select>' |
---|
| 580 | return res |
---|
| 581 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 582 | |
---|
| 583 | InitializeClass(WAeUPLGAWidget) |
---|
| 584 | |
---|
| 585 | widgetRegistry.register(WAeUPLGAWidget) |
---|
| 586 | |
---|
| 587 | ###) |
---|
| 588 | |
---|
[714] | 589 | class WAeUPReservedRoomsWidget(CPSStringWidget): ###( |
---|
| 590 | """ WAeUPReservedRooms Widget""" |
---|
| 591 | meta_type = "WAeUPReservedRooms Widget" |
---|
| 592 | |
---|
| 593 | def validate(self, datastructure, **kw): |
---|
| 594 | """Validate datastructure and update datamodel.""" |
---|
| 595 | import re |
---|
| 596 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 597 | if not valid: |
---|
| 598 | return 0 |
---|
| 599 | else: |
---|
| 600 | widget_id = self.getWidgetId() |
---|
| 601 | value = datastructure[widget_id] |
---|
| 602 | err = 0 |
---|
| 603 | try: |
---|
[926] | 604 | reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',value) |
---|
[714] | 605 | if r] |
---|
[1038] | 606 | except (ValueError,IndexError),msg: |
---|
[714] | 607 | err = str(msg) |
---|
| 608 | if err: |
---|
| 609 | datastructure.setError(widget_id, err) |
---|
| 610 | else: |
---|
| 611 | datamodel = datastructure.getDataModel() |
---|
| 612 | datamodel[self.fields[0]] = value |
---|
| 613 | return not err |
---|
| 614 | |
---|
| 615 | InitializeClass(WAeUPReservedRoomsWidget) |
---|
| 616 | |
---|
| 617 | widgetRegistry.register(WAeUPReservedRoomsWidget) |
---|
| 618 | ###) |
---|
| 619 | |
---|
[388] | 620 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
| 621 | """ WAeUPId Widget""" |
---|
| 622 | meta_type = "WAeUPId Widget" |
---|
[444] | 623 | |
---|
[388] | 624 | def validate(self, datastructure, **kw): |
---|
| 625 | """Validate datastructure and update datamodel.""" |
---|
[444] | 626 | |
---|
[388] | 627 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[422] | 628 | id_pat_str = r"\S" |
---|
[2110] | 629 | inv_id_pat = re.compile(r"^%s$" % id_pat_str) |
---|
[388] | 630 | if not valid: |
---|
| 631 | return 0 |
---|
| 632 | else: |
---|
[586] | 633 | portal_type_query = {'query':['Faculty', |
---|
| 634 | 'Department', |
---|
| 635 | 'Course', |
---|
| 636 | 'Certificate', |
---|
| 637 | 'CertificateCourse',]} |
---|
[388] | 638 | widget_id = self.getWidgetId() |
---|
[2013] | 639 | value = datastructure[widget_id] #.upper() is not necessary here because it's also done in waeup_document_create_do |
---|
[388] | 640 | err = 0 |
---|
[1907] | 641 | mapping = {} |
---|
[440] | 642 | if len(value.split()) > 1: |
---|
[783] | 643 | err = 'Invalid Id, Id contains space(s).' |
---|
[586] | 644 | elif self.portal_catalog(portal_type=portal_type_query,id=value): |
---|
[1718] | 645 | brain = self.portal_catalog(portal_type=portal_type_query,id=value)[0] |
---|
[1907] | 646 | err = 'An ${portal_type} object with the Id ${id} already exists at ${path}.' |
---|
| 647 | mapping = {'portal_type': brain.portal_type, |
---|
| 648 | 'id': value, |
---|
| 649 | 'path': brain.getPath(), |
---|
| 650 | } |
---|
[388] | 651 | if err: |
---|
[1907] | 652 | datastructure.setError(widget_id, err, mapping) |
---|
[388] | 653 | else: |
---|
| 654 | datamodel = datastructure.getDataModel() |
---|
| 655 | datamodel[self.fields[0]] = value |
---|
[444] | 656 | |
---|
[388] | 657 | return not err |
---|
| 658 | |
---|
| 659 | InitializeClass(WAeUPIdWidget) |
---|
| 660 | |
---|
| 661 | widgetRegistry.register(WAeUPIdWidget) |
---|
| 662 | |
---|
| 663 | |
---|
| 664 | ###) |
---|
| 665 | |
---|
[1025] | 666 | class StudyCourseWidget(CPSStringWidget): ###( |
---|
| 667 | """ StudyCourse Widget""" |
---|
| 668 | meta_type = "StudyCourse Widget" |
---|
| 669 | |
---|
| 670 | def validate(self, datastructure, **kw): |
---|
| 671 | """Validate datastructure and update datamodel.""" |
---|
| 672 | #from Products.zdb import set_trace |
---|
| 673 | #set_trace() |
---|
[1747] | 674 | ## valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 675 | ## if not valid: |
---|
| 676 | ## return 0 |
---|
| 677 | widget_id = self.getWidgetId() |
---|
| 678 | value = makeCertificateCode(datastructure[widget_id]).upper() |
---|
[1025] | 679 | id_pat_str = r"\S" |
---|
[2110] | 680 | inv_id_pat = re.compile(r"^%s$" % id_pat_str) |
---|
[1747] | 681 | err = 0 |
---|
| 682 | if len(value.split()) > 1: |
---|
| 683 | err = 'Invalid Id, Id contains space(s).' |
---|
| 684 | elif not self.portal_catalog(portal_type='Certificate',id=value): |
---|
| 685 | err = 'No such certificate' |
---|
| 686 | if err: |
---|
| 687 | datastructure.setError(widget_id, err) |
---|
[1025] | 688 | else: |
---|
[1747] | 689 | datamodel = datastructure.getDataModel() |
---|
| 690 | datamodel[self.fields[0]] = value |
---|
| 691 | return not err |
---|
[1025] | 692 | |
---|
| 693 | InitializeClass(StudyCourseWidget) |
---|
| 694 | |
---|
| 695 | widgetRegistry.register(StudyCourseWidget) |
---|
| 696 | ###) |
---|
| 697 | |
---|
[463] | 698 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
| 699 | """ JambRegNo Widget""" |
---|
| 700 | meta_type = "JambRegNo Widget" |
---|
[1169] | 701 | _properties = CPSStringWidget._properties + ( |
---|
| 702 | {'id': 'catalog', 'type': 'string', 'mode': 'w', |
---|
| 703 | 'label': 'Catalog to search'}, |
---|
| 704 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 705 | 'label': 'Reference Field'}, |
---|
| 706 | ) |
---|
[2100] | 707 | #catalog = "portal_pumeresults" #the catalog to search for jamb_reg_no |
---|
[1169] | 708 | reference = "" |
---|
[463] | 709 | digits = 8 |
---|
| 710 | digits_str = "N"*digits |
---|
| 711 | letters = 2 |
---|
| 712 | letters_str = "L"*letters |
---|
| 713 | def validate(self, datastructure, **kw): |
---|
| 714 | """Validate datastructure and update datamodel.""" |
---|
| 715 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[2100] | 716 | reg_no_catalog = getattr(self,self.catalog) |
---|
[1169] | 717 | widget_id = self.getWidgetId() |
---|
| 718 | value = datastructure[widget_id].upper() |
---|
| 719 | err = 0 |
---|
[2094] | 720 | #import pdb;pdb.set_trace() |
---|
| 721 | if kw.has_key('mode'): |
---|
| 722 | mode = kw['mode'] |
---|
| 723 | else: |
---|
[2098] | 724 | mode = "edit" |
---|
[2099] | 725 | if not valid: |
---|
[2100] | 726 | err = 'Invalid registration number' |
---|
[1169] | 727 | elif self.reference == '': |
---|
| 728 | #s = getStudentByRegNo(self,value) |
---|
[2100] | 729 | pume = reg_no_catalog(jamb_reg_no = value) |
---|
[1169] | 730 | if len(pume) < 1: |
---|
[2100] | 731 | err = 'No student record with this registration number' |
---|
[1169] | 732 | else: |
---|
| 733 | datastructure['pume'] = pume[0] |
---|
[2094] | 734 | elif mode == 'add': |
---|
| 735 | pass |
---|
[2095] | 736 | elif self.reference != '' and self.catalog == "applicants_catalog": |
---|
[2100] | 737 | res = reg_no_catalog.searchResults({"%s" % self.reference: value}) |
---|
[2094] | 738 | if len(res) != 1: |
---|
[2100] | 739 | err = 'No record with this registration number' |
---|
[2094] | 740 | else: |
---|
| 741 | datastructure['record'] = res[0] |
---|
[463] | 742 | else: |
---|
[1169] | 743 | record = datastructure[self.reference] |
---|
| 744 | #jamb_reg_no = getattr(record,widget_id) |
---|
| 745 | jamb_reg_no = record.Entryregno |
---|
| 746 | if jamb_reg_no != value: |
---|
[1783] | 747 | err = 'Registration number does not match.' |
---|
[1169] | 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 |
---|
[463] | 754 | |
---|
| 755 | InitializeClass(JambRegNoWidget) |
---|
| 756 | |
---|
| 757 | widgetRegistry.register(JambRegNoWidget) |
---|
[47] | 758 | ###) |
---|
| 759 | |
---|
[1175] | 760 | class SecretWidget(CPSStringWidget): ###( |
---|
| 761 | """ Secret Widget""" |
---|
| 762 | meta_type = "Secret Widget" |
---|
| 763 | _properties = CPSStringWidget._properties + ( |
---|
| 764 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 765 | 'label': 'Reference Record'}, |
---|
| 766 | {'id': 'check_fields', 'type': 'tokens', 'mode': 'w', |
---|
| 767 | 'label': 'Fields to check'}, |
---|
| 768 | ) |
---|
| 769 | reference = "student" |
---|
[1747] | 770 | matric_no_catalog = 'returning_import' |
---|
[1175] | 771 | check_fields = ("Firstname", "Middlename","Lastname") |
---|
| 772 | def validate(self, datastructure, **kw): |
---|
| 773 | """Validate datastructure and update datamodel.""" |
---|
[1571] | 774 | logger = logging.getLogger('Widgets.SecretWidget.validate') |
---|
[1175] | 775 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 776 | widget_id = self.getWidgetId() |
---|
| 777 | value = datastructure[widget_id].upper() |
---|
| 778 | err = 0 |
---|
[1189] | 779 | record = datastructure.get(self.reference,None) |
---|
[1747] | 780 | #import pdb;pdb.set_trace() |
---|
[1379] | 781 | if not valid or len(value) < 2: |
---|
[1793] | 782 | err = 'Invalid string' |
---|
[1189] | 783 | elif not record or datastructure.errors: |
---|
[1177] | 784 | err = 0 |
---|
[1175] | 785 | else: |
---|
| 786 | found = False |
---|
| 787 | cvs = [] |
---|
| 788 | for field in self.check_fields: |
---|
| 789 | cv = getattr(record,field).upper() |
---|
[1243] | 790 | if len(cv.split()) > 1: |
---|
| 791 | for splited in cv.split(): |
---|
[1747] | 792 | cvs.append(splited.strip()) |
---|
[1243] | 793 | else: |
---|
| 794 | cvs.append(cv) |
---|
[1747] | 795 | for cv in cvs: |
---|
[1175] | 796 | if cv == value.upper(): |
---|
| 797 | found = True |
---|
| 798 | break |
---|
| 799 | matric_no = record.matric_no |
---|
| 800 | name = " ".join(cvs) |
---|
| 801 | if not found: |
---|
[1573] | 802 | logger.info('%(matric_no)s did not find %(value)s in %(name)s' % vars()) |
---|
| 803 | err = 'No name does match.' |
---|
[1175] | 804 | else: |
---|
[1571] | 805 | logger.info('%(matric_no)s found %(value)s in %(name)s' % vars()) |
---|
[1175] | 806 | if err: |
---|
| 807 | datastructure.setError(widget_id, err) |
---|
| 808 | else: |
---|
| 809 | datamodel = datastructure.getDataModel() |
---|
| 810 | datamodel[self.fields[0]] = value |
---|
| 811 | return not err |
---|
| 812 | |
---|
| 813 | InitializeClass(SecretWidget) |
---|
| 814 | |
---|
| 815 | widgetRegistry.register(SecretWidget) |
---|
| 816 | ###) |
---|
| 817 | |
---|
[1804] | 818 | class WAeUPSexWidget(CPSBooleanWidget): ###( |
---|
[1772] | 819 | """WAeUP sex widget.""" |
---|
| 820 | meta_type = 'WAeUP Sex Widget' |
---|
| 821 | |
---|
| 822 | def validate(self, datastructure, **kw): |
---|
| 823 | """Validate datastructure and update datamodel.""" |
---|
| 824 | value = datastructure[self.getWidgetId()] |
---|
| 825 | |
---|
| 826 | if self.render_format not in self.render_formats: |
---|
| 827 | self.render_format = 'select' |
---|
| 828 | |
---|
| 829 | female = value in ('F','f','Female','female',"True",True) |
---|
| 830 | male = value in ('M','m','Male','male','False',False) |
---|
| 831 | if not female and not male: |
---|
| 832 | datastructure.setError(self.getWidgetId(), |
---|
| 833 | "invalid sex %s" % value) |
---|
| 834 | return 0 |
---|
| 835 | elif female: |
---|
| 836 | v = True |
---|
| 837 | else: |
---|
| 838 | v = False |
---|
| 839 | datamodel = datastructure.getDataModel() |
---|
| 840 | datamodel[self.fields[0]] = v |
---|
| 841 | return 1 |
---|
| 842 | |
---|
| 843 | InitializeClass(WAeUPSexWidget) |
---|
| 844 | |
---|
| 845 | widgetRegistry.register(WAeUPSexWidget) |
---|
| 846 | |
---|
[1804] | 847 | ###) |
---|
| 848 | |
---|
[1146] | 849 | class MatricNoWidget(CPSStringWidget): ###( |
---|
| 850 | """ MatricNo Widget""" |
---|
| 851 | meta_type = "MatricNo Widget" |
---|
[1747] | 852 | |
---|
| 853 | _properties = CPSStringWidget._properties + ( |
---|
| 854 | {'id': 'matric_no_catalog', 'type': 'string', 'mode': 'w', |
---|
| 855 | 'label': 'Catalog to search for MatricNo'}, |
---|
| 856 | { 'id': 'results_catalog', 'type': 'string', 'mode': 'w', |
---|
| 857 | 'label': 'Results Catalog'}, |
---|
| 858 | ) |
---|
| 859 | matric_no_catalog = "" #the catalog to search for matric_no |
---|
[1748] | 860 | results_catalog = "results_import" #results catalog |
---|
[1747] | 861 | |
---|
[1146] | 862 | def validate(self, datastructure, **kw): |
---|
| 863 | """Validate datastructure and update datamodel.""" |
---|
[1747] | 864 | #import pdb;pdb.set_trace() |
---|
[1146] | 865 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[1571] | 866 | logger = logging.getLogger('Widgets.MatricNoWidget.validate') |
---|
[1747] | 867 | returning = getattr(self,self.matric_no_catalog) |
---|
| 868 | results = getattr(self,self.results_catalog,None) |
---|
[1146] | 869 | err = 0 |
---|
[1189] | 870 | widget_id = self.getWidgetId() |
---|
| 871 | value = datastructure[widget_id] |
---|
| 872 | if not valid or not value: |
---|
[1891] | 873 | err = 'Invalid string' |
---|
[1573] | 874 | logger.info('Invalid matric_no string %s' % value) |
---|
[1146] | 875 | else: |
---|
[1189] | 876 | value = value.upper() |
---|
[1177] | 877 | datastructure['student'] = None |
---|
[1146] | 878 | while not err: |
---|
[1151] | 879 | res = returning(matric_no = value) |
---|
[1146] | 880 | if len(res) < 1: |
---|
[1573] | 881 | logger.info('matric_no %s not found' % value) |
---|
[1915] | 882 | err = 'No student with this matriculation number.' |
---|
[1747] | 883 | break |
---|
[1146] | 884 | datastructure['student'] = res[0] |
---|
[1747] | 885 | if results is not None: |
---|
| 886 | res = results(matric_no = value) |
---|
| 887 | if len(res) < 1: |
---|
[1891] | 888 | err = 'No results for this matriculation number' |
---|
[1747] | 889 | continue |
---|
| 890 | datastructure['results'] = res |
---|
[1146] | 891 | break |
---|
[1189] | 892 | if err: |
---|
| 893 | datastructure.setError(widget_id, err) |
---|
| 894 | else: |
---|
| 895 | datamodel = datastructure.getDataModel() |
---|
| 896 | datamodel[self.fields[0]] = value |
---|
| 897 | return not err |
---|
[1146] | 898 | |
---|
| 899 | InitializeClass(MatricNoWidget) |
---|
| 900 | |
---|
| 901 | widgetRegistry.register(MatricNoWidget) |
---|
| 902 | ###) |
---|
| 903 | |
---|
[1393] | 904 | class StudentIdWidget(CPSStringWidget): ###( |
---|
| 905 | """ StudentId Widget""" |
---|
| 906 | meta_type = "StudentId Widget" |
---|
| 907 | def validate(self, datastructure, **kw): |
---|
| 908 | """Validate datastructure and update datamodel.""" |
---|
| 909 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[1571] | 910 | logger = logging.getLogger('Widgets.StudentIdWidget.validate') |
---|
[1393] | 911 | #import pdb;pdb.set_trace() |
---|
| 912 | s_cat = self.students_catalog |
---|
| 913 | err = 0 |
---|
| 914 | widget_id = self.getWidgetId() |
---|
| 915 | value = datastructure[widget_id] |
---|
| 916 | if not valid or not value: |
---|
[1891] | 917 | err = 'Invalid Id string' |
---|
[1573] | 918 | logger.info('Invalid id string %s' % value) |
---|
[1449] | 919 | datastructure['student'] = None |
---|
[1393] | 920 | else: |
---|
| 921 | value = value.upper() |
---|
| 922 | res = s_cat(id = value) |
---|
| 923 | if not res: |
---|
[1573] | 924 | logger.info('Student id %s not found' % value) |
---|
[1891] | 925 | err = 'No student with this Id' |
---|
[1393] | 926 | datastructure['student'] = None |
---|
| 927 | else: |
---|
| 928 | datastructure['student'] = res[0] |
---|
| 929 | if err: |
---|
| 930 | datastructure.setError(widget_id, err) |
---|
| 931 | else: |
---|
| 932 | datamodel = datastructure.getDataModel() |
---|
| 933 | datamodel[self.fields[0]] = value |
---|
| 934 | return not err |
---|
| 935 | |
---|
| 936 | InitializeClass(StudentIdWidget) |
---|
| 937 | |
---|
| 938 | widgetRegistry.register(StudentIdWidget) |
---|
| 939 | ###) |
---|
| 940 | |
---|
[1146] | 941 | class WAeUPMultilineResultsWidget(CPSStringWidget): ###( |
---|
| 942 | """ WAeUPMultilineResults Widget""" |
---|
| 943 | meta_type = "WAeUp Multiline Results Widget" |
---|
| 944 | _properties = CPSWidget._properties + ( |
---|
| 945 | {'id': 'nr_of_lines', 'type': 'int', 'mode': 'w', |
---|
| 946 | 'label': 'Nr of Lines'}, |
---|
| 947 | ) |
---|
| 948 | nr_of_lines = 5 |
---|
| 949 | def prepare(self, datastructure, **kw): ###( |
---|
| 950 | """Prepare datastructure from datamodel.""" |
---|
| 951 | datamodel = datastructure.getDataModel() |
---|
| 952 | #import pdb;pdb.set_trace() |
---|
| 953 | widget_id = self.getWidgetId() |
---|
| 954 | v = datamodel[self.fields[0]] |
---|
| 955 | if type(v) is ListType and v: |
---|
| 956 | nr_results = len(v) |
---|
| 957 | else: |
---|
| 958 | v = [] |
---|
| 959 | nr_results = 0 |
---|
| 960 | count = 1 |
---|
| 961 | for s,g in v: |
---|
| 962 | wid = "%s%02d"% (widget_id,count) |
---|
| 963 | datastructure[wid+'_s'] = s |
---|
| 964 | datastructure[wid+'_g'] = g |
---|
| 965 | count += 1 |
---|
| 966 | if nr_results < self.nr_of_lines: |
---|
| 967 | for line in range(nr_results,self.nr_of_lines): |
---|
| 968 | v.append(('','')) |
---|
| 969 | wid = "%s%02d"% (widget_id,line) |
---|
| 970 | datastructure[wid+'_s'] = '' |
---|
| 971 | datastructure[wid+'_g'] = '' |
---|
| 972 | datastructure[widget_id] = v |
---|
| 973 | datastructure[widget_id+'_s'] = '' |
---|
| 974 | datastructure[widget_id+'_g'] = '' |
---|
| 975 | ###) |
---|
| 976 | |
---|
| 977 | def validate(self, datastructure, **kw): ###( |
---|
| 978 | """Validate datastructure and update datamodel.""" |
---|
| 979 | #import pdb;pdb.set_trace() |
---|
| 980 | widget_id = self.getWidgetId() |
---|
| 981 | err = 0 |
---|
| 982 | lines = [] |
---|
| 983 | for line in range(1,30): |
---|
| 984 | wid = "%s%02d"% (widget_id,line) |
---|
| 985 | if not datastructure.has_key(wid+'_s'): |
---|
| 986 | break |
---|
| 987 | lines.append((datastructure[wid+'_s'].strip(), |
---|
| 988 | datastructure[wid+'_g'].strip())) |
---|
[1155] | 989 | |
---|
[1146] | 990 | s = datastructure[widget_id+'_s'].strip() |
---|
| 991 | g = datastructure[widget_id+'_g'].strip() |
---|
| 992 | if s and g: |
---|
| 993 | lines.append((s,g)) |
---|
| 994 | active = [] |
---|
| 995 | for s,g in lines: |
---|
| 996 | if g != "": |
---|
| 997 | active.append((s,g)) |
---|
| 998 | if err: |
---|
| 999 | datastructure.setError(widget_id, err) |
---|
| 1000 | else: |
---|
| 1001 | datamodel = datastructure.getDataModel() |
---|
| 1002 | datamodel[self.fields[0]] = active |
---|
| 1003 | return not err |
---|
| 1004 | ###) |
---|
| 1005 | |
---|
| 1006 | def render(self, mode, datastructure, **kw): ###( |
---|
| 1007 | """Render in mode from datastructure.""" |
---|
| 1008 | render_method = 'widget_waeup_multiline_result_render' |
---|
| 1009 | meth = getattr(self, render_method, None) |
---|
| 1010 | if meth is None: |
---|
| 1011 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1012 | % (render_method, self.getId())) |
---|
| 1013 | #import pdb;pdb.set_trace() |
---|
| 1014 | datamodel = datastructure.getDataModel() |
---|
| 1015 | widget_id = self.getWidgetId() |
---|
| 1016 | lines = datamodel[self.fields[0]] |
---|
| 1017 | if len(lines) < self.nr_of_lines: |
---|
| 1018 | for line in range(len(lines),self.nr_of_lines + 1): |
---|
| 1019 | lines.append(('','')) |
---|
| 1020 | datastructure[widget_id] = lines |
---|
| 1021 | datastructure[widget_id+'_s'] = '' |
---|
| 1022 | datastructure[widget_id+'_g'] = '' |
---|
| 1023 | ## count = 1 |
---|
| 1024 | ## for s,g in v: |
---|
| 1025 | ## wid = "%s%02d"% (widget_id,count) |
---|
| 1026 | ## count += 1 |
---|
| 1027 | return meth(mode=mode, |
---|
| 1028 | datastructure=datastructure, |
---|
| 1029 | ) |
---|
| 1030 | ###) |
---|
| 1031 | |
---|
| 1032 | |
---|
| 1033 | InitializeClass(WAeUPMultilineResultsWidget) |
---|
| 1034 | widgetRegistry.register(WAeUPMultilineResultsWidget) |
---|
| 1035 | ###) |
---|
| 1036 | |
---|
[794] | 1037 | class WAeUPResultsWidget(CPSStringWidget): ###( |
---|
| 1038 | """ WAeUPResults Widget""" |
---|
| 1039 | meta_type = "WAeUp Results Widget" |
---|
| 1040 | |
---|
| 1041 | def prepare(self, datastructure, **kw): ###( |
---|
| 1042 | """Prepare datastructure from datamodel.""" |
---|
| 1043 | datamodel = datastructure.getDataModel() |
---|
| 1044 | v = datamodel[self.fields[0]] |
---|
| 1045 | #import pdb;pdb.set_trace() |
---|
| 1046 | widget_id = self.getWidgetId() |
---|
[807] | 1047 | datastructure[widget_id] = v |
---|
[794] | 1048 | datastructure[widget_id+'_s'] = '' |
---|
| 1049 | datastructure[widget_id+'_g'] = '' |
---|
| 1050 | ###) |
---|
| 1051 | |
---|
| 1052 | def validate(self, datastructure, **kw): ###( |
---|
| 1053 | """Validate datastructure and update datamodel.""" |
---|
[807] | 1054 | #import pdb;pdb.set_trace() |
---|
[794] | 1055 | widget_id = self.getWidgetId() |
---|
| 1056 | v = datastructure[widget_id] |
---|
| 1057 | err = 0 |
---|
| 1058 | s = datastructure[widget_id+'_s'].strip() |
---|
| 1059 | g = datastructure[widget_id+'_g'].strip() |
---|
[807] | 1060 | while 1: |
---|
| 1061 | if not s and g: |
---|
[1891] | 1062 | err = "No subject grade for subject" |
---|
[807] | 1063 | break |
---|
| 1064 | i = 0 |
---|
| 1065 | done = False |
---|
| 1066 | for sv,gv in v: |
---|
| 1067 | if sv == s: |
---|
| 1068 | done = True |
---|
| 1069 | if not g: |
---|
| 1070 | v.pop(i) |
---|
| 1071 | break |
---|
| 1072 | v[i] = (s,g) |
---|
| 1073 | break |
---|
| 1074 | i += 1 |
---|
| 1075 | if done: |
---|
| 1076 | break |
---|
| 1077 | if s and g: |
---|
| 1078 | v.append((s,g)) |
---|
| 1079 | break |
---|
[794] | 1080 | if err: |
---|
| 1081 | datastructure.setError(widget_id, err) |
---|
| 1082 | else: |
---|
| 1083 | datamodel = datastructure.getDataModel() |
---|
| 1084 | datamodel[self.fields[0]] = v |
---|
| 1085 | datastructure[widget_id+'_s'] = s |
---|
| 1086 | datastructure[widget_id+'_g'] = g |
---|
| 1087 | return not err |
---|
| 1088 | ###) |
---|
| 1089 | |
---|
| 1090 | def render(self, mode, datastructure, **kw): ###( |
---|
| 1091 | """Render in mode from datastructure.""" |
---|
| 1092 | render_method = 'widget_waeup_result_render' |
---|
| 1093 | meth = getattr(self, render_method, None) |
---|
| 1094 | if meth is None: |
---|
| 1095 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1096 | % (render_method, self.getId())) |
---|
| 1097 | #import pdb;pdb.set_trace() |
---|
| 1098 | datamodel = datastructure.getDataModel() |
---|
| 1099 | widget_id = self.getWidgetId() |
---|
| 1100 | datastructure[widget_id+'_s'] = '' |
---|
| 1101 | datastructure[widget_id+'_g'] = '' |
---|
| 1102 | return meth(mode=mode, |
---|
| 1103 | datastructure=datastructure, |
---|
| 1104 | ) |
---|
| 1105 | ###) |
---|
| 1106 | |
---|
| 1107 | |
---|
| 1108 | InitializeClass(WAeUPResultsWidget) |
---|
| 1109 | widgetRegistry.register(WAeUPResultsWidget) |
---|
| 1110 | ###) |
---|
| 1111 | |
---|
[488] | 1112 | class ScratchCardPin: ###( |
---|
| 1113 | """the ScratchCardPin""" |
---|
| 1114 | def __init__(self,prefix,batch_no,number): |
---|
[990] | 1115 | if not batch_no and not number: |
---|
| 1116 | s = prefix |
---|
[996] | 1117 | if len(s) > 3: |
---|
[990] | 1118 | prefix,batch_no,number = s[:3],s[3:-10],s[-10:] |
---|
| 1119 | else: |
---|
[996] | 1120 | prefix,batch_no,number = s,'','' |
---|
[488] | 1121 | self.p = prefix |
---|
| 1122 | self.b = batch_no |
---|
| 1123 | self.n = number |
---|
| 1124 | |
---|
| 1125 | def __str__(self): |
---|
| 1126 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
[1146] | 1127 | |
---|
| 1128 | def __repr__(self): |
---|
| 1129 | return "%s%s%s" % (self.p,self.b,self.n) |
---|
[488] | 1130 | ###) |
---|
| 1131 | |
---|
[47] | 1132 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
[22] | 1133 | """ ScratchcardPin Widget""" |
---|
[199] | 1134 | meta_type = "Scratchcard Pin Widget" |
---|
[488] | 1135 | _properties = CPSWidget._properties + ( |
---|
| 1136 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
| 1137 | 'label': 'Prefix'}, |
---|
| 1138 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 1139 | 'label': 'Reference Field'}, |
---|
| 1140 | ) |
---|
| 1141 | prefix = '' |
---|
| 1142 | reference = '' |
---|
[502] | 1143 | def prepare(self, datastructure, **kw): ###( |
---|
[488] | 1144 | """Prepare datastructure from datamodel.""" |
---|
| 1145 | datamodel = datastructure.getDataModel() |
---|
| 1146 | v = datamodel[self.fields[0]] |
---|
| 1147 | widget_id = self.getWidgetId() |
---|
[747] | 1148 | if v and type(v) is StringType: |
---|
[990] | 1149 | try: |
---|
| 1150 | p,b,n = v.split('-') |
---|
| 1151 | v = ScratchCardPin(p,b,n) |
---|
| 1152 | except ValueError: |
---|
| 1153 | v = ScratchCardPin(v,'','') |
---|
[488] | 1154 | if v: |
---|
[742] | 1155 | p = '%s' % v.p |
---|
[488] | 1156 | b = '%s' % v.b |
---|
| 1157 | n = '%s' % v.n |
---|
| 1158 | else: |
---|
[742] | 1159 | p = self.prefix |
---|
| 1160 | if p.startswith('@'): |
---|
| 1161 | p = getattr(self,self.prefix[1:])() |
---|
[488] | 1162 | b = n = '' |
---|
[742] | 1163 | v = ScratchCardPin(p,b,n) |
---|
[488] | 1164 | datastructure[widget_id] = v |
---|
[742] | 1165 | datastructure[widget_id+'_p'] = p |
---|
[488] | 1166 | datastructure[widget_id+'_b'] = b |
---|
| 1167 | datastructure[widget_id+'_n'] = n |
---|
[1376] | 1168 | ###) |
---|
[758] | 1169 | |
---|
[1169] | 1170 | def validate(self, datastructure, **kw): ###( |
---|
[22] | 1171 | """Validate datastructure and update datamodel.""" |
---|
[1571] | 1172 | s_logger = logging.getLogger('Widgets.ScratchcardPinWidget.validate') |
---|
[488] | 1173 | widget_id = self.getWidgetId() |
---|
| 1174 | v = datastructure[widget_id] |
---|
[996] | 1175 | #import pdb;pdb.set_trace() |
---|
[488] | 1176 | err = 0 |
---|
[1907] | 1177 | mapping = {} |
---|
[742] | 1178 | prefix= self.prefix |
---|
| 1179 | if prefix.startswith('@'): |
---|
| 1180 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 1181 | b = datastructure[widget_id+'_b'].strip() |
---|
| 1182 | n = datastructure[widget_id+'_n'].strip() |
---|
[502] | 1183 | pins = self.portal_pins |
---|
[742] | 1184 | pin = "%(prefix)s%(b)s%(n)s" % vars() |
---|
[2001] | 1185 | pin_str = "%(prefix)s-%(b)s-%(n)s" % vars() |
---|
[816] | 1186 | do = 1 |
---|
[1025] | 1187 | s_id = str(self.portal_membership.getAuthenticatedMember()) |
---|
[1343] | 1188 | if self.isStaff(): |
---|
[1326] | 1189 | do = 0 |
---|
| 1190 | err ='You are not a Student. PIN neither checked nor used.' |
---|
[1571] | 1191 | s_logger.info('%s tried to use scratch card %s' % (s_id,pin_str)) |
---|
[1557] | 1192 | elif len(b) > 1 and b.find('-') > -1: |
---|
| 1193 | do = 0 |
---|
[1573] | 1194 | err = 'PIN must not contain "-"' |
---|
| 1195 | s_logger.info('%s entered invalid PIN containing "-"' % (s_id)) |
---|
[1557] | 1196 | elif n.find('-') > -1: |
---|
| 1197 | do = 0 |
---|
[1573] | 1198 | err = 'PIN must not contain "-"' |
---|
| 1199 | s_logger.info('%s entered invalid PIN containing "-"' % (s_id)) |
---|
[1376] | 1200 | elif len(n) != 10: |
---|
| 1201 | do = 0 |
---|
[1891] | 1202 | err = 'Invalid PIN length' |
---|
[1573] | 1203 | s_logger.info('%s entered invalid PIN with length %d' % (s_id,len(n))) |
---|
[1326] | 1204 | elif self.reference == "": |
---|
[1030] | 1205 | ref = s_id |
---|
[635] | 1206 | else: |
---|
| 1207 | ref = datastructure[self.reference] |
---|
[843] | 1208 | if datastructure.errors: |
---|
[816] | 1209 | do = 0 |
---|
[1805] | 1210 | datastructure.setError(widget_id, 'PIN neither checked nor used.') |
---|
[1571] | 1211 | s_logger.info('%s/%s entered wrong data together with PIN %s' % (s_id,ref,pin_str)) |
---|
[816] | 1212 | while do: |
---|
| 1213 | ok = pins.searchAndSetRecord(pin,ref,prefix) |
---|
[1030] | 1214 | if ok < -2 or ok > 2: |
---|
| 1215 | err = 'Unknown error, please report!' |
---|
[1571] | 1216 | s_logger.info('%s/%s caused unknown error with PIN %s' % (s_id,ref,pin_str)) |
---|
[1030] | 1217 | break |
---|
| 1218 | elif ok == -2: |
---|
[1783] | 1219 | err = 'Service already is activated but with a different PIN.' |
---|
[1571] | 1220 | s_logger.info('%s/%s repeatedly activated service but with different PIN %s' % (s_id,ref,pin_str)) |
---|
[710] | 1221 | break |
---|
| 1222 | elif ok == -1: |
---|
[1793] | 1223 | err = 'Invalid PIN' |
---|
[1571] | 1224 | s_logger.info('%s/%s entered invalid PIN %s' % (s_id,ref,pin_str)) |
---|
[502] | 1225 | break |
---|
| 1226 | if ok == 0: |
---|
[1571] | 1227 | err = 'PIN already used' |
---|
| 1228 | s_logger.info('%s/%s entered used PIN %s' % (s_id,ref,pin_str)) |
---|
[502] | 1229 | break |
---|
| 1230 | if ok >= 1: |
---|
[710] | 1231 | #import pdb;pdb.set_trace() |
---|
[635] | 1232 | if self.isStudent(): |
---|
[992] | 1233 | if self.reference == "jamb_reg_no": |
---|
[997] | 1234 | err = "You are already logged in." |
---|
[1573] | 1235 | s_logger.info('%s/%s checked admission with PIN %s though logged in' % (s_id,ref,pin_str)) |
---|
[992] | 1236 | break |
---|
[1082] | 1237 | if ok == 1: |
---|
[1571] | 1238 | s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str)) |
---|
[1032] | 1239 | else: |
---|
[1571] | 1240 | s_logger.info('%s/%s repeatedly used PIN %s' % (s_id,ref,pin_str)) |
---|
[637] | 1241 | break |
---|
[635] | 1242 | else: |
---|
| 1243 | student = getStudentByRegNo(self,ref) |
---|
[1571] | 1244 | s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str)) |
---|
[502] | 1245 | if student is None: |
---|
[1793] | 1246 | err = "Student not found" |
---|
[1571] | 1247 | s_logger.info('%s not found in admission list' % (ref)) |
---|
[502] | 1248 | break |
---|
[648] | 1249 | s_id = student.getId() |
---|
[502] | 1250 | if ok == 2: |
---|
[990] | 1251 | if self.reference == "jamb_reg_no": |
---|
| 1252 | if hasattr(self.portal_directories.students,s_id): |
---|
[1907] | 1253 | err = "Please login with your Student Id ${id} and 10-digit PIN." |
---|
| 1254 | mapping = {'id': s_id} |
---|
[1571] | 1255 | s_logger.info('%s/%s repeatedly checked admission with PIN %s' % (s_id,ref,pin_str)) |
---|
[990] | 1256 | break |
---|
| 1257 | else: |
---|
[1571] | 1258 | s_logger.info('%s/%s (non-member) repeatedly checked admission with PIN %s' % (s_id,ref,pin_str)) |
---|
[990] | 1259 | else: |
---|
[1891] | 1260 | err = "Unknown error" |
---|
[1571] | 1261 | s_logger.info('%s/%s repeatedly activated service with PIN %s' % (s_id,ref,pin_str)) |
---|
[986] | 1262 | break |
---|
| 1263 | try: |
---|
| 1264 | student.getContent().makeStudentMember(s_id,password=pin[4:]) |
---|
[1571] | 1265 | s_logger.info('%s/%s has been created using PIN %s' % (s_id,ref,pin_str)) |
---|
[986] | 1266 | except: |
---|
[1907] | 1267 | err = "Please login with your Student Id ${id} and 10-digit PIN." |
---|
| 1268 | mapping = {'id': s_id} |
---|
[1571] | 1269 | s_logger.info('%s/%s could not be made a member with PIN %s' % (s_id,ref,pin_str)) |
---|
[986] | 1270 | break |
---|
[502] | 1271 | break |
---|
[488] | 1272 | if err: |
---|
[1907] | 1273 | datastructure.setError(widget_id, err,mapping) |
---|
[488] | 1274 | else: |
---|
| 1275 | datamodel = datastructure.getDataModel() |
---|
[742] | 1276 | datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n) |
---|
| 1277 | datastructure[widget_id] = ScratchCardPin(prefix,b,n) |
---|
| 1278 | datastructure[widget_id+'_p'] = prefix |
---|
[488] | 1279 | datastructure[widget_id+'_b'] = b |
---|
| 1280 | datastructure[widget_id+'_n'] = n |
---|
[502] | 1281 | datastructure['s_id'] = s_id |
---|
[488] | 1282 | return not err |
---|
[444] | 1283 | |
---|
[1169] | 1284 | ###) |
---|
| 1285 | |
---|
[502] | 1286 | def render(self, mode, datastructure, **kw): ###( |
---|
[488] | 1287 | """Render in mode from datastructure.""" |
---|
| 1288 | render_method = 'widget_scratch_card_pin_render' |
---|
| 1289 | meth = getattr(self, render_method, None) |
---|
| 1290 | if meth is None: |
---|
| 1291 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1292 | % (render_method, self.getId())) |
---|
| 1293 | |
---|
| 1294 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
| 1295 | # or create mode, because a default value has to be provided. |
---|
| 1296 | #import pdb;pdb.set_trace() |
---|
| 1297 | datamodel = datastructure.getDataModel() |
---|
| 1298 | v = datamodel[self.fields[0]] |
---|
[502] | 1299 | if v and type(v) is StringType: |
---|
[990] | 1300 | try: |
---|
| 1301 | p,b,n = v.split('-') |
---|
| 1302 | v = ScratchCardPin(p,b,n) |
---|
| 1303 | except ValueError: |
---|
[2002] | 1304 | v = ScratchCardPin(self.prefix,'XXX',v) |
---|
[996] | 1305 | pass |
---|
[488] | 1306 | if v: |
---|
[742] | 1307 | prefix= '%s' % v.p |
---|
[488] | 1308 | b = '%s' % v.b |
---|
| 1309 | n = '%s' % v.n |
---|
[22] | 1310 | else: |
---|
[742] | 1311 | prefix= self.prefix |
---|
| 1312 | if prefix.startswith('@'): |
---|
| 1313 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 1314 | b = n = '' |
---|
[742] | 1315 | v = ScratchCardPin(prefix,b,n) |
---|
| 1316 | widget_id = self.getWidgetId() |
---|
| 1317 | datastructure[widget_id] = v |
---|
| 1318 | datastructure[widget_id+'_p'] = prefix |
---|
| 1319 | datastructure[widget_id+'_b'] = b |
---|
| 1320 | datastructure[widget_id+'_n'] = n |
---|
[758] | 1321 | return meth(mode=mode, |
---|
| 1322 | datastructure=datastructure, |
---|
[488] | 1323 | ) |
---|
[523] | 1324 | ###) |
---|
[488] | 1325 | |
---|
[22] | 1326 | InitializeClass(ScratchcardPinWidget) |
---|
[199] | 1327 | widgetRegistry.register(ScratchcardPinWidget) |
---|
[2094] | 1328 | ###) |
---|
[22] | 1329 | |
---|
[2094] | 1330 | class PumePinWidget(ScratchcardPinWidget): ###( |
---|
| 1331 | """ Pume Pin Widget""" |
---|
| 1332 | meta_type = "Pume Pin Widget" |
---|
[2098] | 1333 | catalog = "applicants_catalog" |
---|
[2099] | 1334 | |
---|
[2094] | 1335 | def validate(self, datastructure, **kw): ###( |
---|
| 1336 | """Validate datastructure and update datamodel.""" |
---|
| 1337 | s_logger = logging.getLogger('Widgets.ScratchcardPinWidget.validate') |
---|
| 1338 | widget_id = self.getWidgetId() |
---|
| 1339 | v = datastructure[widget_id] |
---|
| 1340 | err = 0 |
---|
| 1341 | mapping = {} |
---|
[2099] | 1342 | |
---|
[2094] | 1343 | prefix= self.prefix |
---|
| 1344 | if prefix.startswith('@'): |
---|
| 1345 | prefix= getattr(self,self.prefix[1:])() |
---|
| 1346 | b = datastructure[widget_id+'_b'].strip() |
---|
| 1347 | n = datastructure[widget_id+'_n'].strip() |
---|
| 1348 | pins = self.portal_pins |
---|
| 1349 | pin = "%(prefix)s%(b)s%(n)s" % vars() |
---|
| 1350 | pin_str = "%(prefix)s-%(b)s-%(n)s" % vars() |
---|
| 1351 | do = 1 |
---|
| 1352 | s_id = str(self.portal_membership.getAuthenticatedMember()) |
---|
| 1353 | if self.isStaff(): |
---|
| 1354 | do = 0 |
---|
| 1355 | err ='You are not a Student. PIN neither checked nor used.' |
---|
| 1356 | s_logger.info('%s tried to use scratch card %s' % (s_id,pin_str)) |
---|
| 1357 | elif len(b) > 1 and b.find('-') > -1: |
---|
| 1358 | do = 0 |
---|
| 1359 | err = 'PIN must not contain "-"' |
---|
| 1360 | s_logger.info('%s entered invalid PIN containing "-"' % (s_id)) |
---|
| 1361 | elif n.find('-') > -1: |
---|
| 1362 | do = 0 |
---|
| 1363 | err = 'PIN must not contain "-"' |
---|
| 1364 | s_logger.info('%s entered invalid PIN containing "-"' % (s_id)) |
---|
| 1365 | elif len(n) != 10: |
---|
| 1366 | do = 0 |
---|
| 1367 | err = 'Invalid PIN length' |
---|
| 1368 | s_logger.info('%s entered invalid PIN with length %d' % (s_id,len(n))) |
---|
| 1369 | elif self.reference == "": |
---|
| 1370 | ref = s_id |
---|
| 1371 | else: |
---|
| 1372 | ref = datastructure[self.reference] |
---|
| 1373 | if datastructure.errors: |
---|
| 1374 | do = 0 |
---|
| 1375 | datastructure.setError(widget_id, 'PIN neither checked nor used.') |
---|
| 1376 | s_logger.info('%s/%s entered wrong data together with PIN %s' % (s_id,ref,pin_str)) |
---|
| 1377 | while do: |
---|
| 1378 | ok = pins.searchAndSetRecord(pin,ref,prefix) |
---|
| 1379 | if ok < -2 or ok > 2: |
---|
| 1380 | err = 'Unknown error, please report!' |
---|
| 1381 | s_logger.info('%s/%s caused unknown error with PIN %s' % (s_id,ref,pin_str)) |
---|
| 1382 | break |
---|
| 1383 | elif ok == -2: |
---|
| 1384 | err = 'Service already is activated but with a different PIN.' |
---|
| 1385 | s_logger.info('%s/%s repeatedly activated service but with different PIN %s' % (s_id,ref,pin_str)) |
---|
| 1386 | break |
---|
| 1387 | elif ok == -1: |
---|
| 1388 | err = 'Invalid PIN' |
---|
| 1389 | s_logger.info('%s/%s entered invalid PIN %s' % (s_id,ref,pin_str)) |
---|
| 1390 | break |
---|
| 1391 | if ok == 0: |
---|
| 1392 | err = 'PIN already used' |
---|
| 1393 | s_logger.info('%s/%s entered used PIN %s' % (s_id,ref,pin_str)) |
---|
| 1394 | break |
---|
| 1395 | if ok >= 1: |
---|
| 1396 | if self.isStudent(): |
---|
| 1397 | err = "This is only for Pume application." |
---|
[2119] | 1398 | s_logger.info('%s/%s applied for screening test with PIN %s' % (s_id,ref,pin_str)) |
---|
[2094] | 1399 | break |
---|
| 1400 | else: |
---|
| 1401 | applicant = datastructure['record'] |
---|
[2099] | 1402 | if not applicant.pin: |
---|
[2094] | 1403 | s_logger.info('%s successfully used PIN %s' % (ref,pin_str)) |
---|
| 1404 | d = {} |
---|
| 1405 | d['reg_no'] = applicant.reg_no |
---|
[2098] | 1406 | d['pin'] = pin_str |
---|
[2144] | 1407 | d['status'] = 'entered' |
---|
[2098] | 1408 | getattr(self,self.catalog).modifyRecord(**d) |
---|
[2094] | 1409 | elif applicant.pin != pin_str: |
---|
[2119] | 1410 | s_logger.info('%s/%s tried to enter application record with different PIN %s' % (s_id,ref,pin_str)) |
---|
[2094] | 1411 | elif applicant.pin == pin_str: |
---|
[2119] | 1412 | s_logger.info('%s/%s repeatedly entered application record with PIN %s' % (s_id,ref,pin_str)) |
---|
[2094] | 1413 | break |
---|
| 1414 | if err: |
---|
| 1415 | datastructure.setError(widget_id, err,mapping) |
---|
| 1416 | else: |
---|
| 1417 | datamodel = datastructure.getDataModel() |
---|
| 1418 | datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n) |
---|
| 1419 | datastructure[widget_id] = ScratchCardPin(prefix,b,n) |
---|
| 1420 | datastructure[widget_id+'_p'] = prefix |
---|
| 1421 | datastructure[widget_id+'_b'] = b |
---|
| 1422 | datastructure[widget_id+'_n'] = n |
---|
| 1423 | datastructure['s_id'] = s_id |
---|
| 1424 | return not err |
---|
| 1425 | ###) |
---|
| 1426 | |
---|
[2098] | 1427 | def render(self, mode, datastructure, **kw): ###( |
---|
| 1428 | """Render in mode from datastructure.""" |
---|
| 1429 | render_method = 'widget_scratch_card_pin_render' |
---|
| 1430 | meth = getattr(self, render_method, None) |
---|
| 1431 | if meth is None: |
---|
| 1432 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1433 | % (render_method, self.getId())) |
---|
| 1434 | |
---|
| 1435 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
| 1436 | # or create mode, because a default value has to be provided. |
---|
| 1437 | #import pdb;pdb.set_trace() |
---|
| 1438 | datamodel = datastructure.getDataModel() |
---|
| 1439 | v = datamodel[self.fields[0]] |
---|
| 1440 | #import pdb;pdb.set_trace() |
---|
| 1441 | if v and type(v) is StringType: |
---|
| 1442 | try: |
---|
| 1443 | p,b,n = v.split('-') |
---|
| 1444 | v = ScratchCardPin(p,b,n) |
---|
| 1445 | except ValueError: |
---|
| 1446 | v = ScratchCardPin(self.prefix,'XXX',v) |
---|
| 1447 | pass |
---|
| 1448 | if v: |
---|
| 1449 | prefix= '%s' % v.p |
---|
| 1450 | b = '%s' % v.b |
---|
| 1451 | n = '%s' % v.n |
---|
| 1452 | else: |
---|
| 1453 | prefix= self.prefix |
---|
| 1454 | if prefix.startswith('@'): |
---|
| 1455 | prefix= getattr(self,self.prefix[1:])() |
---|
| 1456 | b = n = '' |
---|
| 1457 | v = ScratchCardPin(prefix,b,n) |
---|
| 1458 | widget_id = self.getWidgetId() |
---|
| 1459 | datastructure[widget_id] = v |
---|
| 1460 | datastructure[widget_id+'_p'] = prefix |
---|
| 1461 | datastructure[widget_id+'_b'] = b |
---|
| 1462 | datastructure[widget_id+'_n'] = n |
---|
| 1463 | return meth(mode=mode, |
---|
| 1464 | datastructure=datastructure, |
---|
| 1465 | ) |
---|
| 1466 | ###) |
---|
| 1467 | |
---|
[2094] | 1468 | InitializeClass(PumePinWidget) |
---|
| 1469 | widgetRegistry.register(PumePinWidget) |
---|
[47] | 1470 | ###) |
---|
| 1471 | |
---|
[1169] | 1472 | class WAeUPImageWidget(CPSImageWidget): ###( |
---|
[537] | 1473 | """Photo widget.""" |
---|
| 1474 | meta_type = 'WAeUP Image Widget' |
---|
| 1475 | |
---|
| 1476 | def render(self, mode, datastructure, **kw): |
---|
| 1477 | render_method = 'widget_waeup_image_render' |
---|
| 1478 | meth = getattr(self, render_method, None) |
---|
| 1479 | if meth is None: |
---|
| 1480 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1481 | % (render_method, self.getId())) |
---|
| 1482 | img_info = self.getImageInfo(datastructure) |
---|
| 1483 | return meth(mode=mode, datastructure=datastructure, **img_info) |
---|
| 1484 | |
---|
| 1485 | InitializeClass(WAeUPImageWidget) |
---|
| 1486 | |
---|
| 1487 | widgetRegistry.register(WAeUPImageWidget) |
---|
[1804] | 1488 | ###) |
---|
[537] | 1489 | |
---|
[2110] | 1490 | class NoZodbImageWidget(CPSImageWidget): ###( |
---|
| 1491 | """Image widget with filesystem storage.""" |
---|
| 1492 | meta_type = 'No Zodb Image Widget' |
---|
| 1493 | _properties = CPSImageWidget._properties +\ |
---|
| 1494 | ( |
---|
| 1495 | {'id': 'path', 'type': 'string', 'mode': 'w', |
---|
| 1496 | 'label': 'Relative Path'}, |
---|
| 1497 | {'id': 'id_field', 'type': 'string', 'mode': 'w', |
---|
| 1498 | 'label': 'Field to build the id'}, |
---|
| 1499 | ) |
---|
| 1500 | path = "images" |
---|
[2119] | 1501 | storage_path = "%s/import/%s" % (i_home,path) |
---|
[2110] | 1502 | id_field = "reg_no" |
---|
[537] | 1503 | |
---|
[2110] | 1504 | def getImageInfo(self, datastructure): ###( |
---|
[2098] | 1505 | """Get the file info from the datastructure.""" |
---|
[2114] | 1506 | #import pdb; pdb.set_trace() |
---|
[2098] | 1507 | widget_id = self.getWidgetId() |
---|
[2120] | 1508 | if datastructure.has_key(widget_id): |
---|
| 1509 | fileupload = datastructure[widget_id] |
---|
| 1510 | dm = datastructure.getDataModel() |
---|
| 1511 | field_id = self.fields[0] |
---|
| 1512 | content_url = "/uniben/viewimage?path=%s/%s_%s.jpg" %\ |
---|
| 1513 | (self.path, |
---|
| 1514 | datastructure[self.id_field], |
---|
| 1515 | self.getWidgetId(), |
---|
| 1516 | ) |
---|
| 1517 | current_filename = "%s_%s.jpg" % (datastructure[self.id_field], |
---|
| 1518 | field_id,) |
---|
| 1519 | file_path = "/%s/%s" % (self.storage_path, |
---|
| 1520 | current_filename) |
---|
| 1521 | else: |
---|
| 1522 | file_path = "XXX" |
---|
[2114] | 1523 | # read the file from the filesystem |
---|
| 1524 | if not os.path.exists(file_path): |
---|
[2120] | 1525 | height = -1 |
---|
| 1526 | width = -1 |
---|
[2098] | 1527 | empty_file = True |
---|
| 1528 | session_file = False |
---|
| 1529 | current_filename = '' |
---|
[2120] | 1530 | content_url = '' |
---|
[2098] | 1531 | size = 0 |
---|
[2116] | 1532 | mimetype = '' |
---|
[2098] | 1533 | last_modified = '' |
---|
[2119] | 1534 | height = '' |
---|
| 1535 | width = '' |
---|
[2122] | 1536 | |
---|
[2098] | 1537 | else: |
---|
[2114] | 1538 | image = open(file_path) |
---|
[2142] | 1539 | from OFS.Image import getImageInfo as getImageInfoOFS |
---|
[2110] | 1540 | image.seek(0) |
---|
[2142] | 1541 | data = image.read(2000) |
---|
[2114] | 1542 | size = len(data) |
---|
| 1543 | empty_file = size == 0 |
---|
| 1544 | session_file = False |
---|
| 1545 | last_modified = '' |
---|
| 1546 | image.close() |
---|
[2142] | 1547 | mimetype, width, height = getImageInfoOFS(data) |
---|
[2119] | 1548 | |
---|
[2110] | 1549 | if width < 0: |
---|
| 1550 | width = None |
---|
| 1551 | if height < 0: |
---|
| 1552 | height = None |
---|
[2117] | 1553 | |
---|
[2110] | 1554 | if (self.allow_resize |
---|
| 1555 | and height is not None |
---|
| 1556 | and width is not None): |
---|
| 1557 | z_w = z_h = 1 |
---|
| 1558 | h = int(self.display_height) |
---|
| 1559 | w = int(self.display_width) |
---|
| 1560 | if w and h: |
---|
| 1561 | if w < width: |
---|
| 1562 | z_w = w / float(width) |
---|
| 1563 | if h < height: |
---|
| 1564 | z_h = h / float(height) |
---|
| 1565 | zoom = min(z_w, z_h) |
---|
| 1566 | width = int(zoom * width) |
---|
| 1567 | height = int(zoom * height) |
---|
[2117] | 1568 | #import pdb;pdb.set_trace() |
---|
[2114] | 1569 | image_info = { |
---|
| 1570 | 'empty_file': empty_file, |
---|
| 1571 | 'session_file': session_file, |
---|
| 1572 | 'current_filename': current_filename, |
---|
| 1573 | 'size': size, |
---|
| 1574 | 'last_modified': last_modified, |
---|
| 1575 | 'content_url': content_url, |
---|
| 1576 | 'mimetype': mimetype, |
---|
| 1577 | } |
---|
| 1578 | title = image_info['current_filename'] |
---|
| 1579 | alt = title or '' |
---|
[2117] | 1580 | #height = int(self.display_height) |
---|
| 1581 | #width = int(self.display_width) |
---|
[2114] | 1582 | if height is None or width is None: |
---|
| 1583 | tag = renderHtmlTag('img', src=image_info['content_url'], |
---|
| 1584 | alt=alt, title=title) |
---|
| 1585 | else: |
---|
| 1586 | tag = renderHtmlTag('img', src=image_info['content_url'], |
---|
| 1587 | width=str(width), height=str(height), |
---|
| 1588 | alt=alt, title=title) |
---|
[2110] | 1589 | |
---|
| 1590 | image_info['height'] = height |
---|
| 1591 | image_info['width'] = width |
---|
| 1592 | image_info['image_tag'] = tag |
---|
| 1593 | return image_info |
---|
[2098] | 1594 | ###) |
---|
| 1595 | |
---|
[2114] | 1596 | |
---|
| 1597 | def checkFileName(self, filename, mimetype): |
---|
| 1598 | return '', {} |
---|
| 1599 | if mimetype and mimetype.startswith('image'): |
---|
| 1600 | return '', {} |
---|
| 1601 | return 'cpsschemas_err_image', {} |
---|
| 1602 | |
---|
[2098] | 1603 | def prepare(self, datastructure, **kw): ###( |
---|
| 1604 | """Prepare datastructure from datamodel.""" |
---|
| 1605 | datamodel = datastructure.getDataModel() |
---|
| 1606 | widget_id = self.getWidgetId() |
---|
| 1607 | file_name = datamodel[self.fields[0]] |
---|
[2136] | 1608 | #import pdb; pdb.set_trace() |
---|
| 1609 | if self.allow_resize: |
---|
| 1610 | datastructure[self.getWidgetId() + '_resize'] = '' |
---|
[2110] | 1611 | datastructure[widget_id] = file_name |
---|
[2114] | 1612 | datastructure[widget_id + '_choice'] = 'change' |
---|
| 1613 | title = 'Passport Foto' |
---|
[2098] | 1614 | datastructure[widget_id + '_filename'] = title |
---|
| 1615 | ###) |
---|
| 1616 | |
---|
[2114] | 1617 | def validate(self, datastructure, **kw): ###( |
---|
[2098] | 1618 | """Update datamodel from user data in datastructure. |
---|
| 1619 | """ |
---|
[2122] | 1620 | logger = logging.getLogger('Widgets.NoZodbImageWidget.validate') |
---|
[2098] | 1621 | datamodel = datastructure.getDataModel() |
---|
| 1622 | field_id = self.fields[0] |
---|
| 1623 | widget_id = self.getWidgetId() |
---|
| 1624 | store = False |
---|
| 1625 | fileupload = None |
---|
| 1626 | mimetype = None |
---|
| 1627 | old_file = datamodel[field_id] |
---|
[2114] | 1628 | # if old_file is not None: |
---|
| 1629 | # old_filename = old_file.title |
---|
| 1630 | # else: |
---|
| 1631 | # old_filename = '' |
---|
| 1632 | choice = datastructure[widget_id+'_choice'] |
---|
| 1633 | fileupload = datastructure[widget_id] |
---|
| 1634 | is_upload = isinstance(fileupload, FileUpload) |
---|
[2136] | 1635 | #import pdb; pdb.set_trace() |
---|
| 1636 | if not is_upload and not datamodel[field_id]: |
---|
| 1637 | if self.is_required: |
---|
[2137] | 1638 | return self.validateError('Picture upload required', {}, |
---|
[2136] | 1639 | datastructure) |
---|
[2098] | 1640 | if choice == 'delete': |
---|
| 1641 | if self.is_required: |
---|
| 1642 | return self.validateError('cpsschemas_err_required', {}, |
---|
| 1643 | datastructure) |
---|
| 1644 | datamodel[field_id] = None |
---|
| 1645 | elif choice == 'keep': |
---|
| 1646 | fileupload = datastructure[widget_id] |
---|
| 1647 | if isinstance(fileupload, PersistableFileUpload): |
---|
| 1648 | # Keeping something from the session means we |
---|
| 1649 | # actually want to store it. |
---|
| 1650 | store = True |
---|
[2114] | 1651 | # else: |
---|
| 1652 | # # Nothing to change, don't pollute datastructure |
---|
| 1653 | # # with something costly already stored, which therefore |
---|
| 1654 | # # doesn't need to be kept in the session. |
---|
| 1655 | # self.unprepare(datastructure) |
---|
| 1656 | elif choice == 'change' and is_upload: |
---|
[2098] | 1657 | if not fileupload: |
---|
| 1658 | return self.validateError('cpsschemas_err_file_empty', {}, |
---|
| 1659 | datastructure) |
---|
| 1660 | if not isinstance(fileupload, FileUpload): |
---|
| 1661 | return self.validateError('cpsschemas_err_file', {}, |
---|
| 1662 | datastructure) |
---|
| 1663 | fileupload.seek(0, 2) # end of file |
---|
| 1664 | size = fileupload.tell() |
---|
| 1665 | if not size: |
---|
| 1666 | return self.validateError('cpsschemas_err_file_empty', {}, |
---|
| 1667 | datastructure) |
---|
| 1668 | if self.size_max and size > self.size_max: |
---|
| 1669 | max_size_str = self.getHumanReadableSize(self.size_max) |
---|
[2120] | 1670 | err = 'This file is too big, the allowed max size is ${max_size}' |
---|
[2122] | 1671 | logger.info('%s tried to upload picture with size %dk' %(datastructure['reg_no'],int(size)/1000) ) |
---|
[2098] | 1672 | err_mapping = {'max_size': max_size_str} |
---|
| 1673 | return self.validateError(err, err_mapping, datastructure) |
---|
| 1674 | store = True |
---|
| 1675 | |
---|
| 1676 | |
---|
| 1677 | # Find filename |
---|
[2136] | 1678 | if is_upload and store: |
---|
[2114] | 1679 | ext ='jpg' |
---|
| 1680 | filename = "%s_%s.%s" % (datastructure[self.id_field], |
---|
| 1681 | self.getWidgetId(), |
---|
| 1682 | ext) |
---|
[2136] | 1683 | datamodel[field_id] = filename |
---|
[2114] | 1684 | registry = getToolByName(self, 'mimetypes_registry') |
---|
| 1685 | mimetype = registry.lookupExtension(filename.lower()) |
---|
| 1686 | if mimetype is not None: |
---|
| 1687 | mimetype = str(mimetype) # normalize |
---|
| 1688 | # Set/update data |
---|
[2136] | 1689 | # file_path = "%s_%s.jpg" % (datamodel[self.id_field], |
---|
| 1690 | # field_id,) |
---|
| 1691 | full_path = "%s/%s" % (self.storage_path, filename) |
---|
| 1692 | #import pdb; pdb.set_trace() |
---|
[2110] | 1693 | file = self.makeFile(filename, fileupload, datastructure) |
---|
[2098] | 1694 | # Fixup mimetype |
---|
[2110] | 1695 | if mimetype and file.content_type != mimetype: |
---|
| 1696 | file.content_type = mimetype |
---|
| 1697 | # Store the file in the filesystem |
---|
[2114] | 1698 | if not os.path.exists(self.storage_path): |
---|
[2117] | 1699 | os.mkdir(self.storage_path) |
---|
[2114] | 1700 | #import pdb;pdb.set_trace() |
---|
| 1701 | pict = open(full_path,"w") |
---|
| 1702 | fileupload.seek(0) |
---|
| 1703 | pict.write(fileupload.read()) |
---|
| 1704 | pict.close() |
---|
[2098] | 1705 | |
---|
[2110] | 1706 | |
---|
[2098] | 1707 | return True |
---|
| 1708 | |
---|
[2114] | 1709 | ###) |
---|
[2098] | 1710 | |
---|
[2114] | 1711 | def render(self, mode, datastructure, **kw): ###( |
---|
| 1712 | render_method = 'widget_passport_render' |
---|
| 1713 | meth = getattr(self, render_method, None) |
---|
| 1714 | if meth is None: |
---|
| 1715 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 1716 | % (render_method, self.getId())) |
---|
| 1717 | img_info = self.getImageInfo(datastructure) |
---|
| 1718 | return meth(mode=mode, datastructure=datastructure, **img_info) |
---|
| 1719 | ###) |
---|
| 1720 | |
---|
[2110] | 1721 | InitializeClass(NoZodbImageWidget) |
---|
[2098] | 1722 | |
---|
[2110] | 1723 | widgetRegistry.register(NoZodbImageWidget) |
---|
[2098] | 1724 | ###) |
---|
| 1725 | |
---|
| 1726 | |
---|
[22] | 1727 | ########### |
---|
| 1728 | |
---|