1 | #-*- mode: python; mode: fold -*- |
---|
2 | # $Id: Widgets.py 1155 2006-12-30 09:41:22Z henrik $ |
---|
3 | from cgi import escape |
---|
4 | from types import * |
---|
5 | from Globals import InitializeClass |
---|
6 | ##from Products.CPSSchemas.Widget import CPSWidgetType |
---|
7 | from Products.CMFCore.utils import getToolByName |
---|
8 | from Products.CPSSchemas.BasicWidgets import CPSWidget, CPSStringWidget, CPSEmailWidget,CPSImageWidget |
---|
9 | from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget |
---|
10 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
11 | from Products.CPSSchemas.Widget import widgetRegistry |
---|
12 | ##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry |
---|
13 | from DateTime.DateTime import DateTime |
---|
14 | from AccessControl import getSecurityManager |
---|
15 | from Products.WAeUP_SRP.Students import getStudentByRegNo |
---|
16 | from re import compile |
---|
17 | import logging |
---|
18 | |
---|
19 | #from zLOG import LOG, DEBUG |
---|
20 | |
---|
21 | class CPSSelectWidgetForRecord(CPSSelectWidget): ###( |
---|
22 | """Select widget. with record names""" |
---|
23 | meta_type = 'Select Widget for Records' |
---|
24 | |
---|
25 | field_types = ('CPS String Field',) |
---|
26 | field_inits = ({'is_searchabletext': 1,},) |
---|
27 | |
---|
28 | _properties = CPSSelectWidget._properties + ( |
---|
29 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
30 | 'label': 'Record Id', 'is_required' : 1}, |
---|
31 | ) |
---|
32 | |
---|
33 | def render(self, mode, datastructure, **kw): |
---|
34 | """Render in mode from datastructure.""" |
---|
35 | value = datastructure[self.getWidgetId()] |
---|
36 | vocabulary = self._getVocabulary(datastructure) |
---|
37 | portal = getToolByName(self, 'portal_url').getPortalObject() |
---|
38 | cpsmcat = portal.translation_service |
---|
39 | if mode == 'view': |
---|
40 | if self.translated: |
---|
41 | return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore')) |
---|
42 | else: |
---|
43 | return escape(vocabulary.get(value, value)) |
---|
44 | elif mode == 'edit': |
---|
45 | html_widget_id = self.getHtmlWidgetId() |
---|
46 | res = renderHtmlTag('select', |
---|
47 | name='%s.%s:records' % (self.record_id,html_widget_id), |
---|
48 | id=html_widget_id) |
---|
49 | in_selection = 0 |
---|
50 | for k, v in vocabulary.items(): |
---|
51 | if self.translated: |
---|
52 | kw = {'value': k, |
---|
53 | 'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore') |
---|
54 | } |
---|
55 | else: |
---|
56 | kw = {'value': k, 'contents': v} |
---|
57 | if value == k: |
---|
58 | kw['selected'] = 'selected' |
---|
59 | in_selection = 1 |
---|
60 | res += renderHtmlTag('option', **kw) |
---|
61 | if value and not in_selection: |
---|
62 | kw = {'value': value, 'contents': 'invalid: '+ str(value), |
---|
63 | 'selected': 'selected'} |
---|
64 | res += renderHtmlTag('option', **kw) |
---|
65 | res += '</select>' |
---|
66 | return res |
---|
67 | raise RuntimeError('unknown mode %s' % mode) |
---|
68 | |
---|
69 | InitializeClass(CPSSelectWidgetForRecord) |
---|
70 | |
---|
71 | widgetRegistry.register(CPSSelectWidgetForRecord) |
---|
72 | |
---|
73 | ###) |
---|
74 | |
---|
75 | class CPSStringWidgetForRecord(CPSStringWidget): ###( |
---|
76 | """String widget.""" |
---|
77 | meta_type = 'String Widget For Record' |
---|
78 | |
---|
79 | field_types = ('CPS String Field',) |
---|
80 | field_inits = ({'is_searchabletext': 1,},) |
---|
81 | _properties = CPSStringWidget._properties + ( |
---|
82 | {'id': 'record_id', 'type': 'string', 'mode': 'w', |
---|
83 | 'label': 'Record Id', 'is_required' : 1}, |
---|
84 | ) |
---|
85 | |
---|
86 | def render(self, mode, datastructure, **kw): |
---|
87 | """Render in mode from datastructure.""" |
---|
88 | value = datastructure[self.getWidgetId()] |
---|
89 | if mode == 'view': |
---|
90 | return escape(value) |
---|
91 | elif mode == 'edit': |
---|
92 | # XXX TODO should use an other name than kw ! |
---|
93 | # XXX change this everywhere |
---|
94 | html_widget_id = self.getHtmlWidgetId() |
---|
95 | kw = {'type': 'text', |
---|
96 | 'id' : html_widget_id, |
---|
97 | 'name': '%s.%s:records' % (self.record_id,html_widget_id), |
---|
98 | 'value': escape(value), |
---|
99 | 'size': self.display_width, |
---|
100 | } |
---|
101 | if self.size_max: |
---|
102 | kw['maxlength'] = self.size_max |
---|
103 | return renderHtmlTag('input', **kw) |
---|
104 | raise RuntimeError('unknown mode %s' % mode) |
---|
105 | |
---|
106 | InitializeClass(CPSStringWidgetForRecord) |
---|
107 | |
---|
108 | widgetRegistry.register(CPSStringWidgetForRecord) |
---|
109 | |
---|
110 | ###) |
---|
111 | |
---|
112 | class CertificateCourseIdWidget(CPSStringWidget): ###( |
---|
113 | """ CertificateCourseId Widget""" |
---|
114 | meta_type = "CertificateCourseId Widget" |
---|
115 | |
---|
116 | def validate(self, datastructure, **kw): |
---|
117 | """Validate datastructure and update datamodel.""" |
---|
118 | |
---|
119 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
120 | if not valid: |
---|
121 | return 0 |
---|
122 | else: |
---|
123 | widget_id = self.getWidgetId() |
---|
124 | value = datastructure[widget_id].upper() |
---|
125 | err = 0 |
---|
126 | c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] |
---|
127 | if hasattr(self.aq_parent,value): |
---|
128 | err = 'Course %s already exists' % (value) |
---|
129 | elif value not in c_ids: |
---|
130 | err = 'Course %s does not exist' % (value) |
---|
131 | if err: |
---|
132 | datastructure.setError(widget_id, err) |
---|
133 | else: |
---|
134 | datamodel = datastructure.getDataModel() |
---|
135 | datamodel[self.fields[0]] = value |
---|
136 | |
---|
137 | return not err |
---|
138 | |
---|
139 | InitializeClass(CertificateCourseIdWidget) |
---|
140 | |
---|
141 | widgetRegistry.register(CertificateCourseIdWidget) |
---|
142 | ###) |
---|
143 | |
---|
144 | |
---|
145 | class CourseIdWidget(CPSStringWidget): ###( |
---|
146 | """ CourseId Widget""" |
---|
147 | meta_type = "CourseId Widget" |
---|
148 | |
---|
149 | def validate(self, datastructure, **kw): |
---|
150 | """Validate datastructure and update datamodel.""" |
---|
151 | |
---|
152 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
153 | if not valid: |
---|
154 | return 0 |
---|
155 | else: |
---|
156 | widget_id = self.getWidgetId() |
---|
157 | value = datastructure[widget_id].upper() |
---|
158 | err = 0 |
---|
159 | res = self.portal_catalog(meta_type= "Course",id = value) |
---|
160 | if len(res) > 0: |
---|
161 | err = 'Course %s already exists' % (value) |
---|
162 | if err: |
---|
163 | datastructure.setError(widget_id, err) |
---|
164 | else: |
---|
165 | datamodel = datastructure.getDataModel() |
---|
166 | datamodel[self.fields[0]] = value |
---|
167 | |
---|
168 | return not err |
---|
169 | |
---|
170 | InitializeClass(CourseIdWidget) |
---|
171 | |
---|
172 | widgetRegistry.register(CourseIdWidget) |
---|
173 | |
---|
174 | |
---|
175 | ###) |
---|
176 | |
---|
177 | class WAeUPReservedRoomsWidget(CPSStringWidget): ###( |
---|
178 | """ WAeUPReservedRooms Widget""" |
---|
179 | meta_type = "WAeUPReservedRooms Widget" |
---|
180 | |
---|
181 | def validate(self, datastructure, **kw): |
---|
182 | """Validate datastructure and update datamodel.""" |
---|
183 | import re |
---|
184 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
185 | if not valid: |
---|
186 | return 0 |
---|
187 | else: |
---|
188 | widget_id = self.getWidgetId() |
---|
189 | value = datastructure[widget_id] |
---|
190 | err = 0 |
---|
191 | try: |
---|
192 | reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',value) |
---|
193 | if r] |
---|
194 | except (ValueError,IndexError),msg: |
---|
195 | err = str(msg) |
---|
196 | if err: |
---|
197 | datastructure.setError(widget_id, err) |
---|
198 | else: |
---|
199 | datamodel = datastructure.getDataModel() |
---|
200 | datamodel[self.fields[0]] = value |
---|
201 | return not err |
---|
202 | |
---|
203 | InitializeClass(WAeUPReservedRoomsWidget) |
---|
204 | |
---|
205 | widgetRegistry.register(WAeUPReservedRoomsWidget) |
---|
206 | |
---|
207 | |
---|
208 | ###) |
---|
209 | |
---|
210 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
211 | """ WAeUPId Widget""" |
---|
212 | meta_type = "WAeUPId Widget" |
---|
213 | |
---|
214 | def validate(self, datastructure, **kw): |
---|
215 | """Validate datastructure and update datamodel.""" |
---|
216 | |
---|
217 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
218 | id_pat_str = r"\S" |
---|
219 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
220 | if not valid: |
---|
221 | return 0 |
---|
222 | else: |
---|
223 | portal_type_query = {'query':['Faculty', |
---|
224 | 'Department', |
---|
225 | 'Course', |
---|
226 | 'Certificate', |
---|
227 | 'CertificateCourse',]} |
---|
228 | widget_id = self.getWidgetId() |
---|
229 | value = datastructure[widget_id].upper() |
---|
230 | err = 0 |
---|
231 | if len(value.split()) > 1: |
---|
232 | err = 'Invalid Id, Id contains space(s).' |
---|
233 | elif self.portal_catalog(portal_type=portal_type_query,id=value): |
---|
234 | err = 'An object with the Id %s already exists in the Academic Section.' % (value) |
---|
235 | if err: |
---|
236 | datastructure.setError(widget_id, err) |
---|
237 | else: |
---|
238 | datamodel = datastructure.getDataModel() |
---|
239 | datamodel[self.fields[0]] = value |
---|
240 | |
---|
241 | return not err |
---|
242 | |
---|
243 | InitializeClass(WAeUPIdWidget) |
---|
244 | |
---|
245 | widgetRegistry.register(WAeUPIdWidget) |
---|
246 | |
---|
247 | |
---|
248 | ###) |
---|
249 | |
---|
250 | class StudyCourseWidget(CPSStringWidget): ###( |
---|
251 | """ StudyCourse Widget""" |
---|
252 | meta_type = "StudyCourse Widget" |
---|
253 | |
---|
254 | def validate(self, datastructure, **kw): |
---|
255 | """Validate datastructure and update datamodel.""" |
---|
256 | #from Products.zdb import set_trace |
---|
257 | #set_trace() |
---|
258 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
259 | id_pat_str = r"\S" |
---|
260 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
261 | if not valid: |
---|
262 | return 0 |
---|
263 | else: |
---|
264 | widget_id = self.getWidgetId() |
---|
265 | value = datastructure[widget_id].upper() |
---|
266 | err = 0 |
---|
267 | if len(value.split()) > 1: |
---|
268 | err = 'Invalid Id, Id contains space(s).' |
---|
269 | elif not self.portal_catalog(portal_type='Certificate',id=value): |
---|
270 | err = 'No such certificate' |
---|
271 | if err: |
---|
272 | datastructure.setError(widget_id, err) |
---|
273 | else: |
---|
274 | datamodel = datastructure.getDataModel() |
---|
275 | datamodel[self.fields[0]] = value |
---|
276 | return not err |
---|
277 | |
---|
278 | InitializeClass(StudyCourseWidget) |
---|
279 | |
---|
280 | widgetRegistry.register(StudyCourseWidget) |
---|
281 | ###) |
---|
282 | |
---|
283 | ##class StudentIdWidget(CPSStringWidget): ###( |
---|
284 | ## """ StudentId Widget""" |
---|
285 | ## meta_type = "StudentId Widget" |
---|
286 | ## digits = 8 |
---|
287 | ## digits_str = "N"*digits |
---|
288 | ## letters = 2 |
---|
289 | ## letters_str = "L"*letters |
---|
290 | ## |
---|
291 | ## def validate(self, datastructure, **kw): |
---|
292 | ## """Validate datastructure and update datamodel.""" |
---|
293 | ## |
---|
294 | ## valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
295 | ## if not valid: |
---|
296 | ## return 0 |
---|
297 | ## else: |
---|
298 | ## widget_id = self.getWidgetId() |
---|
299 | ## value = datastructure[widget_id] |
---|
300 | ## err = 0 |
---|
301 | ## if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
302 | ## err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
303 | ## if err: |
---|
304 | ## datastructure.setError(widget_id, err) |
---|
305 | ## else: |
---|
306 | ## datamodel = datastructure.getDataModel() |
---|
307 | ## datamodel[self.fields[0]] = value |
---|
308 | ## |
---|
309 | ## return not err |
---|
310 | ## |
---|
311 | ##InitializeClass(StudentIdWidget) |
---|
312 | ##widgetRegistry.register(StudentIdWidget) |
---|
313 | ## |
---|
314 | #####) |
---|
315 | |
---|
316 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
317 | """ JambRegNo Widget""" |
---|
318 | meta_type = "JambRegNo Widget" |
---|
319 | digits = 8 |
---|
320 | digits_str = "N"*digits |
---|
321 | letters = 2 |
---|
322 | letters_str = "L"*letters |
---|
323 | def validate(self, datastructure, **kw): |
---|
324 | """Validate datastructure and update datamodel.""" |
---|
325 | |
---|
326 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
327 | #import pdb;pdb.set_trace() |
---|
328 | pumeresults = self.portal_pumeresults |
---|
329 | if not valid: |
---|
330 | return 0 |
---|
331 | else: |
---|
332 | widget_id = self.getWidgetId() |
---|
333 | value = datastructure[widget_id].upper() |
---|
334 | err = 0 |
---|
335 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
336 | err = 'Invalid number, the expected format is: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
337 | else: |
---|
338 | #s = getStudentByRegNo(self,value) |
---|
339 | pume = pumeresults(jamb_reg_no = value) |
---|
340 | if len(pume) < 1: |
---|
341 | err = 'No Student with JAMB RegNo %s.' % (value) |
---|
342 | else: |
---|
343 | datastructure['pume'] = pume[0] |
---|
344 | if err: |
---|
345 | datastructure.setError(widget_id, err) |
---|
346 | else: |
---|
347 | datamodel = datastructure.getDataModel() |
---|
348 | datamodel[self.fields[0]] = value |
---|
349 | return not err |
---|
350 | |
---|
351 | InitializeClass(JambRegNoWidget) |
---|
352 | |
---|
353 | widgetRegistry.register(JambRegNoWidget) |
---|
354 | ###) |
---|
355 | |
---|
356 | class MatricNoWidget(CPSStringWidget): ###( |
---|
357 | """ MatricNo Widget""" |
---|
358 | meta_type = "MatricNo Widget" |
---|
359 | def validate(self, datastructure, **kw): |
---|
360 | """Validate datastructure and update datamodel.""" |
---|
361 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
362 | #import pdb;pdb.set_trace() |
---|
363 | returning = self.returning_import |
---|
364 | results = self.results_import |
---|
365 | err = 0 |
---|
366 | if not valid: |
---|
367 | err = 'Invalid MatricNo %s.' % (value) |
---|
368 | else: |
---|
369 | widget_id = self.getWidgetId() |
---|
370 | value = datastructure[widget_id].upper() |
---|
371 | #s = getStudentByRegNo(self,value) |
---|
372 | while not err: |
---|
373 | res = returning(matric_no = value) |
---|
374 | if len(res) < 1: |
---|
375 | err = 'No Student with MatricNo %s.' % (value) |
---|
376 | continue |
---|
377 | datastructure['student'] = res[0] |
---|
378 | res = results(matric_no = value) |
---|
379 | if len(res) < 1: |
---|
380 | err = 'No Results with MatricNo %s.' % (value) |
---|
381 | continue |
---|
382 | datastructure['results'] = res |
---|
383 | break |
---|
384 | if err: |
---|
385 | datastructure.setError(widget_id, err) |
---|
386 | else: |
---|
387 | datamodel = datastructure.getDataModel() |
---|
388 | datamodel[self.fields[0]] = value |
---|
389 | return not err |
---|
390 | |
---|
391 | InitializeClass(MatricNoWidget) |
---|
392 | |
---|
393 | widgetRegistry.register(MatricNoWidget) |
---|
394 | ###) |
---|
395 | |
---|
396 | class WAeUPMultilineResultsWidget(CPSStringWidget): ###( |
---|
397 | """ WAeUPMultilineResults Widget""" |
---|
398 | meta_type = "WAeUp Multiline Results Widget" |
---|
399 | _properties = CPSWidget._properties + ( |
---|
400 | {'id': 'nr_of_lines', 'type': 'int', 'mode': 'w', |
---|
401 | 'label': 'Nr of Lines'}, |
---|
402 | ) |
---|
403 | nr_of_lines = 5 |
---|
404 | def prepare(self, datastructure, **kw): ###( |
---|
405 | """Prepare datastructure from datamodel.""" |
---|
406 | datamodel = datastructure.getDataModel() |
---|
407 | #import pdb;pdb.set_trace() |
---|
408 | widget_id = self.getWidgetId() |
---|
409 | v = datamodel[self.fields[0]] |
---|
410 | if type(v) is ListType and v: |
---|
411 | nr_results = len(v) |
---|
412 | else: |
---|
413 | v = [] |
---|
414 | nr_results = 0 |
---|
415 | count = 1 |
---|
416 | for s,g in v: |
---|
417 | wid = "%s%02d"% (widget_id,count) |
---|
418 | datastructure[wid+'_s'] = s |
---|
419 | datastructure[wid+'_g'] = g |
---|
420 | count += 1 |
---|
421 | if nr_results < self.nr_of_lines: |
---|
422 | for line in range(nr_results,self.nr_of_lines): |
---|
423 | v.append(('','')) |
---|
424 | wid = "%s%02d"% (widget_id,line) |
---|
425 | datastructure[wid+'_s'] = '' |
---|
426 | datastructure[wid+'_g'] = '' |
---|
427 | datastructure[widget_id] = v |
---|
428 | datastructure[widget_id+'_s'] = '' |
---|
429 | datastructure[widget_id+'_g'] = '' |
---|
430 | ###) |
---|
431 | |
---|
432 | def validate(self, datastructure, **kw): ###( |
---|
433 | """Validate datastructure and update datamodel.""" |
---|
434 | #import pdb;pdb.set_trace() |
---|
435 | widget_id = self.getWidgetId() |
---|
436 | err = 0 |
---|
437 | lines = [] |
---|
438 | for line in range(1,30): |
---|
439 | wid = "%s%02d"% (widget_id,line) |
---|
440 | if not datastructure.has_key(wid+'_s'): |
---|
441 | break |
---|
442 | lines.append((datastructure[wid+'_s'].strip(), |
---|
443 | datastructure[wid+'_g'].strip())) |
---|
444 | |
---|
445 | s = datastructure[widget_id+'_s'].strip() |
---|
446 | g = datastructure[widget_id+'_g'].strip() |
---|
447 | if s and g: |
---|
448 | lines.append((s,g)) |
---|
449 | active = [] |
---|
450 | for s,g in lines: |
---|
451 | if g != "": |
---|
452 | active.append((s,g)) |
---|
453 | if err: |
---|
454 | datastructure.setError(widget_id, err) |
---|
455 | else: |
---|
456 | datamodel = datastructure.getDataModel() |
---|
457 | datamodel[self.fields[0]] = active |
---|
458 | return not err |
---|
459 | ###) |
---|
460 | |
---|
461 | def render(self, mode, datastructure, **kw): ###( |
---|
462 | """Render in mode from datastructure.""" |
---|
463 | render_method = 'widget_waeup_multiline_result_render' |
---|
464 | meth = getattr(self, render_method, None) |
---|
465 | if meth is None: |
---|
466 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
467 | % (render_method, self.getId())) |
---|
468 | #import pdb;pdb.set_trace() |
---|
469 | datamodel = datastructure.getDataModel() |
---|
470 | widget_id = self.getWidgetId() |
---|
471 | lines = datamodel[self.fields[0]] |
---|
472 | if len(lines) < self.nr_of_lines: |
---|
473 | for line in range(len(lines),self.nr_of_lines + 1): |
---|
474 | lines.append(('','')) |
---|
475 | datastructure[widget_id] = lines |
---|
476 | datastructure[widget_id+'_s'] = '' |
---|
477 | datastructure[widget_id+'_g'] = '' |
---|
478 | ## count = 1 |
---|
479 | ## for s,g in v: |
---|
480 | ## wid = "%s%02d"% (widget_id,count) |
---|
481 | ## count += 1 |
---|
482 | return meth(mode=mode, |
---|
483 | datastructure=datastructure, |
---|
484 | ) |
---|
485 | ###) |
---|
486 | |
---|
487 | |
---|
488 | InitializeClass(WAeUPMultilineResultsWidget) |
---|
489 | widgetRegistry.register(WAeUPMultilineResultsWidget) |
---|
490 | ###) |
---|
491 | |
---|
492 | class WAeUPResultsWidget(CPSStringWidget): ###( |
---|
493 | """ WAeUPResults Widget""" |
---|
494 | meta_type = "WAeUp Results Widget" |
---|
495 | |
---|
496 | def prepare(self, datastructure, **kw): ###( |
---|
497 | """Prepare datastructure from datamodel.""" |
---|
498 | datamodel = datastructure.getDataModel() |
---|
499 | v = datamodel[self.fields[0]] |
---|
500 | #import pdb;pdb.set_trace() |
---|
501 | widget_id = self.getWidgetId() |
---|
502 | datastructure[widget_id] = v |
---|
503 | datastructure[widget_id+'_s'] = '' |
---|
504 | datastructure[widget_id+'_g'] = '' |
---|
505 | ###) |
---|
506 | |
---|
507 | def validate(self, datastructure, **kw): ###( |
---|
508 | """Validate datastructure and update datamodel.""" |
---|
509 | #import pdb;pdb.set_trace() |
---|
510 | widget_id = self.getWidgetId() |
---|
511 | v = datastructure[widget_id] |
---|
512 | err = 0 |
---|
513 | s = datastructure[widget_id+'_s'].strip() |
---|
514 | g = datastructure[widget_id+'_g'].strip() |
---|
515 | while 1: |
---|
516 | if not s and g: |
---|
517 | err = "no subject grad for subject %s " % s |
---|
518 | break |
---|
519 | i = 0 |
---|
520 | done = False |
---|
521 | for sv,gv in v: |
---|
522 | if sv == s: |
---|
523 | done = True |
---|
524 | if not g: |
---|
525 | v.pop(i) |
---|
526 | break |
---|
527 | v[i] = (s,g) |
---|
528 | break |
---|
529 | i += 1 |
---|
530 | if done: |
---|
531 | break |
---|
532 | if s and g: |
---|
533 | v.append((s,g)) |
---|
534 | break |
---|
535 | if err: |
---|
536 | datastructure.setError(widget_id, err) |
---|
537 | else: |
---|
538 | datamodel = datastructure.getDataModel() |
---|
539 | datamodel[self.fields[0]] = v |
---|
540 | datastructure[widget_id+'_s'] = s |
---|
541 | datastructure[widget_id+'_g'] = g |
---|
542 | return not err |
---|
543 | ###) |
---|
544 | |
---|
545 | def render(self, mode, datastructure, **kw): ###( |
---|
546 | """Render in mode from datastructure.""" |
---|
547 | render_method = 'widget_waeup_result_render' |
---|
548 | meth = getattr(self, render_method, None) |
---|
549 | if meth is None: |
---|
550 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
551 | % (render_method, self.getId())) |
---|
552 | #import pdb;pdb.set_trace() |
---|
553 | datamodel = datastructure.getDataModel() |
---|
554 | widget_id = self.getWidgetId() |
---|
555 | datastructure[widget_id+'_s'] = '' |
---|
556 | datastructure[widget_id+'_g'] = '' |
---|
557 | return meth(mode=mode, |
---|
558 | datastructure=datastructure, |
---|
559 | ) |
---|
560 | ###) |
---|
561 | |
---|
562 | |
---|
563 | InitializeClass(WAeUPResultsWidget) |
---|
564 | widgetRegistry.register(WAeUPResultsWidget) |
---|
565 | ###) |
---|
566 | |
---|
567 | class ScratchCardPin: ###( |
---|
568 | """the ScratchCardPin""" |
---|
569 | def __init__(self,prefix,batch_no,number): |
---|
570 | if not batch_no and not number: |
---|
571 | s = prefix |
---|
572 | if len(s) > 3: |
---|
573 | prefix,batch_no,number = s[:3],s[3:-10],s[-10:] |
---|
574 | else: |
---|
575 | prefix,batch_no,number = s,'','' |
---|
576 | self.p = prefix |
---|
577 | self.b = batch_no |
---|
578 | self.n = number |
---|
579 | |
---|
580 | def __str__(self): |
---|
581 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
582 | |
---|
583 | def __repr__(self): |
---|
584 | return "%s%s%s" % (self.p,self.b,self.n) |
---|
585 | ###) |
---|
586 | |
---|
587 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
588 | """ ScratchcardPin Widget""" |
---|
589 | meta_type = "Scratchcard Pin Widget" |
---|
590 | _properties = CPSWidget._properties + ( |
---|
591 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
592 | 'label': 'Prefix'}, |
---|
593 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
594 | 'label': 'Reference Field'}, |
---|
595 | |
---|
596 | ) |
---|
597 | prefix = '' |
---|
598 | reference = '' |
---|
599 | def prepare(self, datastructure, **kw): ###( |
---|
600 | """Prepare datastructure from datamodel.""" |
---|
601 | datamodel = datastructure.getDataModel() |
---|
602 | v = datamodel[self.fields[0]] |
---|
603 | widget_id = self.getWidgetId() |
---|
604 | #import pdb;pdb.set_trace() |
---|
605 | if v and type(v) is StringType: |
---|
606 | try: |
---|
607 | p,b,n = v.split('-') |
---|
608 | v = ScratchCardPin(p,b,n) |
---|
609 | except ValueError: |
---|
610 | v = ScratchCardPin(v,'','') |
---|
611 | if v: |
---|
612 | p = '%s' % v.p |
---|
613 | b = '%s' % v.b |
---|
614 | n = '%s' % v.n |
---|
615 | else: |
---|
616 | p = self.prefix |
---|
617 | if p.startswith('@'): |
---|
618 | p = getattr(self,self.prefix[1:])() |
---|
619 | b = n = '' |
---|
620 | v = ScratchCardPin(p,b,n) |
---|
621 | datastructure[widget_id] = v |
---|
622 | datastructure[widget_id+'_p'] = p |
---|
623 | datastructure[widget_id+'_b'] = b |
---|
624 | datastructure[widget_id+'_n'] = n |
---|
625 | |
---|
626 | ###) |
---|
627 | |
---|
628 | def validate(self, datastructure, **kw): |
---|
629 | """Validate datastructure and update datamodel.""" |
---|
630 | s_logger = logging.getLogger('Pin.Validate') |
---|
631 | widget_id = self.getWidgetId() |
---|
632 | v = datastructure[widget_id] |
---|
633 | #import pdb;pdb.set_trace() |
---|
634 | err = 0 |
---|
635 | prefix= self.prefix |
---|
636 | if prefix.startswith('@'): |
---|
637 | prefix= getattr(self,self.prefix[1:])() |
---|
638 | b = datastructure[widget_id+'_b'].strip() |
---|
639 | n = datastructure[widget_id+'_n'].strip() |
---|
640 | pins = self.portal_pins |
---|
641 | pin = "%(prefix)s%(b)s%(n)s" % vars() |
---|
642 | do = 1 |
---|
643 | s_id = str(self.portal_membership.getAuthenticatedMember()) |
---|
644 | if self.reference == "": |
---|
645 | ref = s_id |
---|
646 | else: |
---|
647 | ref = datastructure[self.reference] |
---|
648 | if datastructure.errors: |
---|
649 | do = 0 |
---|
650 | datastructure.setError(widget_id, 'Errors in other data, PIN neither checked nor used.') |
---|
651 | s_logger.info('"%s", "%s","entered wrong data together with pin", "%s"' % (s_id,ref,pin)) |
---|
652 | while do: |
---|
653 | ok = pins.searchAndSetRecord(pin,ref,prefix) |
---|
654 | if ok < -2 or ok > 2: |
---|
655 | err = 'Unknown error, please report!' |
---|
656 | s_logger.info('"%s","%s", "caused unknown error with pin", "%s"' % (s_id,ref,pin)) |
---|
657 | break |
---|
658 | elif ok == -2: |
---|
659 | err = 'Service already activated but with a different PIN.' |
---|
660 | s_logger.info('"%s","%s", "repeatedly activated service but with different pin", "%s"' % (s_id,ref,pin)) |
---|
661 | break |
---|
662 | elif ok == -1: |
---|
663 | err = 'Invalid PIN' |
---|
664 | s_logger.info('"%s","%s", "entered invalid pin", "%s"' % (s_id,ref,pin)) |
---|
665 | break |
---|
666 | if ok == 0: |
---|
667 | err = 'PIN already used.' |
---|
668 | s_logger.info('"%s","%s", "entered used pin", "%s"' % (s_id,ref,pin)) |
---|
669 | break |
---|
670 | if ok >= 1: |
---|
671 | #import pdb;pdb.set_trace() |
---|
672 | if self.isStudent(): |
---|
673 | if self.reference == "jamb_reg_no": |
---|
674 | err = "You are already logged in." |
---|
675 | s_logger.info('"%s","%s", "checked admission though logged in", "%s"' % (s_id,ref,pin)) |
---|
676 | break |
---|
677 | if ok == 1: |
---|
678 | s_logger.info('"%s","%s", "successfully used pin", "%s"' % (s_id,ref,pin)) |
---|
679 | else: |
---|
680 | s_logger.info('"%s","%s", "repeatedly used pin", "%s"' % (s_id,ref,pin)) |
---|
681 | break |
---|
682 | else: |
---|
683 | student = getStudentByRegNo(self,ref) |
---|
684 | s_logger.info('"%s","%s","successfully used pin","%s"' % (s_id,ref,pin)) |
---|
685 | if student is None: |
---|
686 | err = "Student not found." |
---|
687 | s_logger.info('"%s","not found in admission list"' % (ref)) |
---|
688 | break |
---|
689 | s_id = student.getId() |
---|
690 | if ok == 2: |
---|
691 | if self.reference == "jamb_reg_no": |
---|
692 | if hasattr(self.portal_directories.students,s_id): |
---|
693 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
694 | s_logger.info('"%s","%s", "repeatedly checked admission with pin", "%s"' % (s_id,ref,pin)) |
---|
695 | break |
---|
696 | else: |
---|
697 | s_logger.info('"%s","%s","(non-member) repeatedly checked admission with pin", "%s"' % (s_id,ref,pin)) |
---|
698 | else: |
---|
699 | err = "Unknown error" % s_id |
---|
700 | s_logger.info('"%s","%s", "repeatedly activated service with pin", "%s"' % (s_id,ref,pin)) |
---|
701 | break |
---|
702 | try: |
---|
703 | student.getContent().makeStudentMember(s_id,password=pin[4:]) |
---|
704 | s_logger.info('"%s","%s", "has been created using pin", "%s"' % (s_id,ref,pin)) |
---|
705 | except: |
---|
706 | err = "Please login with your Student Id %s and 10-digit PIN." % s_id |
---|
707 | s_logger.info('"%s","%s", "could not be made a member with pin", "%s"' % (s_id,ref,pin)) |
---|
708 | break |
---|
709 | break |
---|
710 | if err: |
---|
711 | datastructure.setError(widget_id, err) |
---|
712 | else: |
---|
713 | datamodel = datastructure.getDataModel() |
---|
714 | datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n) |
---|
715 | datastructure[widget_id] = ScratchCardPin(prefix,b,n) |
---|
716 | datastructure[widget_id+'_p'] = prefix |
---|
717 | datastructure[widget_id+'_b'] = b |
---|
718 | datastructure[widget_id+'_n'] = n |
---|
719 | datastructure['s_id'] = s_id |
---|
720 | return not err |
---|
721 | |
---|
722 | def render(self, mode, datastructure, **kw): ###( |
---|
723 | """Render in mode from datastructure.""" |
---|
724 | render_method = 'widget_scratch_card_pin_render' |
---|
725 | meth = getattr(self, render_method, None) |
---|
726 | if meth is None: |
---|
727 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
728 | % (render_method, self.getId())) |
---|
729 | |
---|
730 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
731 | # or create mode, because a default value has to be provided. |
---|
732 | #import pdb;pdb.set_trace() |
---|
733 | datamodel = datastructure.getDataModel() |
---|
734 | v = datamodel[self.fields[0]] |
---|
735 | if v and type(v) is StringType: |
---|
736 | try: |
---|
737 | p,b,n = v.split('-') |
---|
738 | v = ScratchCardPin(p,b,n) |
---|
739 | except ValueError: |
---|
740 | v = ScratchCardPin(self.prefix,'1',v) |
---|
741 | pass |
---|
742 | if v: |
---|
743 | prefix= '%s' % v.p |
---|
744 | b = '%s' % v.b |
---|
745 | n = '%s' % v.n |
---|
746 | else: |
---|
747 | prefix= self.prefix |
---|
748 | if prefix.startswith('@'): |
---|
749 | prefix= getattr(self,self.prefix[1:])() |
---|
750 | b = n = '' |
---|
751 | v = ScratchCardPin(prefix,b,n) |
---|
752 | widget_id = self.getWidgetId() |
---|
753 | datastructure[widget_id] = v |
---|
754 | datastructure[widget_id+'_p'] = prefix |
---|
755 | datastructure[widget_id+'_b'] = b |
---|
756 | datastructure[widget_id+'_n'] = n |
---|
757 | return meth(mode=mode, |
---|
758 | datastructure=datastructure, |
---|
759 | ) |
---|
760 | ###) |
---|
761 | |
---|
762 | |
---|
763 | InitializeClass(ScratchcardPinWidget) |
---|
764 | widgetRegistry.register(ScratchcardPinWidget) |
---|
765 | |
---|
766 | |
---|
767 | ###) |
---|
768 | |
---|
769 | class WAeUPImageWidget(CPSImageWidget): |
---|
770 | """Photo widget.""" |
---|
771 | meta_type = 'WAeUP Image Widget' |
---|
772 | |
---|
773 | def render(self, mode, datastructure, **kw): |
---|
774 | render_method = 'widget_waeup_image_render' |
---|
775 | meth = getattr(self, render_method, None) |
---|
776 | if meth is None: |
---|
777 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
778 | % (render_method, self.getId())) |
---|
779 | img_info = self.getImageInfo(datastructure) |
---|
780 | return meth(mode=mode, datastructure=datastructure, **img_info) |
---|
781 | |
---|
782 | |
---|
783 | |
---|
784 | InitializeClass(WAeUPImageWidget) |
---|
785 | |
---|
786 | widgetRegistry.register(WAeUPImageWidget) |
---|
787 | |
---|
788 | |
---|
789 | ########### |
---|
790 | |
---|