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