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 |
---|
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 | |
---|
145 | class WAeUPIdWidget(CPSStringWidget): ###( |
---|
146 | """ WAeUPId Widget""" |
---|
147 | meta_type = "WAeUPId Widget" |
---|
148 | |
---|
149 | def validate(self, datastructure, **kw): |
---|
150 | """Validate datastructure and update datamodel.""" |
---|
151 | |
---|
152 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
153 | id_pat_str = r"\S" |
---|
154 | inv_id_pat = compile(r"^%s$" % id_pat_str) |
---|
155 | if not valid: |
---|
156 | return 0 |
---|
157 | else: |
---|
158 | widget_id = self.getWidgetId() |
---|
159 | value = datastructure[widget_id].upper() |
---|
160 | err = 0 |
---|
161 | if len(value.split()) > 1: |
---|
162 | err = 'Invalid Id (Id contains space(s)' |
---|
163 | elif hasattr(self.aq_parent,value): |
---|
164 | err = 'An object with the Id %s already exists in this context' % (value) |
---|
165 | if err: |
---|
166 | datastructure.setError(widget_id, err) |
---|
167 | else: |
---|
168 | datamodel = datastructure.getDataModel() |
---|
169 | datamodel[self.fields[0]] = value |
---|
170 | |
---|
171 | return not err |
---|
172 | |
---|
173 | InitializeClass(WAeUPIdWidget) |
---|
174 | |
---|
175 | widgetRegistry.register(WAeUPIdWidget) |
---|
176 | |
---|
177 | |
---|
178 | ###) |
---|
179 | |
---|
180 | class StudentIdWidget(CPSStringWidget): ###( |
---|
181 | """ StudentId Widget""" |
---|
182 | meta_type = "StudentId Widget" |
---|
183 | digits = 8 |
---|
184 | digits_str = "N"*digits |
---|
185 | letters = 2 |
---|
186 | letters_str = "L"*letters |
---|
187 | |
---|
188 | def validate(self, datastructure, **kw): |
---|
189 | """Validate datastructure and update datamodel.""" |
---|
190 | |
---|
191 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
192 | if not valid: |
---|
193 | return 0 |
---|
194 | else: |
---|
195 | widget_id = self.getWidgetId() |
---|
196 | value = datastructure[widget_id] |
---|
197 | err = 0 |
---|
198 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
199 | err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
200 | if err: |
---|
201 | datastructure.setError(widget_id, err) |
---|
202 | else: |
---|
203 | datamodel = datastructure.getDataModel() |
---|
204 | datamodel[self.fields[0]] = value |
---|
205 | |
---|
206 | return not err |
---|
207 | |
---|
208 | InitializeClass(StudentIdWidget) |
---|
209 | widgetRegistry.register(StudentIdWidget) |
---|
210 | |
---|
211 | ###) |
---|
212 | |
---|
213 | ##def getStudentByRegNo(self,reg_no): |
---|
214 | ## search = self.portal_catalog({'meta_type': 'StudentApplication', |
---|
215 | ## 'jamb_reg_no': reg_no, |
---|
216 | ## }) |
---|
217 | ## if len(search) < 1: |
---|
218 | ## return None |
---|
219 | ## return search[0].getObject(). |
---|
220 | |
---|
221 | class JambRegNoWidget(CPSStringWidget): ###( |
---|
222 | """ JambRegNo Widget""" |
---|
223 | meta_type = "JambRegNo Widget" |
---|
224 | digits = 8 |
---|
225 | digits_str = "N"*digits |
---|
226 | letters = 2 |
---|
227 | letters_str = "L"*letters |
---|
228 | |
---|
229 | def validate(self, datastructure, **kw): |
---|
230 | """Validate datastructure and update datamodel.""" |
---|
231 | |
---|
232 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
233 | if not valid: |
---|
234 | return 0 |
---|
235 | else: |
---|
236 | widget_id = self.getWidgetId() |
---|
237 | value = datastructure[widget_id] |
---|
238 | err = 0 |
---|
239 | if not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()): |
---|
240 | err = 'Invalid Registration Number in the format: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str) |
---|
241 | else: |
---|
242 | s = getStudentByRegNo(self,value) |
---|
243 | if s is None: |
---|
244 | err = 'No JAMB data for %s' % (value) |
---|
245 | if err: |
---|
246 | datastructure.setError(widget_id, err) |
---|
247 | else: |
---|
248 | datamodel = datastructure.getDataModel() |
---|
249 | datamodel[self.fields[0]] = value |
---|
250 | |
---|
251 | return not err |
---|
252 | |
---|
253 | InitializeClass(JambRegNoWidget) |
---|
254 | |
---|
255 | widgetRegistry.register(JambRegNoWidget) |
---|
256 | ###) |
---|
257 | |
---|
258 | class ScratchCardPin: ###( |
---|
259 | """the ScratchCardPin""" |
---|
260 | def __init__(self,prefix,batch_no,number): |
---|
261 | self.p = prefix |
---|
262 | self.b = batch_no |
---|
263 | self.n = number |
---|
264 | |
---|
265 | def __str__(self): |
---|
266 | return "%s-%s-%s" % (self.p,self.b,self.n) |
---|
267 | ###) |
---|
268 | |
---|
269 | class ScratchcardPinWidget(CPSStringWidget): ###( |
---|
270 | """ ScratchcardPin Widget""" |
---|
271 | meta_type = "Scratchcard Pin Widget" |
---|
272 | _properties = CPSWidget._properties + ( |
---|
273 | {'id': 'prefix', 'type': 'string', 'mode': 'w', |
---|
274 | 'label': 'Prefix'}, |
---|
275 | {'id': 'reference', 'type': 'string', 'mode': 'w', |
---|
276 | 'label': 'Reference Field'}, |
---|
277 | |
---|
278 | ) |
---|
279 | valid_pins = ['12345678', |
---|
280 | '23456789', |
---|
281 | '34567890', |
---|
282 | '45678901', |
---|
283 | ] |
---|
284 | prefix = '' |
---|
285 | reference = '' |
---|
286 | |
---|
287 | def prepare(self, datastructure, **kw): ###( |
---|
288 | """Prepare datastructure from datamodel.""" |
---|
289 | datamodel = datastructure.getDataModel() |
---|
290 | v = datamodel[self.fields[0]] |
---|
291 | widget_id = self.getWidgetId() |
---|
292 | if v and type(v) is StringType: |
---|
293 | p,b,n = v.split('-') |
---|
294 | v = ScratchCardPin(v,b,n) |
---|
295 | if v: |
---|
296 | b = '%s' % v.b |
---|
297 | n = '%s' % v.n |
---|
298 | else: |
---|
299 | b = n = '' |
---|
300 | datastructure[widget_id] = v |
---|
301 | datastructure[widget_id+'_b'] = b |
---|
302 | datastructure[widget_id+'_n'] = n |
---|
303 | |
---|
304 | ###) |
---|
305 | |
---|
306 | def validate(self, datastructure, **kw): |
---|
307 | """Validate datastructure and update datamodel.""" |
---|
308 | widget_id = self.getWidgetId() |
---|
309 | v = datastructure[widget_id] |
---|
310 | err = 0 |
---|
311 | p = self.prefix |
---|
312 | b = datastructure[widget_id+'_b'].strip() |
---|
313 | n = datastructure[widget_id+'_n'].strip() |
---|
314 | pins = self.portal_pins |
---|
315 | pin = "%(p)s%(b)s%(n)s" % vars() |
---|
316 | ref = datastructure[self.reference] |
---|
317 | #import pdb;pdb.set_trace() |
---|
318 | ok = pins.searchAndSetRecord(pin,ref) |
---|
319 | while 1: |
---|
320 | if ok == -1: |
---|
321 | err = 'invalid Pin' |
---|
322 | break |
---|
323 | if ok == 0: |
---|
324 | err = 'Pin already used' |
---|
325 | break |
---|
326 | if ok >= 1: |
---|
327 | student = getStudentByRegNo(self,ref) |
---|
328 | if student is None: |
---|
329 | err = "Student not Found in validate SC" |
---|
330 | break |
---|
331 | s_id = student.getId() |
---|
332 | if self.portal_workflow.getInfoFor(student, |
---|
333 | 'review_state', |
---|
334 | None) =="created": |
---|
335 | student.content_status_modify(workflow_action="enter_application_pin") |
---|
336 | if ok == 2: |
---|
337 | break |
---|
338 | student.getContent().makeStudentMember(s_id,password=pin[4:]) |
---|
339 | break |
---|
340 | if err: |
---|
341 | datastructure.setError(widget_id, err) |
---|
342 | else: |
---|
343 | datamodel = datastructure.getDataModel() |
---|
344 | datamodel[self.fields[0]] = ScratchCardPin(self.prefix,b,n) |
---|
345 | datastructure[widget_id] = ScratchCardPin(self.prefix,b,n) |
---|
346 | datastructure[widget_id+'_b'] = b |
---|
347 | datastructure[widget_id+'_n'] = n |
---|
348 | datastructure['s_id'] = s_id |
---|
349 | #import pdb;pdb.set_trace() |
---|
350 | return not err |
---|
351 | |
---|
352 | def render(self, mode, datastructure, **kw): ###( |
---|
353 | """Render in mode from datastructure.""" |
---|
354 | render_method = 'widget_scratch_card_pin_render' |
---|
355 | meth = getattr(self, render_method, None) |
---|
356 | if meth is None: |
---|
357 | raise RuntimeError("Unknown Render Method %s for widget type %s" |
---|
358 | % (render_method, self.getId())) |
---|
359 | |
---|
360 | # XXX AT: datastructure has to be set again here, in case we're in edit |
---|
361 | # or create mode, because a default value has to be provided. |
---|
362 | #import pdb;pdb.set_trace() |
---|
363 | datamodel = datastructure.getDataModel() |
---|
364 | v = datamodel[self.fields[0]] |
---|
365 | if v and type(v) is StringType: |
---|
366 | p,b,n = v.split('-') |
---|
367 | v = ScratchCardPin(v,b,n) |
---|
368 | if v: |
---|
369 | b = '%s' % v.b |
---|
370 | n = '%s' % v.n |
---|
371 | else: |
---|
372 | b = n = '' |
---|
373 | if mode in ['edit', 'create']: |
---|
374 | widget_id = self.getWidgetId() |
---|
375 | datastructure[widget_id] = v |
---|
376 | datastructure[widget_id+'_b'] = b |
---|
377 | datastructure[widget_id+'_n'] = n |
---|
378 | |
---|
379 | ###) |
---|
380 | |
---|
381 | |
---|
382 | return meth(mode=mode, |
---|
383 | datastructure=datastructure, |
---|
384 | prefix=self.prefix, |
---|
385 | value=v, |
---|
386 | ) |
---|
387 | |
---|
388 | |
---|
389 | InitializeClass(ScratchcardPinWidget) |
---|
390 | widgetRegistry.register(ScratchcardPinWidget) |
---|
391 | |
---|
392 | |
---|
393 | ###) |
---|
394 | |
---|
395 | ########### |
---|
396 | |
---|