[47] | 1 | #-*- mode: python; mode: fold -*- |
---|
[990] | 2 | # $Id: Widgets.py 1379 2007-01-30 23:05:07Z 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 |
---|
[537] | 8 | from Products.CPSSchemas.BasicWidgets import 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 |
---|
[22] | 16 | from re import compile |
---|
[952] | 17 | import logging |
---|
[22] | 18 | |
---|
[952] | 19 | #from zLOG import LOG, DEBUG |
---|
[22] | 20 | |
---|
[295] | 21 | class CPSSelectWidgetForRecord(CPSSelectWidget): ###( |
---|
| 22 | """Select widget. with record names""" |
---|
| 23 | meta_type = 'Select Widget for Records' |
---|
| 24 | |
---|
| 25 | field_types = ('CPS String Field',) |
---|
| 26 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 27 | |
---|
| 28 | _properties = CPSSelectWidget._properties + ( |
---|
| 29 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 30 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 31 | ) |
---|
| 32 | |
---|
| 33 | def render(self, mode, datastructure, **kw): |
---|
| 34 | """Render in mode from datastructure.""" |
---|
| 35 | value = datastructure[self.getWidgetId()] |
---|
| 36 | vocabulary = self._getVocabulary(datastructure) |
---|
| 37 | portal = getToolByName(self, 'portal_url').getPortalObject() |
---|
| 38 | cpsmcat = portal.translation_service |
---|
| 39 | if mode == 'view': |
---|
| 40 | if self.translated: |
---|
| 41 | return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore')) |
---|
| 42 | else: |
---|
| 43 | return escape(vocabulary.get(value, value)) |
---|
| 44 | elif mode == 'edit': |
---|
| 45 | html_widget_id = self.getHtmlWidgetId() |
---|
[444] | 46 | res = renderHtmlTag('select', |
---|
| 47 | name='%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 48 | id=html_widget_id) |
---|
| 49 | in_selection = 0 |
---|
| 50 | for k, v in vocabulary.items(): |
---|
| 51 | if self.translated: |
---|
| 52 | kw = {'value': k, |
---|
| 53 | 'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore') |
---|
| 54 | } |
---|
| 55 | else: |
---|
| 56 | kw = {'value': k, 'contents': v} |
---|
| 57 | if value == k: |
---|
| 58 | kw['selected'] = 'selected' |
---|
| 59 | in_selection = 1 |
---|
| 60 | res += renderHtmlTag('option', **kw) |
---|
| 61 | if value and not in_selection: |
---|
| 62 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 63 | 'selected': 'selected'} |
---|
| 64 | res += renderHtmlTag('option', **kw) |
---|
| 65 | res += '</select>' |
---|
| 66 | return res |
---|
| 67 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 68 | |
---|
| 69 | InitializeClass(CPSSelectWidgetForRecord) |
---|
| 70 | |
---|
| 71 | widgetRegistry.register(CPSSelectWidgetForRecord) |
---|
| 72 | |
---|
| 73 | ###) |
---|
| 74 | |
---|
[373] | 75 | class CPSStringWidgetForRecord(CPSStringWidget): ###( |
---|
[295] | 76 | """String widget.""" |
---|
| 77 | meta_type = 'String Widget For Record' |
---|
| 78 | |
---|
| 79 | field_types = ('CPS String Field',) |
---|
| 80 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 81 | _properties = CPSStringWidget._properties + ( |
---|
| 82 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 83 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 84 | ) |
---|
| 85 | |
---|
| 86 | def render(self, mode, datastructure, **kw): |
---|
| 87 | """Render in mode from datastructure.""" |
---|
| 88 | value = datastructure[self.getWidgetId()] |
---|
| 89 | if mode == 'view': |
---|
| 90 | return escape(value) |
---|
| 91 | elif mode == 'edit': |
---|
| 92 | # XXX TODO should use an other name than kw ! |
---|
| 93 | # XXX change this everywhere |
---|
| 94 | html_widget_id = self.getHtmlWidgetId() |
---|
| 95 | kw = {'type': 'text', |
---|
| 96 | 'id' : html_widget_id, |
---|
[444] | 97 | 'name': '%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 98 | 'value': escape(value), |
---|
| 99 | 'size': self.display_width, |
---|
| 100 | } |
---|
| 101 | if self.size_max: |
---|
| 102 | kw['maxlength'] = self.size_max |
---|
| 103 | return renderHtmlTag('input', **kw) |
---|
| 104 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 105 | |
---|
| 106 | InitializeClass(CPSStringWidgetForRecord) |
---|
| 107 | |
---|
| 108 | widgetRegistry.register(CPSStringWidgetForRecord) |
---|
| 109 | |
---|
[373] | 110 | ###) |
---|
| 111 | |
---|
| 112 | class CertificateCourseIdWidget(CPSStringWidget): ###( |
---|
| 113 | """ CertificateCourseId Widget""" |
---|
| 114 | meta_type = "CertificateCourseId Widget" |
---|
[444] | 115 | |
---|
[373] | 116 | def validate(self, datastructure, **kw): |
---|
| 117 | """Validate datastructure and update datamodel.""" |
---|
[444] | 118 | |
---|
[373] | 119 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 120 | if not valid: |
---|
| 121 | return 0 |
---|
| 122 | else: |
---|
| 123 | widget_id = self.getWidgetId() |
---|
| 124 | value = datastructure[widget_id].upper() |
---|
| 125 | err = 0 |
---|
| 126 | c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] |
---|
[381] | 127 | if hasattr(self.aq_parent,value): |
---|
[444] | 128 | err = 'Course %s already exists' % (value) |
---|
[381] | 129 | elif value not in c_ids: |
---|
[444] | 130 | err = 'Course %s does not exist' % (value) |
---|
[373] | 131 | if err: |
---|
| 132 | datastructure.setError(widget_id, err) |
---|
| 133 | else: |
---|
| 134 | datamodel = datastructure.getDataModel() |
---|
| 135 | datamodel[self.fields[0]] = value |
---|
[444] | 136 | |
---|
[373] | 137 | return not err |
---|
| 138 | |
---|
| 139 | InitializeClass(CertificateCourseIdWidget) |
---|
| 140 | |
---|
| 141 | widgetRegistry.register(CertificateCourseIdWidget) |
---|
[551] | 142 | ###) |
---|
[373] | 143 | |
---|
[551] | 144 | class CourseIdWidget(CPSStringWidget): ###( |
---|
| 145 | """ CourseId Widget""" |
---|
| 146 | meta_type = "CourseId Widget" |
---|
| 147 | |
---|
| 148 | def validate(self, datastructure, **kw): |
---|
| 149 | """Validate datastructure and update datamodel.""" |
---|
| 150 | |
---|
| 151 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 152 | if not valid: |
---|
| 153 | return 0 |
---|
| 154 | else: |
---|
| 155 | widget_id = self.getWidgetId() |
---|
| 156 | value = datastructure[widget_id].upper() |
---|
| 157 | err = 0 |
---|
| 158 | res = self.portal_catalog(meta_type= "Course",id = value) |
---|
| 159 | if len(res) > 0: |
---|
| 160 | err = 'Course %s already exists' % (value) |
---|
| 161 | if err: |
---|
| 162 | datastructure.setError(widget_id, err) |
---|
| 163 | else: |
---|
| 164 | datamodel = datastructure.getDataModel() |
---|
| 165 | datamodel[self.fields[0]] = value |
---|
| 166 | |
---|
| 167 | return not err |
---|
| 168 | |
---|
| 169 | InitializeClass(CourseIdWidget) |
---|
| 170 | |
---|
| 171 | widgetRegistry.register(CourseIdWidget) |
---|
| 172 | |
---|
| 173 | |
---|
[373] | 174 | ###) |
---|
| 175 | |
---|
[714] | 176 | class WAeUPReservedRoomsWidget(CPSStringWidget): ###( |
---|
| 177 | """ WAeUPReservedRooms Widget""" |
---|
| 178 | meta_type = "WAeUPReservedRooms Widget" |
---|
| 179 | |
---|
| 180 | def validate(self, datastructure, **kw): |
---|
| 181 | """Validate datastructure and update datamodel.""" |
---|
| 182 | import re |
---|
| 183 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 184 | if not valid: |
---|
| 185 | return 0 |
---|
| 186 | else: |
---|
| 187 | widget_id = self.getWidgetId() |
---|
| 188 | value = datastructure[widget_id] |
---|
| 189 | err = 0 |
---|
| 190 | try: |
---|
[926] | 191 | reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',value) |
---|
[714] | 192 | if r] |
---|
[1038] | 193 | except (ValueError,IndexError),msg: |
---|
[714] | 194 | err = str(msg) |
---|
| 195 | if err: |
---|
| 196 | datastructure.setError(widget_id, err) |
---|
| 197 | else: |
---|
| 198 | datamodel = datastructure.getDataModel() |
---|
| 199 | datamodel[self.fields[0]] = value |
---|
| 200 | return not err |
---|
| 201 | |
---|
| 202 | InitializeClass(WAeUPReservedRoomsWidget) |
---|
| 203 | |
---|
| 204 | widgetRegistry.register(WAeUPReservedRoomsWidget) |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | ###) |
---|
| 208 | |
---|
[388] | 209 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
| 210 | """ WAeUPId Widget""" |
---|
| 211 | meta_type = "WAeUPId Widget" |
---|
[444] | 212 | |
---|
[388] | 213 | def validate(self, datastructure, **kw): |
---|
| 214 | """Validate datastructure and update datamodel.""" |
---|
[444] | 215 | |
---|
[388] | 216 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[422] | 217 | id_pat_str = r"\S" |
---|
| 218 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
[388] | 219 | if not valid: |
---|
| 220 | return 0 |
---|
| 221 | else: |
---|
[586] | 222 | portal_type_query = {'query':['Faculty', |
---|
| 223 | 'Department', |
---|
| 224 | 'Course', |
---|
| 225 | 'Certificate', |
---|
| 226 | 'CertificateCourse',]} |
---|
[388] | 227 | widget_id = self.getWidgetId() |
---|
| 228 | value = datastructure[widget_id].upper() |
---|
| 229 | err = 0 |
---|
[440] | 230 | if len(value.split()) > 1: |
---|
[783] | 231 | err = 'Invalid Id, Id contains space(s).' |
---|
[586] | 232 | elif self.portal_catalog(portal_type=portal_type_query,id=value): |
---|
[783] | 233 | err = 'An object with the Id %s already exists in the Academic Section.' % (value) |
---|
[388] | 234 | if err: |
---|
| 235 | datastructure.setError(widget_id, err) |
---|
| 236 | else: |
---|
| 237 | datamodel = datastructure.getDataModel() |
---|
| 238 | datamodel[self.fields[0]] = value |
---|
[444] | 239 | |
---|
[388] | 240 | return not err |
---|
| 241 | |
---|
| 242 | InitializeClass(WAeUPIdWidget) |
---|
| 243 | |
---|
| 244 | widgetRegistry.register(WAeUPIdWidget) |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | ###) |
---|
| 248 | |
---|
[1025] | 249 | class StudyCourseWidget(CPSStringWidget): ###( |
---|
| 250 | """ StudyCourse Widget""" |
---|
| 251 | meta_type = "StudyCourse Widget" |
---|
| 252 | |
---|
| 253 | def validate(self, datastructure, **kw): |
---|
| 254 | """Validate datastructure and update datamodel.""" |
---|
| 255 | #from Products.zdb import set_trace |
---|
| 256 | #set_trace() |
---|
| 257 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 258 | id_pat_str = r"\S" |
---|
| 259 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
| 260 | if not valid: |
---|
| 261 | return 0 |
---|
| 262 | else: |
---|
| 263 | widget_id = self.getWidgetId() |
---|
| 264 | value = datastructure[widget_id].upper() |
---|
| 265 | err = 0 |
---|
| 266 | if len(value.split()) > 1: |
---|
| 267 | err = 'Invalid Id, Id contains space(s).' |
---|
| 268 | elif not self.portal_catalog(portal_type='Certificate',id=value): |
---|
| 269 | err = 'No such certificate' |
---|
| 270 | if err: |
---|
| 271 | datastructure.setError(widget_id, err) |
---|
| 272 | else: |
---|
| 273 | datamodel = datastructure.getDataModel() |
---|
| 274 | datamodel[self.fields[0]] = value |
---|
| 275 | return not err |
---|
| 276 | |
---|
| 277 | InitializeClass(StudyCourseWidget) |
---|
| 278 | |
---|
| 279 | widgetRegistry.register(StudyCourseWidget) |
---|
| 280 | ###) |
---|
| 281 | |
---|
[710] | 282 | ##class StudentIdWidget(CPSStringWidget): ###( |
---|
| 283 | ## """ StudentId Widget""" |
---|
| 284 | ## meta_type = "StudentId Widget" |
---|
| 285 | ## digits = 8 |
---|
| 286 | ## digits_str = "N"*digits |
---|
| 287 | ## letters = 2 |
---|
| 288 | ## letters_str = "L"*letters |
---|
| 289 | ## |
---|
| 290 | ## def validate(self, datastructure, **kw): |
---|
| 291 | ## """Validate datastructure and update datamodel.""" |
---|
| 292 | ## |
---|
| 293 | ## valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 294 | ## if not valid: |
---|
| 295 | ## return 0 |
---|
| 296 | ## else: |
---|
| 297 | ## widget_id = self.getWidgetId() |
---|
| 298 | ## value = datastructure[widget_id] |
---|
| 299 | ## err = 0 |
---|
| 300 | ## if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 301 | ## err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
| 302 | ## if err: |
---|
| 303 | ## datastructure.setError(widget_id, err) |
---|
| 304 | ## else: |
---|
| 305 | ## datamodel = datastructure.getDataModel() |
---|
| 306 | ## datamodel[self.fields[0]] = value |
---|
| 307 | ## |
---|
| 308 | ## return not err |
---|
| 309 | ## |
---|
| 310 | ##InitializeClass(StudentIdWidget) |
---|
| 311 | ##widgetRegistry.register(StudentIdWidget) |
---|
| 312 | ## |
---|
| 313 | #####) |
---|
[444] | 314 | |
---|
[463] | 315 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
| 316 | """ JambRegNo Widget""" |
---|
| 317 | meta_type = "JambRegNo Widget" |
---|
[1169] | 318 | _properties = CPSStringWidget._properties + ( |
---|
| 319 | {'id': 'catalog', 'type': 'string', 'mode': 'w', |
---|
| 320 | 'label': 'Catalog to search'}, |
---|
| 321 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 322 | 'label': 'Reference Field'}, |
---|
| 323 | ) |
---|
| 324 | catalog = "portal_pumeresults" #the catalog to search for jamb_reg_no |
---|
| 325 | reference = "" |
---|
[463] | 326 | digits = 8 |
---|
| 327 | digits_str = "N"*digits |
---|
| 328 | letters = 2 |
---|
| 329 | letters_str = "L"*letters |
---|
| 330 | def validate(self, datastructure, **kw): |
---|
| 331 | """Validate datastructure and update datamodel.""" |
---|
| 332 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[816] | 333 | #import pdb;pdb.set_trace() |
---|
[1169] | 334 | jamb_nr_catalog = getattr(self,self.catalog) |
---|
| 335 | widget_id = self.getWidgetId() |
---|
| 336 | value = datastructure[widget_id].upper() |
---|
| 337 | err = 0 |
---|
| 338 | if not valid: #or not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 339 | #err = 'Invalid number, the expected format is: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
| 340 | err = 'Invalid number' |
---|
| 341 | elif self.reference == '': |
---|
| 342 | #s = getStudentByRegNo(self,value) |
---|
| 343 | pume = jamb_nr_catalog(jamb_reg_no = value) |
---|
| 344 | if len(pume) < 1: |
---|
| 345 | err = 'No Student with JAMB RegNo %s.' % (value) |
---|
| 346 | else: |
---|
| 347 | datastructure['pume'] = pume[0] |
---|
[463] | 348 | else: |
---|
[1169] | 349 | #import pdb;pdb.set_trace() |
---|
| 350 | record = datastructure[self.reference] |
---|
| 351 | #jamb_reg_no = getattr(record,widget_id) |
---|
| 352 | jamb_reg_no = record.Entryregno |
---|
| 353 | if jamb_reg_no != value: |
---|
| 354 | err = 'Entry Registration Nr does not match.' |
---|
| 355 | if err: |
---|
| 356 | datastructure.setError(widget_id, err) |
---|
| 357 | else: |
---|
| 358 | datamodel = datastructure.getDataModel() |
---|
| 359 | datamodel[self.fields[0]] = value |
---|
| 360 | return not err |
---|
[463] | 361 | |
---|
| 362 | InitializeClass(JambRegNoWidget) |
---|
| 363 | |
---|
| 364 | widgetRegistry.register(JambRegNoWidget) |
---|
[47] | 365 | ###) |
---|
| 366 | |
---|
[1175] | 367 | class SecretWidget(CPSStringWidget): ###( |
---|
| 368 | """ Secret Widget""" |
---|
| 369 | meta_type = "Secret Widget" |
---|
| 370 | _properties = CPSStringWidget._properties + ( |
---|
| 371 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 372 | 'label': 'Reference Record'}, |
---|
| 373 | {'id': 'check_fields', 'type': 'tokens', 'mode': 'w', |
---|
| 374 | 'label': 'Fields to check'}, |
---|
| 375 | ) |
---|
| 376 | reference = "student" |
---|
| 377 | check_fields = ("Firstname", "Middlename","Lastname") |
---|
| 378 | def validate(self, datastructure, **kw): |
---|
| 379 | """Validate datastructure and update datamodel.""" |
---|
| 380 | logger = logging.getLogger('Secret.Validate') |
---|
| 381 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 382 | #import pdb;pdb.set_trace() |
---|
| 383 | widget_id = self.getWidgetId() |
---|
| 384 | value = datastructure[widget_id].upper() |
---|
| 385 | err = 0 |
---|
[1189] | 386 | record = datastructure.get(self.reference,None) |
---|
[1379] | 387 | if not valid or len(value) < 2: |
---|
[1175] | 388 | err = 'Invalid String' |
---|
[1189] | 389 | elif not record or datastructure.errors: |
---|
[1177] | 390 | err = 0 |
---|
[1175] | 391 | else: |
---|
| 392 | found = False |
---|
| 393 | cvs = [] |
---|
| 394 | for field in self.check_fields: |
---|
| 395 | cv = getattr(record,field).upper() |
---|
[1243] | 396 | if len(cv.split()) > 1: |
---|
| 397 | for splited in cv.split(): |
---|
| 398 | cvs.append(splited) |
---|
| 399 | else: |
---|
| 400 | cvs.append(cv) |
---|
[1175] | 401 | if cv == value.upper(): |
---|
| 402 | found = True |
---|
| 403 | break |
---|
| 404 | matric_no = record.matric_no |
---|
| 405 | name = " ".join(cvs) |
---|
| 406 | if not found: |
---|
| 407 | logger.info('"%(matric_no)s","mismatch", "%(value)s", "not in","%(name)s"' % vars()) |
---|
| 408 | err = 'No Name does match.' |
---|
| 409 | else: |
---|
| 410 | logger.info('"%(matric_no)s","found", "%(value)s", "in","%(name)s"' % vars()) |
---|
| 411 | if err: |
---|
| 412 | datastructure.setError(widget_id, err) |
---|
| 413 | else: |
---|
| 414 | datamodel = datastructure.getDataModel() |
---|
| 415 | datamodel[self.fields[0]] = value |
---|
| 416 | return not err |
---|
| 417 | |
---|
| 418 | InitializeClass(SecretWidget) |
---|
| 419 | |
---|
| 420 | widgetRegistry.register(SecretWidget) |
---|
| 421 | ###) |
---|
| 422 | |
---|
[1146] | 423 | class MatricNoWidget(CPSStringWidget): ###( |
---|
| 424 | """ MatricNo Widget""" |
---|
| 425 | meta_type = "MatricNo Widget" |
---|
| 426 | def validate(self, datastructure, **kw): |
---|
| 427 | """Validate datastructure and update datamodel.""" |
---|
| 428 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[1175] | 429 | logger = logging.getLogger('MatricNo.Validate') |
---|
[1146] | 430 | #import pdb;pdb.set_trace() |
---|
[1151] | 431 | returning = self.returning_import |
---|
[1146] | 432 | results = self.results_import |
---|
| 433 | err = 0 |
---|
[1189] | 434 | widget_id = self.getWidgetId() |
---|
| 435 | value = datastructure[widget_id] |
---|
| 436 | if not valid or not value: |
---|
[1188] | 437 | err = 'Invalid MatricNo String %s.' % value |
---|
[1189] | 438 | logger.info('"%s","invalid MatricNo String"' % value) |
---|
[1146] | 439 | else: |
---|
[1189] | 440 | value = value.upper() |
---|
[1177] | 441 | datastructure['student'] = None |
---|
[1146] | 442 | while not err: |
---|
[1151] | 443 | res = returning(matric_no = value) |
---|
[1146] | 444 | if len(res) < 1: |
---|
[1177] | 445 | logger.info('"%s","MatricNo not found."' % value) |
---|
[1146] | 446 | err = 'No Student with MatricNo %s.' % (value) |
---|
| 447 | continue |
---|
| 448 | datastructure['student'] = res[0] |
---|
| 449 | res = results(matric_no = value) |
---|
| 450 | if len(res) < 1: |
---|
| 451 | err = 'No Results with MatricNo %s.' % (value) |
---|
| 452 | continue |
---|
| 453 | datastructure['results'] = res |
---|
| 454 | break |
---|
[1189] | 455 | if err: |
---|
| 456 | datastructure.setError(widget_id, err) |
---|
| 457 | else: |
---|
| 458 | datamodel = datastructure.getDataModel() |
---|
| 459 | datamodel[self.fields[0]] = value |
---|
| 460 | return not err |
---|
[1146] | 461 | |
---|
| 462 | InitializeClass(MatricNoWidget) |
---|
| 463 | |
---|
| 464 | widgetRegistry.register(MatricNoWidget) |
---|
| 465 | ###) |
---|
| 466 | |
---|
| 467 | class WAeUPMultilineResultsWidget(CPSStringWidget): ###( |
---|
| 468 | """ WAeUPMultilineResults Widget""" |
---|
| 469 | meta_type = "WAeUp Multiline Results Widget" |
---|
| 470 | _properties = CPSWidget._properties + ( |
---|
| 471 | {'id': 'nr_of_lines', 'type': 'int', 'mode': 'w', |
---|
| 472 | 'label': 'Nr of Lines'}, |
---|
| 473 | ) |
---|
| 474 | nr_of_lines = 5 |
---|
| 475 | def prepare(self, datastructure, **kw): ###( |
---|
| 476 | """Prepare datastructure from datamodel.""" |
---|
| 477 | datamodel = datastructure.getDataModel() |
---|
| 478 | #import pdb;pdb.set_trace() |
---|
| 479 | widget_id = self.getWidgetId() |
---|
| 480 | v = datamodel[self.fields[0]] |
---|
| 481 | if type(v) is ListType and v: |
---|
| 482 | nr_results = len(v) |
---|
| 483 | else: |
---|
| 484 | v = [] |
---|
| 485 | nr_results = 0 |
---|
| 486 | count = 1 |
---|
| 487 | for s,g in v: |
---|
| 488 | wid = "%s%02d"% (widget_id,count) |
---|
| 489 | datastructure[wid+'_s'] = s |
---|
| 490 | datastructure[wid+'_g'] = g |
---|
| 491 | count += 1 |
---|
| 492 | if nr_results < self.nr_of_lines: |
---|
| 493 | for line in range(nr_results,self.nr_of_lines): |
---|
| 494 | v.append(('','')) |
---|
| 495 | wid = "%s%02d"% (widget_id,line) |
---|
| 496 | datastructure[wid+'_s'] = '' |
---|
| 497 | datastructure[wid+'_g'] = '' |
---|
| 498 | datastructure[widget_id] = v |
---|
| 499 | datastructure[widget_id+'_s'] = '' |
---|
| 500 | datastructure[widget_id+'_g'] = '' |
---|
| 501 | ###) |
---|
| 502 | |
---|
| 503 | def validate(self, datastructure, **kw): ###( |
---|
| 504 | """Validate datastructure and update datamodel.""" |
---|
| 505 | #import pdb;pdb.set_trace() |
---|
| 506 | widget_id = self.getWidgetId() |
---|
| 507 | err = 0 |
---|
| 508 | lines = [] |
---|
| 509 | for line in range(1,30): |
---|
| 510 | wid = "%s%02d"% (widget_id,line) |
---|
| 511 | if not datastructure.has_key(wid+'_s'): |
---|
| 512 | break |
---|
| 513 | lines.append((datastructure[wid+'_s'].strip(), |
---|
| 514 | datastructure[wid+'_g'].strip())) |
---|
[1155] | 515 | |
---|
[1146] | 516 | s = datastructure[widget_id+'_s'].strip() |
---|
| 517 | g = datastructure[widget_id+'_g'].strip() |
---|
| 518 | if s and g: |
---|
| 519 | lines.append((s,g)) |
---|
| 520 | active = [] |
---|
| 521 | for s,g in lines: |
---|
| 522 | if g != "": |
---|
| 523 | active.append((s,g)) |
---|
| 524 | if err: |
---|
| 525 | datastructure.setError(widget_id, err) |
---|
| 526 | else: |
---|
| 527 | datamodel = datastructure.getDataModel() |
---|
| 528 | datamodel[self.fields[0]] = active |
---|
| 529 | return not err |
---|
| 530 | ###) |
---|
| 531 | |
---|
| 532 | def render(self, mode, datastructure, **kw): ###( |
---|
| 533 | """Render in mode from datastructure.""" |
---|
| 534 | render_method = 'widget_waeup_multiline_result_render' |
---|
| 535 | meth = getattr(self, render_method, None) |
---|
| 536 | if meth is None: |
---|
| 537 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 538 | % (render_method, self.getId())) |
---|
| 539 | #import pdb;pdb.set_trace() |
---|
| 540 | datamodel = datastructure.getDataModel() |
---|
| 541 | widget_id = self.getWidgetId() |
---|
| 542 | lines = datamodel[self.fields[0]] |
---|
| 543 | if len(lines) < self.nr_of_lines: |
---|
| 544 | for line in range(len(lines),self.nr_of_lines + 1): |
---|
| 545 | lines.append(('','')) |
---|
| 546 | datastructure[widget_id] = lines |
---|
| 547 | datastructure[widget_id+'_s'] = '' |
---|
| 548 | datastructure[widget_id+'_g'] = '' |
---|
| 549 | ## count = 1 |
---|
| 550 | ## for s,g in v: |
---|
| 551 | ## wid = "%s%02d"% (widget_id,count) |
---|
| 552 | ## count += 1 |
---|
| 553 | return meth(mode=mode, |
---|
| 554 | datastructure=datastructure, |
---|
| 555 | ) |
---|
| 556 | ###) |
---|
| 557 | |
---|
| 558 | |
---|
| 559 | InitializeClass(WAeUPMultilineResultsWidget) |
---|
| 560 | widgetRegistry.register(WAeUPMultilineResultsWidget) |
---|
| 561 | ###) |
---|
| 562 | |
---|
[794] | 563 | class WAeUPResultsWidget(CPSStringWidget): ###( |
---|
| 564 | """ WAeUPResults Widget""" |
---|
| 565 | meta_type = "WAeUp Results Widget" |
---|
| 566 | |
---|
| 567 | def prepare(self, datastructure, **kw): ###( |
---|
| 568 | """Prepare datastructure from datamodel.""" |
---|
| 569 | datamodel = datastructure.getDataModel() |
---|
| 570 | v = datamodel[self.fields[0]] |
---|
| 571 | #import pdb;pdb.set_trace() |
---|
| 572 | widget_id = self.getWidgetId() |
---|
[807] | 573 | datastructure[widget_id] = v |
---|
[794] | 574 | datastructure[widget_id+'_s'] = '' |
---|
| 575 | datastructure[widget_id+'_g'] = '' |
---|
| 576 | ###) |
---|
| 577 | |
---|
| 578 | def validate(self, datastructure, **kw): ###( |
---|
| 579 | """Validate datastructure and update datamodel.""" |
---|
[807] | 580 | #import pdb;pdb.set_trace() |
---|
[794] | 581 | widget_id = self.getWidgetId() |
---|
| 582 | v = datastructure[widget_id] |
---|
| 583 | err = 0 |
---|
| 584 | s = datastructure[widget_id+'_s'].strip() |
---|
| 585 | g = datastructure[widget_id+'_g'].strip() |
---|
[807] | 586 | while 1: |
---|
| 587 | if not s and g: |
---|
| 588 | err = "no subject grad for subject %s " % s |
---|
| 589 | break |
---|
| 590 | i = 0 |
---|
| 591 | done = False |
---|
| 592 | for sv,gv in v: |
---|
| 593 | if sv == s: |
---|
| 594 | done = True |
---|
| 595 | if not g: |
---|
| 596 | v.pop(i) |
---|
| 597 | break |
---|
| 598 | v[i] = (s,g) |
---|
| 599 | break |
---|
| 600 | i += 1 |
---|
| 601 | if done: |
---|
| 602 | break |
---|
| 603 | if s and g: |
---|
| 604 | v.append((s,g)) |
---|
| 605 | break |
---|
[794] | 606 | if err: |
---|
| 607 | datastructure.setError(widget_id, err) |
---|
| 608 | else: |
---|
| 609 | datamodel = datastructure.getDataModel() |
---|
| 610 | datamodel[self.fields[0]] = v |
---|
| 611 | datastructure[widget_id+'_s'] = s |
---|
| 612 | datastructure[widget_id+'_g'] = g |
---|
| 613 | return not err |
---|
| 614 | ###) |
---|
| 615 | |
---|
| 616 | def render(self, mode, datastructure, **kw): ###( |
---|
| 617 | """Render in mode from datastructure.""" |
---|
| 618 | render_method = 'widget_waeup_result_render' |
---|
| 619 | meth = getattr(self, render_method, None) |
---|
| 620 | if meth is None: |
---|
| 621 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 622 | % (render_method, self.getId())) |
---|
| 623 | #import pdb;pdb.set_trace() |
---|
| 624 | datamodel = datastructure.getDataModel() |
---|
| 625 | widget_id = self.getWidgetId() |
---|
| 626 | datastructure[widget_id+'_s'] = '' |
---|
| 627 | datastructure[widget_id+'_g'] = '' |
---|
| 628 | return meth(mode=mode, |
---|
| 629 | datastructure=datastructure, |
---|
| 630 | ) |
---|
| 631 | ###) |
---|
| 632 | |
---|
| 633 | |
---|
| 634 | InitializeClass(WAeUPResultsWidget) |
---|
| 635 | widgetRegistry.register(WAeUPResultsWidget) |
---|
| 636 | ###) |
---|
| 637 | |
---|
[488] | 638 | class ScratchCardPin: ###( |
---|
| 639 | """the ScratchCardPin""" |
---|
| 640 | def __init__(self,prefix,batch_no,number): |
---|
[990] | 641 | if not batch_no and not number: |
---|
| 642 | s = prefix |
---|
[996] | 643 | if len(s) > 3: |
---|
[990] | 644 | prefix,batch_no,number = s[:3],s[3:-10],s[-10:] |
---|
| 645 | else: |
---|
[996] | 646 | prefix,batch_no,number = s,'','' |
---|
[488] | 647 | self.p = prefix |
---|
| 648 | self.b = batch_no |
---|
| 649 | self.n = number |
---|
| 650 | |
---|
| 651 | def __str__(self): |
---|
| 652 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
[1146] | 653 | |
---|
| 654 | def __repr__(self): |
---|
| 655 | return "%s%s%s" % (self.p,self.b,self.n) |
---|
[488] | 656 | ###) |
---|
| 657 | |
---|
[47] | 658 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
[22] | 659 | """ ScratchcardPin Widget""" |
---|
[199] | 660 | meta_type = "Scratchcard Pin Widget" |
---|
[488] | 661 | _properties = CPSWidget._properties + ( |
---|
| 662 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
| 663 | 'label': 'Prefix'}, |
---|
| 664 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 665 | 'label': 'Reference Field'}, |
---|
| 666 | ) |
---|
| 667 | prefix = '' |
---|
| 668 | reference = '' |
---|
[502] | 669 | def prepare(self, datastructure, **kw): ###( |
---|
[488] | 670 | """Prepare datastructure from datamodel.""" |
---|
| 671 | datamodel = datastructure.getDataModel() |
---|
| 672 | v = datamodel[self.fields[0]] |
---|
| 673 | widget_id = self.getWidgetId() |
---|
[742] | 674 | #import pdb;pdb.set_trace() |
---|
[747] | 675 | if v and type(v) is StringType: |
---|
[990] | 676 | try: |
---|
| 677 | p,b,n = v.split('-') |
---|
| 678 | v = ScratchCardPin(p,b,n) |
---|
| 679 | except ValueError: |
---|
| 680 | v = ScratchCardPin(v,'','') |
---|
[488] | 681 | if v: |
---|
[742] | 682 | p = '%s' % v.p |
---|
[488] | 683 | b = '%s' % v.b |
---|
| 684 | n = '%s' % v.n |
---|
| 685 | else: |
---|
[742] | 686 | p = self.prefix |
---|
| 687 | if p.startswith('@'): |
---|
| 688 | p = getattr(self,self.prefix[1:])() |
---|
[488] | 689 | b = n = '' |
---|
[742] | 690 | v = ScratchCardPin(p,b,n) |
---|
[488] | 691 | datastructure[widget_id] = v |
---|
[742] | 692 | datastructure[widget_id+'_p'] = p |
---|
[488] | 693 | datastructure[widget_id+'_b'] = b |
---|
| 694 | datastructure[widget_id+'_n'] = n |
---|
[1376] | 695 | ###) |
---|
[758] | 696 | |
---|
[1169] | 697 | def validate(self, datastructure, **kw): ###( |
---|
[22] | 698 | """Validate datastructure and update datamodel.""" |
---|
[1082] | 699 | s_logger = logging.getLogger('Pin.Validate') |
---|
[488] | 700 | widget_id = self.getWidgetId() |
---|
| 701 | v = datastructure[widget_id] |
---|
[996] | 702 | #import pdb;pdb.set_trace() |
---|
[488] | 703 | err = 0 |
---|
[742] | 704 | prefix= self.prefix |
---|
| 705 | if prefix.startswith('@'): |
---|
| 706 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 707 | b = datastructure[widget_id+'_b'].strip() |
---|
| 708 | n = datastructure[widget_id+'_n'].strip() |
---|
[502] | 709 | pins = self.portal_pins |
---|
[742] | 710 | pin = "%(prefix)s%(b)s%(n)s" % vars() |
---|
[1376] | 711 | pin_str = "%(prefix)s-%(b)-s%(n)s" % vars() |
---|
[816] | 712 | do = 1 |
---|
[1025] | 713 | s_id = str(self.portal_membership.getAuthenticatedMember()) |
---|
[1326] | 714 | #import pdb;pdb.set_trace() |
---|
[1343] | 715 | if self.isStaff(): |
---|
[1326] | 716 | do = 0 |
---|
| 717 | err ='You are not a Student. PIN neither checked nor used.' |
---|
[1376] | 718 | s_logger.info('"%s","tried to use Scratchcard", "%s"' % (s_id,pin_str)) |
---|
| 719 | elif len(n) != 10: |
---|
| 720 | do = 0 |
---|
| 721 | err = 'Invalid PIN-Number length %d' % len(n) |
---|
| 722 | s_logger.info('"%s","Invalid PIN-Number length", "%d"' % (s_id,len(n))) |
---|
[1326] | 723 | elif self.reference == "": |
---|
[1030] | 724 | ref = s_id |
---|
[635] | 725 | else: |
---|
| 726 | ref = datastructure[self.reference] |
---|
[843] | 727 | if datastructure.errors: |
---|
[816] | 728 | do = 0 |
---|
[977] | 729 | datastructure.setError(widget_id, 'Errors in other data, PIN neither checked nor used.') |
---|
[1376] | 730 | s_logger.info('"%s", "%s","entered wrong data together with pin", "%s"' % (s_id,ref,pin_str)) |
---|
[816] | 731 | while do: |
---|
| 732 | ok = pins.searchAndSetRecord(pin,ref,prefix) |
---|
[1030] | 733 | if ok < -2 or ok > 2: |
---|
| 734 | err = 'Unknown error, please report!' |
---|
[1376] | 735 | s_logger.info('"%s","%s", "caused unknown error with pin", "%s"' % (s_id,ref,pin_str)) |
---|
[1030] | 736 | break |
---|
| 737 | elif ok == -2: |
---|
[819] | 738 | err = 'Service already activated but with a different PIN.' |
---|
[1376] | 739 | s_logger.info('"%s","%s", "repeatedly activated service but with different pin", "%s"' % (s_id,ref,pin_str)) |
---|
[710] | 740 | break |
---|
| 741 | elif ok == -1: |
---|
[783] | 742 | err = 'Invalid PIN' |
---|
[1376] | 743 | s_logger.info('"%s","%s", "entered invalid pin", "%s"' % (s_id,ref,pin_str)) |
---|
[502] | 744 | break |
---|
| 745 | if ok == 0: |
---|
[796] | 746 | err = 'PIN already used.' |
---|
[1376] | 747 | s_logger.info('"%s","%s", "entered used pin", "%s"' % (s_id,ref,pin_str)) |
---|
[502] | 748 | break |
---|
| 749 | if ok >= 1: |
---|
[710] | 750 | #import pdb;pdb.set_trace() |
---|
[635] | 751 | if self.isStudent(): |
---|
[992] | 752 | if self.reference == "jamb_reg_no": |
---|
[997] | 753 | err = "You are already logged in." |
---|
[1376] | 754 | s_logger.info('"%s","%s", "checked admission though logged in", "%s"' % (s_id,ref,pin_str)) |
---|
[992] | 755 | break |
---|
[1082] | 756 | if ok == 1: |
---|
[1376] | 757 | s_logger.info('"%s","%s", "successfully used pin", "%s"' % (s_id,ref,pin_str)) |
---|
[1032] | 758 | else: |
---|
[1376] | 759 | s_logger.info('"%s","%s", "repeatedly used pin", "%s"' % (s_id,ref,pin_str)) |
---|
[637] | 760 | break |
---|
[635] | 761 | else: |
---|
| 762 | student = getStudentByRegNo(self,ref) |
---|
[1376] | 763 | s_logger.info('"%s","%s","successfully used pin","%s"' % (s_id,ref,pin_str)) |
---|
[502] | 764 | if student is None: |
---|
[796] | 765 | err = "Student not found." |
---|
[1032] | 766 | s_logger.info('"%s","not found in admission list"' % (ref)) |
---|
[502] | 767 | break |
---|
[648] | 768 | s_id = student.getId() |
---|
[502] | 769 | if ok == 2: |
---|
[990] | 770 | if self.reference == "jamb_reg_no": |
---|
| 771 | if hasattr(self.portal_directories.students,s_id): |
---|
| 772 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
[1376] | 773 | s_logger.info('"%s","%s", "repeatedly checked admission with pin", "%s"' % (s_id,ref,pin_str)) |
---|
[990] | 774 | break |
---|
| 775 | else: |
---|
[1376] | 776 | s_logger.info('"%s","%s","(non-member) repeatedly checked admission with pin", "%s"' % (s_id,ref,pin_str)) |
---|
[990] | 777 | else: |
---|
[1030] | 778 | err = "Unknown error" % s_id |
---|
[1376] | 779 | s_logger.info('"%s","%s", "repeatedly activated service with pin", "%s"' % (s_id,ref,pin_str)) |
---|
[986] | 780 | break |
---|
| 781 | try: |
---|
| 782 | student.getContent().makeStudentMember(s_id,password=pin[4:]) |
---|
[1376] | 783 | s_logger.info('"%s","%s", "has been created using pin", "%s"' % (s_id,ref,pin_str)) |
---|
[986] | 784 | except: |
---|
[989] | 785 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
[1376] | 786 | s_logger.info('"%s","%s", "could not be made a member with pin", "%s"' % (s_id,ref,pin_str)) |
---|
[986] | 787 | break |
---|
[502] | 788 | break |
---|
[488] | 789 | if err: |
---|
| 790 | datastructure.setError(widget_id, err) |
---|
| 791 | else: |
---|
| 792 | datamodel = datastructure.getDataModel() |
---|
[742] | 793 | datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n) |
---|
| 794 | datastructure[widget_id] = ScratchCardPin(prefix,b,n) |
---|
| 795 | datastructure[widget_id+'_p'] = prefix |
---|
[488] | 796 | datastructure[widget_id+'_b'] = b |
---|
| 797 | datastructure[widget_id+'_n'] = n |
---|
[502] | 798 | datastructure['s_id'] = s_id |
---|
[488] | 799 | return not err |
---|
[444] | 800 | |
---|
[1169] | 801 | ###) |
---|
| 802 | |
---|
[502] | 803 | def render(self, mode, datastructure, **kw): ###( |
---|
[488] | 804 | """Render in mode from datastructure.""" |
---|
| 805 | render_method = 'widget_scratch_card_pin_render' |
---|
| 806 | meth = getattr(self, render_method, None) |
---|
| 807 | if meth is None: |
---|
| 808 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 809 | % (render_method, self.getId())) |
---|
| 810 | |
---|
| 811 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
| 812 | # or create mode, because a default value has to be provided. |
---|
| 813 | #import pdb;pdb.set_trace() |
---|
| 814 | datamodel = datastructure.getDataModel() |
---|
| 815 | v = datamodel[self.fields[0]] |
---|
[502] | 816 | if v and type(v) is StringType: |
---|
[990] | 817 | try: |
---|
| 818 | p,b,n = v.split('-') |
---|
| 819 | v = ScratchCardPin(p,b,n) |
---|
| 820 | except ValueError: |
---|
[996] | 821 | v = ScratchCardPin(self.prefix,'1',v) |
---|
| 822 | pass |
---|
[488] | 823 | if v: |
---|
[742] | 824 | prefix= '%s' % v.p |
---|
[488] | 825 | b = '%s' % v.b |
---|
| 826 | n = '%s' % v.n |
---|
[22] | 827 | else: |
---|
[742] | 828 | prefix= self.prefix |
---|
| 829 | if prefix.startswith('@'): |
---|
| 830 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 831 | b = n = '' |
---|
[742] | 832 | v = ScratchCardPin(prefix,b,n) |
---|
| 833 | widget_id = self.getWidgetId() |
---|
| 834 | datastructure[widget_id] = v |
---|
| 835 | datastructure[widget_id+'_p'] = prefix |
---|
| 836 | datastructure[widget_id+'_b'] = b |
---|
| 837 | datastructure[widget_id+'_n'] = n |
---|
[758] | 838 | return meth(mode=mode, |
---|
| 839 | datastructure=datastructure, |
---|
[488] | 840 | ) |
---|
[523] | 841 | ###) |
---|
[488] | 842 | |
---|
| 843 | |
---|
[22] | 844 | InitializeClass(ScratchcardPinWidget) |
---|
[199] | 845 | widgetRegistry.register(ScratchcardPinWidget) |
---|
[22] | 846 | |
---|
[47] | 847 | |
---|
| 848 | ###) |
---|
| 849 | |
---|
[1169] | 850 | class WAeUPImageWidget(CPSImageWidget): ###( |
---|
[537] | 851 | """Photo widget.""" |
---|
| 852 | meta_type = 'WAeUP Image Widget' |
---|
| 853 | |
---|
| 854 | def render(self, mode, datastructure, **kw): |
---|
| 855 | render_method = 'widget_waeup_image_render' |
---|
| 856 | meth = getattr(self, render_method, None) |
---|
| 857 | if meth is None: |
---|
| 858 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 859 | % (render_method, self.getId())) |
---|
| 860 | img_info = self.getImageInfo(datastructure) |
---|
| 861 | return meth(mode=mode, datastructure=datastructure, **img_info) |
---|
| 862 | |
---|
[1169] | 863 | ###) |
---|
[537] | 864 | |
---|
| 865 | |
---|
[1169] | 866 | |
---|
[537] | 867 | InitializeClass(WAeUPImageWidget) |
---|
| 868 | |
---|
| 869 | widgetRegistry.register(WAeUPImageWidget) |
---|
| 870 | |
---|
| 871 | |
---|
[22] | 872 | ########### |
---|
| 873 | |
---|