[47] | 1 | #-*- mode: python; mode: fold -*- |
---|
[295] | 2 | |
---|
| 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 |
---|
| 17 | |
---|
| 18 | from zLOG import LOG, DEBUG |
---|
| 19 | |
---|
[295] | 20 | class CPSSelectWidgetForRecord(CPSSelectWidget): ###( |
---|
| 21 | """Select widget. with record names""" |
---|
| 22 | meta_type = 'Select Widget for Records' |
---|
| 23 | |
---|
| 24 | field_types = ('CPS String Field',) |
---|
| 25 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 26 | |
---|
| 27 | _properties = CPSSelectWidget._properties + ( |
---|
| 28 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 29 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 30 | ) |
---|
| 31 | |
---|
| 32 | def render(self, mode, datastructure, **kw): |
---|
| 33 | """Render in mode from datastructure.""" |
---|
| 34 | value = datastructure[self.getWidgetId()] |
---|
| 35 | vocabulary = self._getVocabulary(datastructure) |
---|
| 36 | portal = getToolByName(self, 'portal_url').getPortalObject() |
---|
| 37 | cpsmcat = portal.translation_service |
---|
| 38 | if mode == 'view': |
---|
| 39 | if self.translated: |
---|
| 40 | return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore')) |
---|
| 41 | else: |
---|
| 42 | return escape(vocabulary.get(value, value)) |
---|
| 43 | elif mode == 'edit': |
---|
| 44 | html_widget_id = self.getHtmlWidgetId() |
---|
[444] | 45 | res = renderHtmlTag('select', |
---|
| 46 | name='%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 47 | id=html_widget_id) |
---|
| 48 | in_selection = 0 |
---|
| 49 | for k, v in vocabulary.items(): |
---|
| 50 | if self.translated: |
---|
| 51 | kw = {'value': k, |
---|
| 52 | 'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore') |
---|
| 53 | } |
---|
| 54 | else: |
---|
| 55 | kw = {'value': k, 'contents': v} |
---|
| 56 | if value == k: |
---|
| 57 | kw['selected'] = 'selected' |
---|
| 58 | in_selection = 1 |
---|
| 59 | res += renderHtmlTag('option', **kw) |
---|
| 60 | if value and not in_selection: |
---|
| 61 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
| 62 | 'selected': 'selected'} |
---|
| 63 | res += renderHtmlTag('option', **kw) |
---|
| 64 | res += '</select>' |
---|
| 65 | return res |
---|
| 66 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 67 | |
---|
| 68 | InitializeClass(CPSSelectWidgetForRecord) |
---|
| 69 | |
---|
| 70 | widgetRegistry.register(CPSSelectWidgetForRecord) |
---|
| 71 | |
---|
| 72 | ###) |
---|
| 73 | |
---|
[373] | 74 | class CPSStringWidgetForRecord(CPSStringWidget): ###( |
---|
[295] | 75 | """String widget.""" |
---|
| 76 | meta_type = 'String Widget For Record' |
---|
| 77 | |
---|
| 78 | field_types = ('CPS String Field',) |
---|
| 79 | field_inits = ({'is_searchabletext': 1,},) |
---|
| 80 | _properties = CPSStringWidget._properties + ( |
---|
| 81 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
| 82 | 'label': 'Record Id', 'is_required' : 1}, |
---|
| 83 | ) |
---|
| 84 | |
---|
| 85 | def render(self, mode, datastructure, **kw): |
---|
| 86 | """Render in mode from datastructure.""" |
---|
| 87 | value = datastructure[self.getWidgetId()] |
---|
| 88 | if mode == 'view': |
---|
| 89 | return escape(value) |
---|
| 90 | elif mode == 'edit': |
---|
| 91 | # XXX TODO should use an other name than kw ! |
---|
| 92 | # XXX change this everywhere |
---|
| 93 | html_widget_id = self.getHtmlWidgetId() |
---|
| 94 | kw = {'type': 'text', |
---|
| 95 | 'id' : html_widget_id, |
---|
[444] | 96 | 'name': '%s.%s:records' % (self.record_id,html_widget_id), |
---|
[295] | 97 | 'value': escape(value), |
---|
| 98 | 'size': self.display_width, |
---|
| 99 | } |
---|
| 100 | if self.size_max: |
---|
| 101 | kw['maxlength'] = self.size_max |
---|
| 102 | return renderHtmlTag('input', **kw) |
---|
| 103 | raise RuntimeError('unknown mode %s' % mode) |
---|
| 104 | |
---|
| 105 | InitializeClass(CPSStringWidgetForRecord) |
---|
| 106 | |
---|
| 107 | widgetRegistry.register(CPSStringWidgetForRecord) |
---|
| 108 | |
---|
[373] | 109 | ###) |
---|
| 110 | |
---|
| 111 | class CertificateCourseIdWidget(CPSStringWidget): ###( |
---|
| 112 | """ CertificateCourseId Widget""" |
---|
| 113 | meta_type = "CertificateCourseId Widget" |
---|
[444] | 114 | |
---|
[373] | 115 | def validate(self, datastructure, **kw): |
---|
| 116 | """Validate datastructure and update datamodel.""" |
---|
[444] | 117 | |
---|
[373] | 118 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 119 | if not valid: |
---|
| 120 | return 0 |
---|
| 121 | else: |
---|
| 122 | widget_id = self.getWidgetId() |
---|
| 123 | value = datastructure[widget_id].upper() |
---|
| 124 | err = 0 |
---|
| 125 | c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] |
---|
[381] | 126 | if hasattr(self.aq_parent,value): |
---|
[444] | 127 | err = 'Course %s already exists' % (value) |
---|
[381] | 128 | elif value not in c_ids: |
---|
[444] | 129 | err = 'Course %s does not exist' % (value) |
---|
[373] | 130 | if err: |
---|
| 131 | datastructure.setError(widget_id, err) |
---|
| 132 | else: |
---|
| 133 | datamodel = datastructure.getDataModel() |
---|
| 134 | datamodel[self.fields[0]] = value |
---|
[444] | 135 | |
---|
[373] | 136 | return not err |
---|
| 137 | |
---|
| 138 | InitializeClass(CertificateCourseIdWidget) |
---|
| 139 | |
---|
| 140 | widgetRegistry.register(CertificateCourseIdWidget) |
---|
[551] | 141 | ###) |
---|
[373] | 142 | |
---|
| 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 | |
---|
[388] | 176 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
| 177 | """ WAeUPId Widget""" |
---|
| 178 | meta_type = "WAeUPId Widget" |
---|
[444] | 179 | |
---|
[388] | 180 | def validate(self, datastructure, **kw): |
---|
| 181 | """Validate datastructure and update datamodel.""" |
---|
[444] | 182 | |
---|
[388] | 183 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
[422] | 184 | id_pat_str = r"\S" |
---|
| 185 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
[388] | 186 | if not valid: |
---|
| 187 | return 0 |
---|
| 188 | else: |
---|
[586] | 189 | portal_type_query = {'query':['Faculty', |
---|
| 190 | 'Department', |
---|
| 191 | 'Course', |
---|
| 192 | 'Certificate', |
---|
| 193 | 'CertificateCourse',]} |
---|
[388] | 194 | widget_id = self.getWidgetId() |
---|
| 195 | value = datastructure[widget_id].upper() |
---|
| 196 | err = 0 |
---|
[440] | 197 | if len(value.split()) > 1: |
---|
[444] | 198 | err = 'Invalid Id (Id contains space(s)' |
---|
[586] | 199 | elif self.portal_catalog(portal_type=portal_type_query,id=value): |
---|
| 200 | err = 'An object with the Id %s already exists in the Academic section' % (value) |
---|
[388] | 201 | if err: |
---|
| 202 | datastructure.setError(widget_id, err) |
---|
| 203 | else: |
---|
| 204 | datamodel = datastructure.getDataModel() |
---|
| 205 | datamodel[self.fields[0]] = value |
---|
[444] | 206 | |
---|
[388] | 207 | return not err |
---|
| 208 | |
---|
| 209 | InitializeClass(WAeUPIdWidget) |
---|
| 210 | |
---|
| 211 | widgetRegistry.register(WAeUPIdWidget) |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | ###) |
---|
| 215 | |
---|
[47] | 216 | class StudentIdWidget(CPSStringWidget): ###( |
---|
[22] | 217 | """ StudentId Widget""" |
---|
| 218 | meta_type = "StudentId Widget" |
---|
[57] | 219 | digits = 8 |
---|
| 220 | digits_str = "N"*digits |
---|
| 221 | letters = 2 |
---|
| 222 | letters_str = "L"*letters |
---|
[444] | 223 | |
---|
[22] | 224 | def validate(self, datastructure, **kw): |
---|
| 225 | """Validate datastructure and update datamodel.""" |
---|
[444] | 226 | |
---|
[22] | 227 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 228 | if not valid: |
---|
| 229 | return 0 |
---|
| 230 | else: |
---|
| 231 | widget_id = self.getWidgetId() |
---|
| 232 | value = datastructure[widget_id] |
---|
| 233 | err = 0 |
---|
[57] | 234 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
[444] | 235 | err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
[22] | 236 | if err: |
---|
| 237 | datastructure.setError(widget_id, err) |
---|
| 238 | else: |
---|
| 239 | datamodel = datastructure.getDataModel() |
---|
| 240 | datamodel[self.fields[0]] = value |
---|
[444] | 241 | |
---|
[22] | 242 | return not err |
---|
| 243 | |
---|
| 244 | InitializeClass(StudentIdWidget) |
---|
[199] | 245 | widgetRegistry.register(StudentIdWidget) |
---|
[22] | 246 | |
---|
[463] | 247 | ###) |
---|
[199] | 248 | |
---|
[502] | 249 | ##def getStudentByRegNo(self,reg_no): |
---|
| 250 | ## search = self.portal_catalog({'meta_type': 'StudentApplication', |
---|
| 251 | ## 'jamb_reg_no': reg_no, |
---|
| 252 | ## }) |
---|
| 253 | ## if len(search) < 1: |
---|
| 254 | ## return None |
---|
| 255 | ## return search[0].getObject(). |
---|
| 256 | |
---|
[463] | 257 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
| 258 | """ JambRegNo Widget""" |
---|
| 259 | meta_type = "JambRegNo Widget" |
---|
| 260 | digits = 8 |
---|
| 261 | digits_str = "N"*digits |
---|
| 262 | letters = 2 |
---|
| 263 | letters_str = "L"*letters |
---|
| 264 | |
---|
| 265 | def validate(self, datastructure, **kw): |
---|
| 266 | """Validate datastructure and update datamodel.""" |
---|
| 267 | |
---|
| 268 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 269 | if not valid: |
---|
| 270 | return 0 |
---|
| 271 | else: |
---|
| 272 | widget_id = self.getWidgetId() |
---|
| 273 | value = datastructure[widget_id] |
---|
| 274 | err = 0 |
---|
| 275 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
| 276 | err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
| 277 | else: |
---|
[502] | 278 | s = getStudentByRegNo(self,value) |
---|
| 279 | if s is None: |
---|
[463] | 280 | err = 'No JAMB data for %s' % (value) |
---|
| 281 | if err: |
---|
| 282 | datastructure.setError(widget_id, err) |
---|
| 283 | else: |
---|
| 284 | datamodel = datastructure.getDataModel() |
---|
| 285 | datamodel[self.fields[0]] = value |
---|
| 286 | |
---|
| 287 | return not err |
---|
| 288 | |
---|
| 289 | InitializeClass(JambRegNoWidget) |
---|
| 290 | |
---|
| 291 | widgetRegistry.register(JambRegNoWidget) |
---|
[47] | 292 | ###) |
---|
| 293 | |
---|
[488] | 294 | class ScratchCardPin: ###( |
---|
| 295 | """the ScratchCardPin""" |
---|
| 296 | def __init__(self,prefix,batch_no,number): |
---|
| 297 | self.p = prefix |
---|
| 298 | self.b = batch_no |
---|
| 299 | self.n = number |
---|
| 300 | |
---|
| 301 | def __str__(self): |
---|
| 302 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
| 303 | ###) |
---|
| 304 | |
---|
[47] | 305 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
[22] | 306 | """ ScratchcardPin Widget""" |
---|
[199] | 307 | meta_type = "Scratchcard Pin Widget" |
---|
[488] | 308 | _properties = CPSWidget._properties + ( |
---|
| 309 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
| 310 | 'label': 'Prefix'}, |
---|
| 311 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
| 312 | 'label': 'Reference Field'}, |
---|
| 313 | |
---|
| 314 | ) |
---|
| 315 | prefix = '' |
---|
| 316 | reference = '' |
---|
| 317 | |
---|
[502] | 318 | def prepare(self, datastructure, **kw): ###( |
---|
[488] | 319 | """Prepare datastructure from datamodel.""" |
---|
| 320 | datamodel = datastructure.getDataModel() |
---|
| 321 | v = datamodel[self.fields[0]] |
---|
| 322 | widget_id = self.getWidgetId() |
---|
[502] | 323 | if v and type(v) is StringType: |
---|
| 324 | p,b,n = v.split('-') |
---|
| 325 | v = ScratchCardPin(v,b,n) |
---|
[488] | 326 | if v: |
---|
| 327 | b = '%s' % v.b |
---|
| 328 | n = '%s' % v.n |
---|
| 329 | else: |
---|
| 330 | b = n = '' |
---|
| 331 | datastructure[widget_id] = v |
---|
| 332 | datastructure[widget_id+'_b'] = b |
---|
| 333 | datastructure[widget_id+'_n'] = n |
---|
| 334 | |
---|
[502] | 335 | ###) |
---|
| 336 | |
---|
[22] | 337 | def validate(self, datastructure, **kw): |
---|
| 338 | """Validate datastructure and update datamodel.""" |
---|
[488] | 339 | widget_id = self.getWidgetId() |
---|
| 340 | v = datastructure[widget_id] |
---|
| 341 | err = 0 |
---|
[502] | 342 | p = self.prefix |
---|
[488] | 343 | b = datastructure[widget_id+'_b'].strip() |
---|
| 344 | n = datastructure[widget_id+'_n'].strip() |
---|
[502] | 345 | pins = self.portal_pins |
---|
| 346 | pin = "%(p)s%(b)s%(n)s" % vars() |
---|
[635] | 347 | if self.reference == "": |
---|
| 348 | ref = str(self.portal_membership.getAuthenticatedMember()) |
---|
| 349 | else: |
---|
| 350 | ref = datastructure[self.reference] |
---|
[637] | 351 | #import pdb;pdb.set_trace() |
---|
[502] | 352 | ok = pins.searchAndSetRecord(pin,ref) |
---|
| 353 | while 1: |
---|
| 354 | if ok == -1: |
---|
| 355 | err = 'invalid Pin' |
---|
| 356 | break |
---|
| 357 | if ok == 0: |
---|
| 358 | err = 'Pin already used' |
---|
| 359 | break |
---|
| 360 | if ok >= 1: |
---|
[635] | 361 | if self.isStudent(): |
---|
| 362 | student = self.getStudentInfo()['student'] |
---|
[637] | 363 | s_id = student.getId() |
---|
| 364 | break |
---|
[635] | 365 | else: |
---|
| 366 | student = getStudentByRegNo(self,ref) |
---|
[502] | 367 | if student is None: |
---|
| 368 | err = "Student not Found in validate SC" |
---|
| 369 | break |
---|
[648] | 370 | s_id = student.getId() |
---|
[502] | 371 | if ok == 2: |
---|
| 372 | break |
---|
[511] | 373 | student.getContent().makeStudentMember(s_id,password=pin[4:]) |
---|
[502] | 374 | break |
---|
[488] | 375 | if err: |
---|
| 376 | datastructure.setError(widget_id, err) |
---|
| 377 | else: |
---|
| 378 | datamodel = datastructure.getDataModel() |
---|
| 379 | datamodel[self.fields[0]] = ScratchCardPin(self.prefix,b,n) |
---|
| 380 | datastructure[widget_id] = ScratchCardPin(self.prefix,b,n) |
---|
| 381 | datastructure[widget_id+'_b'] = b |
---|
| 382 | datastructure[widget_id+'_n'] = n |
---|
[502] | 383 | datastructure['s_id'] = s_id |
---|
[488] | 384 | #import pdb;pdb.set_trace() |
---|
| 385 | return not err |
---|
[444] | 386 | |
---|
[502] | 387 | def render(self, mode, datastructure, **kw): ###( |
---|
[488] | 388 | """Render in mode from datastructure.""" |
---|
| 389 | render_method = 'widget_scratch_card_pin_render' |
---|
| 390 | meth = getattr(self, render_method, None) |
---|
| 391 | if meth is None: |
---|
| 392 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 393 | % (render_method, self.getId())) |
---|
| 394 | |
---|
| 395 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
| 396 | # or create mode, because a default value has to be provided. |
---|
| 397 | #import pdb;pdb.set_trace() |
---|
| 398 | datamodel = datastructure.getDataModel() |
---|
| 399 | v = datamodel[self.fields[0]] |
---|
[502] | 400 | if v and type(v) is StringType: |
---|
| 401 | p,b,n = v.split('-') |
---|
[523] | 402 | v = ScratchCardPin(p,b,n) |
---|
[488] | 403 | if v: |
---|
| 404 | b = '%s' % v.b |
---|
| 405 | n = '%s' % v.n |
---|
[22] | 406 | else: |
---|
[488] | 407 | b = n = '' |
---|
| 408 | if mode in ['edit', 'create']: |
---|
[22] | 409 | widget_id = self.getWidgetId() |
---|
[488] | 410 | datastructure[widget_id] = v |
---|
| 411 | datastructure[widget_id+'_b'] = b |
---|
| 412 | datastructure[widget_id+'_n'] = n |
---|
| 413 | return meth(mode=mode, |
---|
| 414 | datastructure=datastructure, |
---|
| 415 | prefix=self.prefix, |
---|
| 416 | ) |
---|
[523] | 417 | ###) |
---|
[488] | 418 | |
---|
| 419 | |
---|
[22] | 420 | InitializeClass(ScratchcardPinWidget) |
---|
[199] | 421 | widgetRegistry.register(ScratchcardPinWidget) |
---|
[22] | 422 | |
---|
[47] | 423 | |
---|
| 424 | ###) |
---|
| 425 | |
---|
[537] | 426 | class WAeUPImageWidget(CPSImageWidget): |
---|
| 427 | """Photo widget.""" |
---|
| 428 | meta_type = 'WAeUP Image Widget' |
---|
| 429 | |
---|
| 430 | def render(self, mode, datastructure, **kw): |
---|
| 431 | render_method = 'widget_waeup_image_render' |
---|
| 432 | meth = getattr(self, render_method, None) |
---|
| 433 | if meth is None: |
---|
| 434 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
| 435 | % (render_method, self.getId())) |
---|
| 436 | img_info = self.getImageInfo(datastructure) |
---|
| 437 | return meth(mode=mode, datastructure=datastructure, **img_info) |
---|
| 438 | |
---|
| 439 | |
---|
| 440 | |
---|
| 441 | InitializeClass(WAeUPImageWidget) |
---|
| 442 | |
---|
| 443 | widgetRegistry.register(WAeUPImageWidget) |
---|
| 444 | |
---|
| 445 | |
---|
[22] | 446 | ########### |
---|
| 447 | |
---|