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