[47] | 1 | #-*- mode: python; mode: fold -*- |
---|
[990] | 2 | # $Id: Widgets.py 1169 2007-01-01 16:38:46Z joachim $ |
---|
[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 | |
---|
| 144 | |
---|
[551] | 145 | class CourseIdWidget(CPSStringWidget): ###( |
---|
| 146 | """ CourseId Widget""" |
---|
| 147 | meta_type = "CourseId Widget" |
---|
| 148 | |
---|
| 149 | def validate(self, datastructure, **kw): |
---|
| 150 | """Validate datastructure and update datamodel.""" |
---|
| 151 | |
---|
| 152 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 153 | if not valid: |
---|
| 154 | return 0 |
---|
| 155 | else: |
---|
| 156 | widget_id = self.getWidgetId() |
---|
| 157 | value = datastructure[widget_id].upper() |
---|
| 158 | err = 0 |
---|
| 159 | res = self.portal_catalog(meta_type= "Course",id = value) |
---|
| 160 | if len(res) > 0: |
---|
| 161 | err = 'Course %s already exists' % (value) |
---|
| 162 | if err: |
---|
| 163 | datastructure.setError(widget_id, err) |
---|
| 164 | else: |
---|
| 165 | datamodel = datastructure.getDataModel() |
---|
| 166 | datamodel[self.fields[0]] = value |
---|
| 167 | |
---|
| 168 | return not err |
---|
| 169 | |
---|
| 170 | InitializeClass(CourseIdWidget) |
---|
| 171 | |
---|
| 172 | widgetRegistry.register(CourseIdWidget) |
---|
| 173 | |
---|
| 174 | |
---|
[373] | 175 | ###) |
---|
| 176 | |
---|
[714] | 177 | class WAeUPReservedRoomsWidget(CPSStringWidget): ###( |
---|
| 178 | """ WAeUPReservedRooms Widget""" |
---|
| 179 | meta_type = "WAeUPReservedRooms Widget" |
---|
| 180 | |
---|
| 181 | def validate(self, datastructure, **kw): |
---|
| 182 | """Validate datastructure and update datamodel.""" |
---|
| 183 | import re |
---|
| 184 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 185 | if not valid: |
---|
| 186 | return 0 |
---|
| 187 | else: |
---|
| 188 | widget_id = self.getWidgetId() |
---|
| 189 | value = datastructure[widget_id] |
---|
| 190 | err = 0 |
---|
| 191 | try: |
---|
[926] | 192 | reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',value) |
---|
[714] | 193 | if r] |
---|
[1038] | 194 | except (ValueError,IndexError),msg: |
---|
[714] | 195 | err = str(msg) |
---|
| 196 | if err: |
---|
| 197 | datastructure.setError(widget_id, err) |
---|
| 198 | else: |
---|
| 199 | datamodel = datastructure.getDataModel() |
---|
| 200 | datamodel[self.fields[0]] = value |
---|
| 201 | return not err |
---|
| 202 | |
---|
| 203 | InitializeClass(WAeUPReservedRoomsWidget) |
---|
| 204 | |
---|
| 205 | widgetRegistry.register(WAeUPReservedRoomsWidget) |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | ###) |
---|
| 209 | |
---|
[388] | 210 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
| 211 | """ WAeUPId Widget""" |
---|
| 212 | meta_type = "WAeUPId Widget" |
---|
[444] | 213 | |
---|
[388] | 214 | def validate(self, datastructure, **kw): |
---|
| 215 | """Validate datastructure and update datamodel.""" |
---|
[444] | 216 | |
---|
[388] | 217 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[422] | 218 | id_pat_str = r"\S" |
---|
| 219 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
[388] | 220 | if not valid: |
---|
| 221 | return 0 |
---|
| 222 | else: |
---|
[586] | 223 | portal_type_query = {'query':['Faculty', |
---|
| 224 | 'Department', |
---|
| 225 | 'Course', |
---|
| 226 | 'Certificate', |
---|
| 227 | 'CertificateCourse',]} |
---|
[388] | 228 | widget_id = self.getWidgetId() |
---|
| 229 | value = datastructure[widget_id].upper() |
---|
| 230 | err = 0 |
---|
[440] | 231 | if len(value.split()) > 1: |
---|
[783] | 232 | err = 'Invalid Id, Id contains space(s).' |
---|
[586] | 233 | elif self.portal_catalog(portal_type=portal_type_query,id=value): |
---|
[783] | 234 | err = 'An object with the Id %s already exists in the Academic Section.' % (value) |
---|
[388] | 235 | if err: |
---|
| 236 | datastructure.setError(widget_id, err) |
---|
| 237 | else: |
---|
| 238 | datamodel = datastructure.getDataModel() |
---|
| 239 | datamodel[self.fields[0]] = value |
---|
[444] | 240 | |
---|
[388] | 241 | return not err |
---|
| 242 | |
---|
| 243 | InitializeClass(WAeUPIdWidget) |
---|
| 244 | |
---|
| 245 | widgetRegistry.register(WAeUPIdWidget) |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | ###) |
---|
| 249 | |
---|
[1025] | 250 | class StudyCourseWidget(CPSStringWidget): ###( |
---|
| 251 | """ StudyCourse Widget""" |
---|
| 252 | meta_type = "StudyCourse Widget" |
---|
| 253 | |
---|
| 254 | def validate(self, datastructure, **kw): |
---|
| 255 | """Validate datastructure and update datamodel.""" |
---|
| 256 | #from Products.zdb import set_trace |
---|
| 257 | #set_trace() |
---|
| 258 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 259 | id_pat_str = r"\S" |
---|
| 260 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
| 261 | if not valid: |
---|
| 262 | return 0 |
---|
| 263 | else: |
---|
| 264 | widget_id = self.getWidgetId() |
---|
| 265 | value = datastructure[widget_id].upper() |
---|
| 266 | err = 0 |
---|
| 267 | if len(value.split()) > 1: |
---|
| 268 | err = 'Invalid Id, Id contains space(s).' |
---|
| 269 | elif not self.portal_catalog(portal_type='Certificate',id=value): |
---|
| 270 | err = 'No such certificate' |
---|
| 271 | if err: |
---|
| 272 | datastructure.setError(widget_id, err) |
---|
| 273 | else: |
---|
| 274 | datamodel = datastructure.getDataModel() |
---|
| 275 | datamodel[self.fields[0]] = value |
---|
| 276 | return not err |
---|
| 277 | |
---|
| 278 | InitializeClass(StudyCourseWidget) |
---|
| 279 | |
---|
| 280 | widgetRegistry.register(StudyCourseWidget) |
---|
| 281 | ###) |
---|
| 282 | |
---|
[710] | 283 | ##class StudentIdWidget(CPSStringWidget): ###( |
---|
| 284 | ## """ StudentId Widget""" |
---|
| 285 | ## meta_type = "StudentId Widget" |
---|
| 286 | ## digits = 8 |
---|
| 287 | ## digits_str = "N"*digits |
---|
| 288 | ## letters = 2 |
---|
| 289 | ## letters_str = "L"*letters |
---|
| 290 | ## |
---|
| 291 | ## def validate(self, datastructure, **kw): |
---|
| 292 | ## """Validate datastructure and update datamodel.""" |
---|
| 293 | ## |
---|
| 294 | ## valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 295 | ## if not valid: |
---|
| 296 | ## return 0 |
---|
| 297 | ## else: |
---|
| 298 | ## widget_id = self.getWidgetId() |
---|
| 299 | ## value = datastructure[widget_id] |
---|
| 300 | ## err = 0 |
---|
| 301 | ## if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 302 | ## err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
| 303 | ## if err: |
---|
| 304 | ## datastructure.setError(widget_id, err) |
---|
| 305 | ## else: |
---|
| 306 | ## datamodel = datastructure.getDataModel() |
---|
| 307 | ## datamodel[self.fields[0]] = value |
---|
| 308 | ## |
---|
| 309 | ## return not err |
---|
| 310 | ## |
---|
| 311 | ##InitializeClass(StudentIdWidget) |
---|
| 312 | ##widgetRegistry.register(StudentIdWidget) |
---|
| 313 | ## |
---|
| 314 | #####) |
---|
[444] | 315 | |
---|
[463] | 316 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
| 317 | """ JambRegNo Widget""" |
---|
| 318 | meta_type = "JambRegNo Widget" |
---|
[1169] | 319 | _properties = CPSStringWidget._properties + ( |
---|
| 320 | {'id': 'catalog', 'type': 'string', 'mode': 'w', |
---|
| 321 | 'label': 'Catalog to search'}, |
---|
| 322 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 323 | 'label': 'Reference Field'}, |
---|
| 324 | ) |
---|
| 325 | catalog = "portal_pumeresults" #the catalog to search for jamb_reg_no |
---|
| 326 | reference = "" |
---|
[463] | 327 | digits = 8 |
---|
| 328 | digits_str = "N"*digits |
---|
| 329 | letters = 2 |
---|
| 330 | letters_str = "L"*letters |
---|
| 331 | def validate(self, datastructure, **kw): |
---|
| 332 | """Validate datastructure and update datamodel.""" |
---|
| 333 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[816] | 334 | #import pdb;pdb.set_trace() |
---|
[1169] | 335 | jamb_nr_catalog = getattr(self,self.catalog) |
---|
| 336 | widget_id = self.getWidgetId() |
---|
| 337 | value = datastructure[widget_id].upper() |
---|
| 338 | err = 0 |
---|
| 339 | if not valid: #or not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 340 | #err = 'Invalid number, the expected format is: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
| 341 | err = 'Invalid number' |
---|
| 342 | elif self.reference == '': |
---|
| 343 | #s = getStudentByRegNo(self,value) |
---|
| 344 | pume = jamb_nr_catalog(jamb_reg_no = value) |
---|
| 345 | if len(pume) < 1: |
---|
| 346 | err = 'No Student with JAMB RegNo %s.' % (value) |
---|
| 347 | else: |
---|
| 348 | datastructure['pume'] = pume[0] |
---|
[463] | 349 | else: |
---|
[1169] | 350 | #import pdb;pdb.set_trace() |
---|
| 351 | record = datastructure[self.reference] |
---|
| 352 | #jamb_reg_no = getattr(record,widget_id) |
---|
| 353 | jamb_reg_no = record.Entryregno |
---|
| 354 | if jamb_reg_no != value: |
---|
| 355 | err = 'Entry Registration Nr does not match.' |
---|
| 356 | if err: |
---|
| 357 | datastructure.setError(widget_id, err) |
---|
| 358 | else: |
---|
| 359 | datamodel = datastructure.getDataModel() |
---|
| 360 | datamodel[self.fields[0]] = value |
---|
| 361 | return not err |
---|
[463] | 362 | |
---|
| 363 | InitializeClass(JambRegNoWidget) |
---|
| 364 | |
---|
| 365 | widgetRegistry.register(JambRegNoWidget) |
---|
[47] | 366 | ###) |
---|
| 367 | |
---|
[1146] | 368 | class MatricNoWidget(CPSStringWidget): ###( |
---|
| 369 | """ MatricNo Widget""" |
---|
| 370 | meta_type = "MatricNo Widget" |
---|
| 371 | def validate(self, datastructure, **kw): |
---|
| 372 | """Validate datastructure and update datamodel.""" |
---|
| 373 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 374 | #import pdb;pdb.set_trace() |
---|
[1151] | 375 | returning = self.returning_import |
---|
[1146] | 376 | results = self.results_import |
---|
| 377 | err = 0 |
---|
| 378 | if not valid: |
---|
| 379 | err = 'Invalid MatricNo %s.' % (value) |
---|
| 380 | else: |
---|
| 381 | widget_id = self.getWidgetId() |
---|
| 382 | value = datastructure[widget_id].upper() |
---|
| 383 | #s = getStudentByRegNo(self,value) |
---|
| 384 | while not err: |
---|
[1151] | 385 | res = returning(matric_no = value) |
---|
[1146] | 386 | if len(res) < 1: |
---|
| 387 | err = 'No Student with MatricNo %s.' % (value) |
---|
| 388 | continue |
---|
| 389 | datastructure['student'] = res[0] |
---|
| 390 | res = results(matric_no = value) |
---|
| 391 | if len(res) < 1: |
---|
| 392 | err = 'No Results with MatricNo %s.' % (value) |
---|
| 393 | continue |
---|
| 394 | datastructure['results'] = res |
---|
| 395 | break |
---|
| 396 | if err: |
---|
| 397 | datastructure.setError(widget_id, err) |
---|
| 398 | else: |
---|
| 399 | datamodel = datastructure.getDataModel() |
---|
| 400 | datamodel[self.fields[0]] = value |
---|
| 401 | return not err |
---|
| 402 | |
---|
| 403 | InitializeClass(MatricNoWidget) |
---|
| 404 | |
---|
| 405 | widgetRegistry.register(MatricNoWidget) |
---|
| 406 | ###) |
---|
| 407 | |
---|
| 408 | class WAeUPMultilineResultsWidget(CPSStringWidget): ###( |
---|
| 409 | """ WAeUPMultilineResults Widget""" |
---|
| 410 | meta_type = "WAeUp Multiline Results Widget" |
---|
| 411 | _properties = CPSWidget._properties + ( |
---|
| 412 | {'id': 'nr_of_lines', 'type': 'int', 'mode': 'w', |
---|
| 413 | 'label': 'Nr of Lines'}, |
---|
| 414 | ) |
---|
| 415 | nr_of_lines = 5 |
---|
| 416 | def prepare(self, datastructure, **kw): ###( |
---|
| 417 | """Prepare datastructure from datamodel.""" |
---|
| 418 | datamodel = datastructure.getDataModel() |
---|
| 419 | #import pdb;pdb.set_trace() |
---|
| 420 | widget_id = self.getWidgetId() |
---|
| 421 | v = datamodel[self.fields[0]] |
---|
| 422 | if type(v) is ListType and v: |
---|
| 423 | nr_results = len(v) |
---|
| 424 | else: |
---|
| 425 | v = [] |
---|
| 426 | nr_results = 0 |
---|
| 427 | count = 1 |
---|
| 428 | for s,g in v: |
---|
| 429 | wid = "%s%02d"% (widget_id,count) |
---|
| 430 | datastructure[wid+'_s'] = s |
---|
| 431 | datastructure[wid+'_g'] = g |
---|
| 432 | count += 1 |
---|
| 433 | if nr_results < self.nr_of_lines: |
---|
| 434 | for line in range(nr_results,self.nr_of_lines): |
---|
| 435 | v.append(('','')) |
---|
| 436 | wid = "%s%02d"% (widget_id,line) |
---|
| 437 | datastructure[wid+'_s'] = '' |
---|
| 438 | datastructure[wid+'_g'] = '' |
---|
| 439 | datastructure[widget_id] = v |
---|
| 440 | datastructure[widget_id+'_s'] = '' |
---|
| 441 | datastructure[widget_id+'_g'] = '' |
---|
| 442 | ###) |
---|
| 443 | |
---|
| 444 | def validate(self, datastructure, **kw): ###( |
---|
| 445 | """Validate datastructure and update datamodel.""" |
---|
| 446 | #import pdb;pdb.set_trace() |
---|
| 447 | widget_id = self.getWidgetId() |
---|
| 448 | err = 0 |
---|
| 449 | lines = [] |
---|
| 450 | for line in range(1,30): |
---|
| 451 | wid = "%s%02d"% (widget_id,line) |
---|
| 452 | if not datastructure.has_key(wid+'_s'): |
---|
| 453 | break |
---|
| 454 | lines.append((datastructure[wid+'_s'].strip(), |
---|
| 455 | datastructure[wid+'_g'].strip())) |
---|
[1155] | 456 | |
---|
[1146] | 457 | s = datastructure[widget_id+'_s'].strip() |
---|
| 458 | g = datastructure[widget_id+'_g'].strip() |
---|
| 459 | if s and g: |
---|
| 460 | lines.append((s,g)) |
---|
| 461 | active = [] |
---|
| 462 | for s,g in lines: |
---|
| 463 | if g != "": |
---|
| 464 | active.append((s,g)) |
---|
| 465 | if err: |
---|
| 466 | datastructure.setError(widget_id, err) |
---|
| 467 | else: |
---|
| 468 | datamodel = datastructure.getDataModel() |
---|
| 469 | datamodel[self.fields[0]] = active |
---|
| 470 | return not err |
---|
| 471 | ###) |
---|
| 472 | |
---|
| 473 | def render(self, mode, datastructure, **kw): ###( |
---|
| 474 | """Render in mode from datastructure.""" |
---|
| 475 | render_method = 'widget_waeup_multiline_result_render' |
---|
| 476 | meth = getattr(self, render_method, None) |
---|
| 477 | if meth is None: |
---|
| 478 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 479 | % (render_method, self.getId())) |
---|
| 480 | #import pdb;pdb.set_trace() |
---|
| 481 | datamodel = datastructure.getDataModel() |
---|
| 482 | widget_id = self.getWidgetId() |
---|
| 483 | lines = datamodel[self.fields[0]] |
---|
| 484 | if len(lines) < self.nr_of_lines: |
---|
| 485 | for line in range(len(lines),self.nr_of_lines + 1): |
---|
| 486 | lines.append(('','')) |
---|
| 487 | datastructure[widget_id] = lines |
---|
| 488 | datastructure[widget_id+'_s'] = '' |
---|
| 489 | datastructure[widget_id+'_g'] = '' |
---|
| 490 | ## count = 1 |
---|
| 491 | ## for s,g in v: |
---|
| 492 | ## wid = "%s%02d"% (widget_id,count) |
---|
| 493 | ## count += 1 |
---|
| 494 | return meth(mode=mode, |
---|
| 495 | datastructure=datastructure, |
---|
| 496 | ) |
---|
| 497 | ###) |
---|
| 498 | |
---|
| 499 | |
---|
| 500 | InitializeClass(WAeUPMultilineResultsWidget) |
---|
| 501 | widgetRegistry.register(WAeUPMultilineResultsWidget) |
---|
| 502 | ###) |
---|
| 503 | |
---|
[794] | 504 | class WAeUPResultsWidget(CPSStringWidget): ###( |
---|
| 505 | """ WAeUPResults Widget""" |
---|
| 506 | meta_type = "WAeUp Results Widget" |
---|
| 507 | |
---|
| 508 | def prepare(self, datastructure, **kw): ###( |
---|
| 509 | """Prepare datastructure from datamodel.""" |
---|
| 510 | datamodel = datastructure.getDataModel() |
---|
| 511 | v = datamodel[self.fields[0]] |
---|
| 512 | #import pdb;pdb.set_trace() |
---|
| 513 | widget_id = self.getWidgetId() |
---|
[807] | 514 | datastructure[widget_id] = v |
---|
[794] | 515 | datastructure[widget_id+'_s'] = '' |
---|
| 516 | datastructure[widget_id+'_g'] = '' |
---|
| 517 | ###) |
---|
| 518 | |
---|
| 519 | def validate(self, datastructure, **kw): ###( |
---|
| 520 | """Validate datastructure and update datamodel.""" |
---|
[807] | 521 | #import pdb;pdb.set_trace() |
---|
[794] | 522 | widget_id = self.getWidgetId() |
---|
| 523 | v = datastructure[widget_id] |
---|
| 524 | err = 0 |
---|
| 525 | s = datastructure[widget_id+'_s'].strip() |
---|
| 526 | g = datastructure[widget_id+'_g'].strip() |
---|
[807] | 527 | while 1: |
---|
| 528 | if not s and g: |
---|
| 529 | err = "no subject grad for subject %s " % s |
---|
| 530 | break |
---|
| 531 | i = 0 |
---|
| 532 | done = False |
---|
| 533 | for sv,gv in v: |
---|
| 534 | if sv == s: |
---|
| 535 | done = True |
---|
| 536 | if not g: |
---|
| 537 | v.pop(i) |
---|
| 538 | break |
---|
| 539 | v[i] = (s,g) |
---|
| 540 | break |
---|
| 541 | i += 1 |
---|
| 542 | if done: |
---|
| 543 | break |
---|
| 544 | if s and g: |
---|
| 545 | v.append((s,g)) |
---|
| 546 | break |
---|
[794] | 547 | if err: |
---|
| 548 | datastructure.setError(widget_id, err) |
---|
| 549 | else: |
---|
| 550 | datamodel = datastructure.getDataModel() |
---|
| 551 | datamodel[self.fields[0]] = v |
---|
| 552 | datastructure[widget_id+'_s'] = s |
---|
| 553 | datastructure[widget_id+'_g'] = g |
---|
| 554 | return not err |
---|
| 555 | ###) |
---|
| 556 | |
---|
| 557 | def render(self, mode, datastructure, **kw): ###( |
---|
| 558 | """Render in mode from datastructure.""" |
---|
| 559 | render_method = 'widget_waeup_result_render' |
---|
| 560 | meth = getattr(self, render_method, None) |
---|
| 561 | if meth is None: |
---|
| 562 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 563 | % (render_method, self.getId())) |
---|
| 564 | #import pdb;pdb.set_trace() |
---|
| 565 | datamodel = datastructure.getDataModel() |
---|
| 566 | widget_id = self.getWidgetId() |
---|
| 567 | datastructure[widget_id+'_s'] = '' |
---|
| 568 | datastructure[widget_id+'_g'] = '' |
---|
| 569 | return meth(mode=mode, |
---|
| 570 | datastructure=datastructure, |
---|
| 571 | ) |
---|
| 572 | ###) |
---|
| 573 | |
---|
| 574 | |
---|
| 575 | InitializeClass(WAeUPResultsWidget) |
---|
| 576 | widgetRegistry.register(WAeUPResultsWidget) |
---|
| 577 | ###) |
---|
| 578 | |
---|
[488] | 579 | class ScratchCardPin: ###( |
---|
| 580 | """the ScratchCardPin""" |
---|
| 581 | def __init__(self,prefix,batch_no,number): |
---|
[990] | 582 | if not batch_no and not number: |
---|
| 583 | s = prefix |
---|
[996] | 584 | if len(s) > 3: |
---|
[990] | 585 | prefix,batch_no,number = s[:3],s[3:-10],s[-10:] |
---|
| 586 | else: |
---|
[996] | 587 | prefix,batch_no,number = s,'','' |
---|
[488] | 588 | self.p = prefix |
---|
| 589 | self.b = batch_no |
---|
| 590 | self.n = number |
---|
| 591 | |
---|
| 592 | def __str__(self): |
---|
| 593 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
[1146] | 594 | |
---|
| 595 | def __repr__(self): |
---|
| 596 | return "%s%s%s" % (self.p,self.b,self.n) |
---|
[488] | 597 | ###) |
---|
| 598 | |
---|
[47] | 599 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
[22] | 600 | """ ScratchcardPin Widget""" |
---|
[199] | 601 | meta_type = "Scratchcard Pin Widget" |
---|
[488] | 602 | _properties = CPSWidget._properties + ( |
---|
| 603 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
| 604 | 'label': 'Prefix'}, |
---|
| 605 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 606 | 'label': 'Reference Field'}, |
---|
| 607 | ) |
---|
| 608 | prefix = '' |
---|
| 609 | reference = '' |
---|
[502] | 610 | def prepare(self, datastructure, **kw): ###( |
---|
[488] | 611 | """Prepare datastructure from datamodel.""" |
---|
| 612 | datamodel = datastructure.getDataModel() |
---|
| 613 | v = datamodel[self.fields[0]] |
---|
| 614 | widget_id = self.getWidgetId() |
---|
[742] | 615 | #import pdb;pdb.set_trace() |
---|
[747] | 616 | if v and type(v) is StringType: |
---|
[990] | 617 | try: |
---|
| 618 | p,b,n = v.split('-') |
---|
| 619 | v = ScratchCardPin(p,b,n) |
---|
| 620 | except ValueError: |
---|
| 621 | v = ScratchCardPin(v,'','') |
---|
[488] | 622 | if v: |
---|
[742] | 623 | p = '%s' % v.p |
---|
[488] | 624 | b = '%s' % v.b |
---|
| 625 | n = '%s' % v.n |
---|
| 626 | else: |
---|
[742] | 627 | p = self.prefix |
---|
| 628 | if p.startswith('@'): |
---|
| 629 | p = getattr(self,self.prefix[1:])() |
---|
[488] | 630 | b = n = '' |
---|
[742] | 631 | v = ScratchCardPin(p,b,n) |
---|
[488] | 632 | datastructure[widget_id] = v |
---|
[742] | 633 | datastructure[widget_id+'_p'] = p |
---|
[488] | 634 | datastructure[widget_id+'_b'] = b |
---|
| 635 | datastructure[widget_id+'_n'] = n |
---|
[758] | 636 | |
---|
[502] | 637 | ###) |
---|
| 638 | |
---|
[1169] | 639 | def validate(self, datastructure, **kw): ###( |
---|
[22] | 640 | """Validate datastructure and update datamodel.""" |
---|
[1082] | 641 | s_logger = logging.getLogger('Pin.Validate') |
---|
[488] | 642 | widget_id = self.getWidgetId() |
---|
| 643 | v = datastructure[widget_id] |
---|
[996] | 644 | #import pdb;pdb.set_trace() |
---|
[488] | 645 | err = 0 |
---|
[742] | 646 | prefix= self.prefix |
---|
| 647 | if prefix.startswith('@'): |
---|
| 648 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 649 | b = datastructure[widget_id+'_b'].strip() |
---|
| 650 | n = datastructure[widget_id+'_n'].strip() |
---|
[502] | 651 | pins = self.portal_pins |
---|
[742] | 652 | pin = "%(prefix)s%(b)s%(n)s" % vars() |
---|
[816] | 653 | do = 1 |
---|
[1025] | 654 | s_id = str(self.portal_membership.getAuthenticatedMember()) |
---|
[635] | 655 | if self.reference == "": |
---|
[1030] | 656 | ref = s_id |
---|
[635] | 657 | else: |
---|
| 658 | ref = datastructure[self.reference] |
---|
[843] | 659 | if datastructure.errors: |
---|
[816] | 660 | do = 0 |
---|
[977] | 661 | datastructure.setError(widget_id, 'Errors in other data, PIN neither checked nor used.') |
---|
[1030] | 662 | s_logger.info('"%s", "%s","entered wrong data together with pin", "%s"' % (s_id,ref,pin)) |
---|
[816] | 663 | while do: |
---|
| 664 | ok = pins.searchAndSetRecord(pin,ref,prefix) |
---|
[1030] | 665 | if ok < -2 or ok > 2: |
---|
| 666 | err = 'Unknown error, please report!' |
---|
| 667 | s_logger.info('"%s","%s", "caused unknown error with pin", "%s"' % (s_id,ref,pin)) |
---|
| 668 | break |
---|
| 669 | elif ok == -2: |
---|
[819] | 670 | err = 'Service already activated but with a different PIN.' |
---|
[1030] | 671 | s_logger.info('"%s","%s", "repeatedly activated service but with different pin", "%s"' % (s_id,ref,pin)) |
---|
[710] | 672 | break |
---|
| 673 | elif ok == -1: |
---|
[783] | 674 | err = 'Invalid PIN' |
---|
[1030] | 675 | s_logger.info('"%s","%s", "entered invalid pin", "%s"' % (s_id,ref,pin)) |
---|
[502] | 676 | break |
---|
| 677 | if ok == 0: |
---|
[796] | 678 | err = 'PIN already used.' |
---|
[1030] | 679 | s_logger.info('"%s","%s", "entered used pin", "%s"' % (s_id,ref,pin)) |
---|
[502] | 680 | break |
---|
| 681 | if ok >= 1: |
---|
[710] | 682 | #import pdb;pdb.set_trace() |
---|
[635] | 683 | if self.isStudent(): |
---|
[992] | 684 | if self.reference == "jamb_reg_no": |
---|
[997] | 685 | err = "You are already logged in." |
---|
[1030] | 686 | s_logger.info('"%s","%s", "checked admission though logged in", "%s"' % (s_id,ref,pin)) |
---|
[992] | 687 | break |
---|
[1082] | 688 | if ok == 1: |
---|
[1032] | 689 | s_logger.info('"%s","%s", "successfully used pin", "%s"' % (s_id,ref,pin)) |
---|
| 690 | else: |
---|
| 691 | s_logger.info('"%s","%s", "repeatedly used pin", "%s"' % (s_id,ref,pin)) |
---|
[637] | 692 | break |
---|
[635] | 693 | else: |
---|
| 694 | student = getStudentByRegNo(self,ref) |
---|
[1030] | 695 | s_logger.info('"%s","%s","successfully used pin","%s"' % (s_id,ref,pin)) |
---|
[502] | 696 | if student is None: |
---|
[796] | 697 | err = "Student not found." |
---|
[1032] | 698 | s_logger.info('"%s","not found in admission list"' % (ref)) |
---|
[502] | 699 | break |
---|
[648] | 700 | s_id = student.getId() |
---|
[502] | 701 | if ok == 2: |
---|
[990] | 702 | if self.reference == "jamb_reg_no": |
---|
| 703 | if hasattr(self.portal_directories.students,s_id): |
---|
| 704 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
[1032] | 705 | s_logger.info('"%s","%s", "repeatedly checked admission with pin", "%s"' % (s_id,ref,pin)) |
---|
[990] | 706 | break |
---|
| 707 | else: |
---|
[1032] | 708 | s_logger.info('"%s","%s","(non-member) repeatedly checked admission with pin", "%s"' % (s_id,ref,pin)) |
---|
[990] | 709 | else: |
---|
[1030] | 710 | err = "Unknown error" % s_id |
---|
[1032] | 711 | s_logger.info('"%s","%s", "repeatedly activated service with pin", "%s"' % (s_id,ref,pin)) |
---|
[986] | 712 | break |
---|
| 713 | try: |
---|
| 714 | student.getContent().makeStudentMember(s_id,password=pin[4:]) |
---|
[1030] | 715 | s_logger.info('"%s","%s", "has been created using pin", "%s"' % (s_id,ref,pin)) |
---|
[986] | 716 | except: |
---|
[989] | 717 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
[1032] | 718 | s_logger.info('"%s","%s", "could not be made a member with pin", "%s"' % (s_id,ref,pin)) |
---|
[986] | 719 | break |
---|
[502] | 720 | break |
---|
[488] | 721 | if err: |
---|
| 722 | datastructure.setError(widget_id, err) |
---|
| 723 | else: |
---|
| 724 | datamodel = datastructure.getDataModel() |
---|
[742] | 725 | datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n) |
---|
| 726 | datastructure[widget_id] = ScratchCardPin(prefix,b,n) |
---|
| 727 | datastructure[widget_id+'_p'] = prefix |
---|
[488] | 728 | datastructure[widget_id+'_b'] = b |
---|
| 729 | datastructure[widget_id+'_n'] = n |
---|
[502] | 730 | datastructure['s_id'] = s_id |
---|
[488] | 731 | return not err |
---|
[444] | 732 | |
---|
[1169] | 733 | ###) |
---|
| 734 | |
---|
[502] | 735 | def render(self, mode, datastructure, **kw): ###( |
---|
[488] | 736 | """Render in mode from datastructure.""" |
---|
| 737 | render_method = 'widget_scratch_card_pin_render' |
---|
| 738 | meth = getattr(self, render_method, None) |
---|
| 739 | if meth is None: |
---|
| 740 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 741 | % (render_method, self.getId())) |
---|
| 742 | |
---|
| 743 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
| 744 | # or create mode, because a default value has to be provided. |
---|
| 745 | #import pdb;pdb.set_trace() |
---|
| 746 | datamodel = datastructure.getDataModel() |
---|
| 747 | v = datamodel[self.fields[0]] |
---|
[502] | 748 | if v and type(v) is StringType: |
---|
[990] | 749 | try: |
---|
| 750 | p,b,n = v.split('-') |
---|
| 751 | v = ScratchCardPin(p,b,n) |
---|
| 752 | except ValueError: |
---|
[996] | 753 | v = ScratchCardPin(self.prefix,'1',v) |
---|
| 754 | pass |
---|
[488] | 755 | if v: |
---|
[742] | 756 | prefix= '%s' % v.p |
---|
[488] | 757 | b = '%s' % v.b |
---|
| 758 | n = '%s' % v.n |
---|
[22] | 759 | else: |
---|
[742] | 760 | prefix= self.prefix |
---|
| 761 | if prefix.startswith('@'): |
---|
| 762 | prefix= getattr(self,self.prefix[1:])() |
---|
[488] | 763 | b = n = '' |
---|
[742] | 764 | v = ScratchCardPin(prefix,b,n) |
---|
| 765 | widget_id = self.getWidgetId() |
---|
| 766 | datastructure[widget_id] = v |
---|
| 767 | datastructure[widget_id+'_p'] = prefix |
---|
| 768 | datastructure[widget_id+'_b'] = b |
---|
| 769 | datastructure[widget_id+'_n'] = n |
---|
[758] | 770 | return meth(mode=mode, |
---|
| 771 | datastructure=datastructure, |
---|
[488] | 772 | ) |
---|
[523] | 773 | ###) |
---|
[488] | 774 | |
---|
| 775 | |
---|
[22] | 776 | InitializeClass(ScratchcardPinWidget) |
---|
[199] | 777 | widgetRegistry.register(ScratchcardPinWidget) |
---|
[22] | 778 | |
---|
[47] | 779 | |
---|
| 780 | ###) |
---|
| 781 | |
---|
[1169] | 782 | class WAeUPImageWidget(CPSImageWidget): ###( |
---|
[537] | 783 | """Photo widget.""" |
---|
| 784 | meta_type = 'WAeUP Image Widget' |
---|
| 785 | |
---|
| 786 | def render(self, mode, datastructure, **kw): |
---|
| 787 | render_method = 'widget_waeup_image_render' |
---|
| 788 | meth = getattr(self, render_method, None) |
---|
| 789 | if meth is None: |
---|
| 790 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 791 | % (render_method, self.getId())) |
---|
| 792 | img_info = self.getImageInfo(datastructure) |
---|
| 793 | return meth(mode=mode, datastructure=datastructure, **img_info) |
---|
| 794 | |
---|
[1169] | 795 | ###) |
---|
[537] | 796 | |
---|
| 797 | |
---|
[1169] | 798 | |
---|
[537] | 799 | InitializeClass(WAeUPImageWidget) |
---|
| 800 | |
---|
| 801 | widgetRegistry.register(WAeUPImageWidget) |
---|
| 802 | |
---|
| 803 | |
---|
[22] | 804 | ########### |
---|
| 805 | |
---|