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