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