source: WAeUP_SRP/trunk/Widgets.py @ 1949

Last change on this file since 1949 was 1915, checked in by Henrik Bettermann, 18 years ago

show widgets in alphabetical order

  • Property svn:keywords set to Id
File size: 43.6 KB
RevLine 
[47]1#-*- mode: python; mode: fold -*-
[990]2# $Id: Widgets.py 1915 2007-06-17 16:43:57Z henrik $
[295]3from cgi import escape
[502]4from types import *
[22]5from Globals import InitializeClass
[199]6##from Products.CPSSchemas.Widget import CPSWidgetType
[295]7from Products.CMFCore.utils import getToolByName
[1772]8from Products.CPSSchemas.BasicWidgets import CPSBooleanWidget, CPSWidget, CPSStringWidget, CPSEmailWidget,CPSImageWidget
[295]9from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget
[22]10from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget
[199]11from Products.CPSSchemas.Widget import widgetRegistry
12##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry
[22]13from DateTime.DateTime import DateTime
14from AccessControl import getSecurityManager
[502]15from Products.WAeUP_SRP.Students import getStudentByRegNo
[1747]16from Products.WAeUP_SRP.Academics import makeCertificateCode
[22]17from re import compile
[952]18import logging
[1915]19import operator
[22]20
[952]21#from zLOG import LOG, DEBUG
[22]22
[295]23class CPSSelectWidgetForRecord(CPSSelectWidget): ###(
24    """Select widget. with record names"""
25    meta_type = 'Select Widget for Records'
26
27    field_types = ('CPS String Field',)
28    field_inits = ({'is_searchabletext': 1,},)
29
30    _properties = CPSSelectWidget._properties + (
31        {'id': 'record_id', 'type': 'string', 'mode': 'w',
32         'label': 'Record Id', 'is_required' : 1},
33        )
34
35    def render(self, mode, datastructure, **kw):
36        """Render in mode from datastructure."""
37        value = datastructure[self.getWidgetId()]
38        vocabulary = self._getVocabulary(datastructure)
39        portal = getToolByName(self, 'portal_url').getPortalObject()
40        cpsmcat = portal.translation_service
41        if mode == 'view':
42            if self.translated:
43                return escape(cpsmcat(vocabulary.getMsgid(value, value)).encode('ISO-8859-15', 'ignore'))
44            else:
45                return escape(vocabulary.get(value, value))
46        elif mode == 'edit':
47            html_widget_id = self.getHtmlWidgetId()
[444]48            res = renderHtmlTag('select',
49                                name='%s.%s:records' % (self.record_id,html_widget_id),
[295]50                                id=html_widget_id)
51            in_selection = 0
52            for k, v in vocabulary.items():
53                if self.translated:
54                    kw = {'value': k,
55                          'contents': cpsmcat(vocabulary.getMsgid(k, k)).encode('ISO-8859-15', 'ignore')
56                          }
57                else:
58                    kw = {'value': k, 'contents': v}
59                if value == k:
60                    kw['selected'] = 'selected'
61                    in_selection = 1
62                res += renderHtmlTag('option', **kw)
63            if value and not in_selection:
64                kw = {'value': value, 'contents': 'invalid: '+ str(value),
65                      'selected': 'selected'}
66                res += renderHtmlTag('option', **kw)
67            res += '</select>'
68            return res
69        raise RuntimeError('unknown mode %s' % mode)
70
71InitializeClass(CPSSelectWidgetForRecord)
72
73widgetRegistry.register(CPSSelectWidgetForRecord)
74
75###)
76
[373]77class CPSStringWidgetForRecord(CPSStringWidget): ###(
[295]78    """String widget."""
79    meta_type = 'String Widget For Record'
80
81    field_types = ('CPS String Field',)
82    field_inits = ({'is_searchabletext': 1,},)
83    _properties = CPSStringWidget._properties + (
84        {'id': 'record_id', 'type': 'string', 'mode': 'w',
85         'label': 'Record Id', 'is_required' : 1},
86        )
87
88    def render(self, mode, datastructure, **kw):
89        """Render in mode from datastructure."""
90        value = datastructure[self.getWidgetId()]
91        if mode == 'view':
92            return escape(value)
93        elif mode == 'edit':
94            # XXX TODO should use an other name than kw !
95            # XXX change this everywhere
96            html_widget_id = self.getHtmlWidgetId()
97            kw = {'type': 'text',
98                  'id'  : html_widget_id,
[444]99                  'name': '%s.%s:records' % (self.record_id,html_widget_id),
[295]100                  'value': escape(value),
101                  'size': self.display_width,
102                  }
103            if self.size_max:
104                kw['maxlength'] = self.size_max
105            return renderHtmlTag('input', **kw)
106        raise RuntimeError('unknown mode %s' % mode)
107
108InitializeClass(CPSStringWidgetForRecord)
109
110widgetRegistry.register(CPSStringWidgetForRecord)
111
[373]112###)
113
114class CertificateCourseIdWidget(CPSStringWidget): ###(
115    """ CertificateCourseId Widget"""
116    meta_type = "CertificateCourseId Widget"
[444]117
[373]118    def validate(self, datastructure, **kw):
119        """Validate datastructure and update datamodel."""
[444]120
[373]121        valid = CPSStringWidget.validate(self, datastructure, **kw)
122        if not valid:
123            return 0
124        else:
125            widget_id = self.getWidgetId()
126            value = datastructure[widget_id].upper()
127            err = 0
128            c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})]
[381]129            if hasattr(self.aq_parent,value):
[1891]130                err = 'Course already exists'
[381]131            elif value not in c_ids:
[1891]132                err = 'Course does not exist'
[373]133            if err:
134                datastructure.setError(widget_id, err)
135            else:
136                datamodel = datastructure.getDataModel()
137                datamodel[self.fields[0]] = value
[444]138
[373]139            return not err
140
141InitializeClass(CertificateCourseIdWidget)
142
143widgetRegistry.register(CertificateCourseIdWidget)
[551]144###)
[373]145
[551]146class CourseIdWidget(CPSStringWidget): ###(
147    """ CourseId Widget"""
148    meta_type = "CourseId Widget"
149
150    def validate(self, datastructure, **kw):
151        """Validate datastructure and update datamodel."""
152
153        valid = CPSStringWidget.validate(self, datastructure, **kw)
154        if not valid:
155            return 0
156        else:
157            widget_id = self.getWidgetId()
158            value = datastructure[widget_id].upper()
159            err = 0
160            res = self.portal_catalog(meta_type= "Course",id = value)
161            if len(res) > 0:
[1891]162                err = 'Course already exists'
[551]163            if err:
164                datastructure.setError(widget_id, err)
165            else:
166                datamodel = datastructure.getDataModel()
167                datamodel[self.fields[0]] = value
168
169            return not err
170
171InitializeClass(CourseIdWidget)
172
173widgetRegistry.register(CourseIdWidget)
174
175
[373]176###)
177
[1820]178class WAeUPStudyModeWidget(CPSSelectWidget): ###(
179    """WAeUP StudyMode widget."""
180    meta_type = 'WAeUP StudyMode Widget'
181    vocabulary = 'entry_modes'
182
183    def _getStudyModes(self):
184        voc = getattr(self.portal_vocabularies,self.vocabulary)
185        d = {}
186        for k,v in voc.items():
187            d[k] = v
188        return d
189
190    def validate(self, datastructure, **kw):
191        """Validate datastructure and update datamodel."""
192        widget_id = self.getWidgetId()
193        value = datastructure[widget_id]
194        try:
195            v = str(value)
196        except ValueError:
197            datastructure.setError(widget_id, "'%s' not a valid session key" % value)
198            return 0
199        studymodes = self._getStudyModes()
200        if not value:
[1836]201            v = value = 'ume_ft'
[1820]202        #import pdb;pdb.set_trace()
203        if not studymodes.has_key(value):
204            datastructure.setError(widget_id, "'%s' not a valid session key" % v)
205            return 0
206        if self.is_required and not len(v):
207            datastructure.setError(widget_id, "session key required")
208            return 0
209
210        datamodel = datastructure.getDataModel()
211        datamodel[self.fields[0]] = v
212        return 1
213
214    def render(self, mode, datastructure, **kw):
215        """Render in mode from datastructure."""
216        value = datastructure[self.getWidgetId()]
217        studymodes = self._getStudyModes()
218        if mode == 'view':
219            return escape(studymodes.get(value, value))
220        elif mode == 'edit':
221            html_widget_id = self.getHtmlWidgetId()
222            res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id)
223            in_selection = 0
224            vocabulary_items = studymodes.items()
225            if self.sorted:
226                vocabulary_items.sort(key=operator.itemgetter(1))
227            for k, v in vocabulary_items:
228                kw = {'value': k, 'contents': v}
229                if value == k:
230                    kw['selected'] = 'selected'
231                    in_selection = 1
232                res += renderHtmlTag('option', **kw)
233            if value and not in_selection:
234                kw = {'value': value, 'contents': 'invalid: '+ str(value),
235                      'selected': 'selected'}
236                res += renderHtmlTag('option', **kw)
237            res += '</select>'
238            return res
239        raise RuntimeError('unknown mode %s' % mode)
240
241InitializeClass(WAeUPStudyModeWidget)
242
243widgetRegistry.register(WAeUPStudyModeWidget)
244
245###)
246
[1804]247class WAeUPSessionWidget(CPSSelectWidget): ###(
248    """WAeUP Session widget."""
249    meta_type = 'WAeUP Session Widget'
250
251    def _getSessions(self):
252        current_year = DateTime().year()
253        d = {'xx': 'unknown'}
254        for y in range(current_year - 9,current_year + 1):
255            d['%s' % str(y)[-2:]] = '%4d/%4d' % (y,y+1)
256        return d
257
258    def validate(self, datastructure, **kw):
259        """Validate datastructure and update datamodel."""
260        widget_id = self.getWidgetId()
261        value = datastructure[widget_id]
262        try:
263            v = str(value)
264        except ValueError:
265            datastructure.setError(widget_id, "'%s' not a valid session key" % value)
266            return 0
[1805]267
[1804]268        if len(v) == 1:
269            v = value = '0%c' % v
270        elif not value:
271            v = value = self.getSessionId()[-2:]
272        #import pdb;pdb.set_trace()
273        sessions = self._getSessions()
274        if not sessions.has_key(value):
275            datastructure.setError(widget_id, "'%s' not a valid session key" % v)
276            return 0
277        if self.is_required and not len(v):
278            datastructure.setError(widget_id, "session key required")
279            return 0
280
281        datamodel = datastructure.getDataModel()
282        datamodel[self.fields[0]] = v
283        return 1
284
285    def render(self, mode, datastructure, **kw):
286        """Render in mode from datastructure."""
287        value = datastructure[self.getWidgetId()]
288        sessions = self._getSessions()
289        if mode == 'view':
290            return escape(sessions.get(value, value))
291        elif mode == 'edit':
292            html_widget_id = self.getHtmlWidgetId()
293            res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id)
294            in_selection = 0
295            vocabulary_items = sessions.items()
296            if self.sorted:
297                vocabulary_items.sort(key=operator.itemgetter(1))
298            for k, v in vocabulary_items:
299                kw = {'value': k, 'contents': v}
300                if value == k:
301                    kw['selected'] = 'selected'
302                    in_selection = 1
303                res += renderHtmlTag('option', **kw)
304            if value and not in_selection:
305                kw = {'value': value, 'contents': 'invalid: '+ str(value),
306                      'selected': 'selected'}
307                res += renderHtmlTag('option', **kw)
308            res += '</select>'
309            return res
310        raise RuntimeError('unknown mode %s' % mode)
311
312InitializeClass(WAeUPSessionWidget)
313
314widgetRegistry.register(WAeUPSessionWidget)
315
316###)
317
318class WAeUPLevelWidget(CPSSelectWidget): ###(
319    """WAeUP Level Widget."""
320    meta_type = 'WAeUP Level Widget'
321
322    def _getLevels(self):
323        d = {'999':'unknown'}
324        for y in range(100,800,100):
325            d['%s' % str(y)] = 'Year %1d (%3d Level)' % (y/100,y)
326        return d
327
328    def validate(self, datastructure, **kw):
329        """Validate datastructure and update datamodel."""
330        widget_id = self.getWidgetId()
331        value = datastructure[widget_id]
332        try:
333            v = str(value)
334        except ValueError:
335            datastructure.setError(widget_id, "'%s' not a valid level key" % value)
336            return 0
[1805]337
338        if not value:
[1804]339            v = value = '100'
340        #import pdb;pdb.set_trace()
341        levels = self._getLevels()
342        if not levels.has_key(value):
343            datastructure.setError(widget_id, "'%s' not a valid level" % v)
344            return 0
345        if self.is_required and not len(v):
346            datastructure.setError(widget_id, "level key required")
347            return 0
348
349        datamodel = datastructure.getDataModel()
350        datamodel[self.fields[0]] = v
351        return 1
352
353    def render(self, mode, datastructure, **kw):
354        """Render in mode from datastructure."""
355        value = datastructure[self.getWidgetId()]
356        levels = self._getLevels()
357        if mode == 'view':
358            return escape(levels.get(value, value))
359        elif mode == 'edit':
360            html_widget_id = self.getHtmlWidgetId()
361            res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id)
362            in_selection = 0
363            vocabulary_items = levels.items()
364            if self.sorted:
365                vocabulary_items.sort(key=operator.itemgetter(1))
366            for k, v in vocabulary_items:
367                kw = {'value': k, 'contents': v}
368                if value == k:
369                    kw['selected'] = 'selected'
370                    in_selection = 1
371                res += renderHtmlTag('option', **kw)
372            if value and not in_selection:
373                kw = {'value': value, 'contents': 'invalid: '+ str(value),
374                      'selected': 'selected'}
375                res += renderHtmlTag('option', **kw)
376            res += '</select>'
377            return res
378        raise RuntimeError('unknown mode %s' % mode)
379
380InitializeClass(WAeUPLevelWidget)
381
382widgetRegistry.register(WAeUPLevelWidget)
383
384###)
385
386class WAeUPVerdictWidget(CPSSelectWidget): ###(
387    """WAeUP Verdict widget."""
388    meta_type = 'WAeUP Verdict Widget'
389
390    # XXX make a menu for the vocabulary.
391    vocabulary = 'verdicts'
392
393    # Associating the widget label with an input area to improve the widget
394    # accessibility.
395    has_input_area = True
396
397    def _getVerdicts(self,datastructure):
398        voc = getattr(self.portal_vocabularies,self.vocabulary)
399        d = {}
400        for k,v in voc.items():
401            d[k] = v
402        return d
403
404    def validate(self, datastructure, **kw):
405        """Validate datastructure and update datamodel."""
406        widget_id = self.getWidgetId()
407        value = datastructure[widget_id]
408        try:
409            v = str(value)
410        except ValueError:
411            datastructure.setError(widget_id, "'%s' not a valid verdict key" % value)
412            return 0
413        #import pdb;pdb.set_trace()
414        verdicts = self._getVerdicts(datastructure)
415        if not value:
416            v = value = verdicts['N/A']
417        if not verdicts.has_key(value):
418            datastructure.setError(widget_id, "'%s' not a valid verdict key" % v)
419            return 0
420        if self.is_required and not len(v):
421            datastructure.setError(widget_id, "verdict required")
422            return 0
423
424        datamodel = datastructure.getDataModel()
425        datamodel[self.fields[0]] = v
426        return 1
427
428    def render(self, mode, datastructure, **kw):
429        """Render in mode from datastructure."""
430        value = datastructure[self.getWidgetId()]
431        verdicts = self._getVerdicts(datastructure)
432        if mode == 'view':
433            return escape(verdicts.get(value, value))
434        elif mode == 'edit':
435            html_widget_id = self.getHtmlWidgetId()
436            res = renderHtmlTag('select', name=html_widget_id, id=html_widget_id)
437            in_selection = 0
438            vocabulary_items = verdicts.items()
439            if self.sorted:
440                vocabulary_items.sort(key=operator.itemgetter(1))
441            for k, v in vocabulary_items:
442                kw = {'value': k, 'contents': v}
443                if value == k:
444                    kw['selected'] = 'selected'
445                    in_selection = 1
446                res += renderHtmlTag('option', **kw)
447            if value and not in_selection:
448                kw = {'value': value, 'contents': 'invalid: '+ str(value),
449                      'selected': 'selected'}
450                res += renderHtmlTag('option', **kw)
451            res += '</select>'
452            return res
453        raise RuntimeError('unknown mode %s' % mode)
454
455InitializeClass(WAeUPVerdictWidget)
456
457widgetRegistry.register(WAeUPVerdictWidget)
458
459###)
460
[714]461class WAeUPReservedRoomsWidget(CPSStringWidget): ###(
462    """ WAeUPReservedRooms Widget"""
463    meta_type = "WAeUPReservedRooms Widget"
464
465    def validate(self, datastructure, **kw):
466        """Validate datastructure and update datamodel."""
467        import re
468        valid = CPSStringWidget.validate(self, datastructure, **kw)
469        if not valid:
470            return 0
471        else:
472            widget_id = self.getWidgetId()
473            value = datastructure[widget_id]
474            err = 0
475            try:
[926]476                reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',value)
[714]477                                     if r]
[1038]478            except (ValueError,IndexError),msg:
[714]479                err = str(msg)
480            if err:
481                datastructure.setError(widget_id, err)
482            else:
483                datamodel = datastructure.getDataModel()
484                datamodel[self.fields[0]] = value
485            return not err
486
487InitializeClass(WAeUPReservedRoomsWidget)
488
489widgetRegistry.register(WAeUPReservedRoomsWidget)
490###)
491
[388]492class WAeUPIdWidget(CPSStringWidget): ###(
493    """ WAeUPId Widget"""
494    meta_type = "WAeUPId Widget"
[444]495
[388]496    def validate(self, datastructure, **kw):
497        """Validate datastructure and update datamodel."""
[444]498
[388]499        valid = CPSStringWidget.validate(self, datastructure, **kw)
[422]500        id_pat_str = r"\S"
501        inv_id_pat = compile(r"^%s$" % id_pat_str)
[388]502        if not valid:
503            return 0
504        else:
[586]505            portal_type_query = {'query':['Faculty',
506                                          'Department',
507                                          'Course',
508                                          'Certificate',
509                                          'CertificateCourse',]}
[388]510            widget_id = self.getWidgetId()
511            value = datastructure[widget_id].upper()
512            err = 0
[1907]513            mapping = {}
[440]514            if len(value.split()) > 1:
[783]515                err = 'Invalid Id, Id contains space(s).'
[586]516            elif self.portal_catalog(portal_type=portal_type_query,id=value):
[1718]517                brain = self.portal_catalog(portal_type=portal_type_query,id=value)[0]
[1907]518                err = 'An ${portal_type} object with the Id ${id} already exists at ${path}.'
519                mapping = {'portal_type': brain.portal_type,
520                           'id': value,
521                           'path': brain.getPath(),
522                           }
[388]523            if err:
[1907]524                datastructure.setError(widget_id, err, mapping)
[388]525            else:
526                datamodel = datastructure.getDataModel()
527                datamodel[self.fields[0]] = value
[444]528
[388]529            return not err
530
531InitializeClass(WAeUPIdWidget)
532
533widgetRegistry.register(WAeUPIdWidget)
534
535
536###)
537
[1025]538class StudyCourseWidget(CPSStringWidget): ###(
539    """ StudyCourse Widget"""
540    meta_type = "StudyCourse Widget"
541
542    def validate(self, datastructure, **kw):
543        """Validate datastructure and update datamodel."""
544        #from Products.zdb import set_trace
545        #set_trace()
[1747]546##        valid = CPSStringWidget.validate(self, datastructure, **kw)
547##        if not valid:
548##            return 0
549        widget_id = self.getWidgetId()
550        value = makeCertificateCode(datastructure[widget_id]).upper()
[1025]551        id_pat_str = r"\S"
552        inv_id_pat = compile(r"^%s$" % id_pat_str)
[1747]553        err = 0
554        if len(value.split()) > 1:
555            err = 'Invalid Id, Id contains space(s).'
556        elif not self.portal_catalog(portal_type='Certificate',id=value):
557            err = 'No such certificate'
558        if err:
559            datastructure.setError(widget_id, err)
[1025]560        else:
[1747]561            datamodel = datastructure.getDataModel()
562            datamodel[self.fields[0]] = value
563        return not err
[1025]564
565InitializeClass(StudyCourseWidget)
566
567widgetRegistry.register(StudyCourseWidget)
568###)
569
[463]570class JambRegNoWidget(CPSStringWidget): ###(
571    """ JambRegNo Widget"""
572    meta_type = "JambRegNo Widget"
[1169]573    _properties = CPSStringWidget._properties + (
574        {'id': 'catalog', 'type': 'string', 'mode': 'w',
575         'label': 'Catalog to search'},
576         {'id': 'reference', 'type': 'string', 'mode': 'w',
577         'label': 'Reference Field'},
578         )
579    catalog = "portal_pumeresults" #the catalog to search for jamb_reg_no
580    reference = ""
[463]581    digits = 8
582    digits_str = "N"*digits
583    letters = 2
584    letters_str = "L"*letters
585    def validate(self, datastructure, **kw):
586        """Validate datastructure and update datamodel."""
587        valid = CPSStringWidget.validate(self, datastructure, **kw)
[1169]588        jamb_nr_catalog = getattr(self,self.catalog)
589        widget_id = self.getWidgetId()
590        value = datastructure[widget_id].upper()
591        err = 0
592        if not valid: #or not (len(value) == self.digits + self.letters and value[:self.digits].isdigit() and value[self.digits:].isalpha()):
593            #err = 'Invalid number, the expected format is: %s%s with N = Number, L = Letter' % (self.digits_str,self.letters_str)
[1793]594            err = 'Invalid number'
[1169]595        elif self.reference == '':
596            #s = getStudentByRegNo(self,value)
597            pume = jamb_nr_catalog(jamb_reg_no = value)
598            if len(pume) < 1:
[1891]599                err = 'No student record with this registration number.'
[1169]600            else:
601                datastructure['pume'] = pume[0]
[463]602        else:
[1169]603            #import pdb;pdb.set_trace()
604            record = datastructure[self.reference]
605            #jamb_reg_no = getattr(record,widget_id)
606            jamb_reg_no = record.Entryregno
607            if jamb_reg_no != value:
[1783]608                err = 'Registration number does not match.'
[1169]609        if err:
610            datastructure.setError(widget_id, err)
611        else:
612            datamodel = datastructure.getDataModel()
613            datamodel[self.fields[0]] = value
614        return not err
[463]615
616InitializeClass(JambRegNoWidget)
617
618widgetRegistry.register(JambRegNoWidget)
[47]619###)
620
[1175]621class SecretWidget(CPSStringWidget): ###(
622    """ Secret Widget"""
623    meta_type = "Secret Widget"
624    _properties = CPSStringWidget._properties + (
625        {'id': 'reference', 'type': 'string', 'mode': 'w',
626         'label': 'Reference Record'},
627         {'id': 'check_fields', 'type': 'tokens', 'mode': 'w',
628         'label': 'Fields to check'},
629         )
630    reference = "student"
[1747]631    matric_no_catalog = 'returning_import'
[1175]632    check_fields = ("Firstname", "Middlename","Lastname")
633    def validate(self, datastructure, **kw):
634        """Validate datastructure and update datamodel."""
[1571]635        logger = logging.getLogger('Widgets.SecretWidget.validate')
[1175]636        valid = CPSStringWidget.validate(self, datastructure, **kw)
637        widget_id = self.getWidgetId()
638        value = datastructure[widget_id].upper()
639        err = 0
[1189]640        record = datastructure.get(self.reference,None)
[1747]641        #import pdb;pdb.set_trace()
[1379]642        if not valid or len(value) < 2:
[1793]643            err = 'Invalid string'
[1189]644        elif not record or datastructure.errors:
[1177]645            err = 0
[1175]646        else:
647            found = False
648            cvs = []
649            for field in self.check_fields:
650                cv = getattr(record,field).upper()
[1243]651                if len(cv.split()) > 1:
652                    for splited in cv.split():
[1747]653                        cvs.append(splited.strip())
[1243]654                else:
655                    cvs.append(cv)
[1747]656            for cv in cvs:
[1175]657                if cv  == value.upper():
658                    found = True
659                    break
660            matric_no = record.matric_no
661            name = " ".join(cvs)
662            if not found:
[1573]663                logger.info('%(matric_no)s did not find %(value)s in %(name)s' % vars())
664                err = 'No name does match.'
[1175]665            else:
[1571]666                logger.info('%(matric_no)s found %(value)s in %(name)s' % vars())
[1175]667        if err:
668            datastructure.setError(widget_id, err)
669        else:
670            datamodel = datastructure.getDataModel()
671            datamodel[self.fields[0]] = value
672        return not err
673
674InitializeClass(SecretWidget)
675
676widgetRegistry.register(SecretWidget)
677###)
678
[1804]679class WAeUPSexWidget(CPSBooleanWidget): ###(
[1772]680    """WAeUP sex widget."""
681    meta_type = 'WAeUP Sex Widget'
682
683    def validate(self, datastructure, **kw):
684        """Validate datastructure and update datamodel."""
685        value = datastructure[self.getWidgetId()]
686
687        if self.render_format not in self.render_formats:
688            self.render_format = 'select'
689
690        female = value in ('F','f','Female','female',"True",True)
691        male = value in ('M','m','Male','male','False',False)
692        if not female and not male:
693            datastructure.setError(self.getWidgetId(),
694                                   "invalid sex %s" % value)
695            return 0
696        elif female:
697            v = True
698        else:
699            v = False
700        datamodel = datastructure.getDataModel()
701        datamodel[self.fields[0]] = v
702        return 1
703
704InitializeClass(WAeUPSexWidget)
705
706widgetRegistry.register(WAeUPSexWidget)
707
[1804]708###)
709
[1146]710class MatricNoWidget(CPSStringWidget): ###(
711    """ MatricNo Widget"""
712    meta_type = "MatricNo Widget"
[1747]713
714    _properties = CPSStringWidget._properties + (
715        {'id': 'matric_no_catalog', 'type': 'string', 'mode': 'w',
716         'label': 'Catalog to search for MatricNo'},
717        { 'id': 'results_catalog', 'type': 'string', 'mode': 'w',
718         'label': 'Results Catalog'},
719         )
720    matric_no_catalog = "" #the catalog to search for matric_no
[1748]721    results_catalog = "results_import" #results catalog
[1747]722
[1146]723    def validate(self, datastructure, **kw):
724        """Validate datastructure and update datamodel."""
[1747]725        #import pdb;pdb.set_trace()
[1146]726        valid = CPSStringWidget.validate(self, datastructure, **kw)
[1571]727        logger = logging.getLogger('Widgets.MatricNoWidget.validate')
[1747]728        returning = getattr(self,self.matric_no_catalog)
729        results = getattr(self,self.results_catalog,None)
[1146]730        err = 0
[1189]731        widget_id = self.getWidgetId()
732        value = datastructure[widget_id]
733        if not valid or not value:
[1891]734            err = 'Invalid string'
[1573]735            logger.info('Invalid matric_no string %s' % value)
[1146]736        else:
[1189]737            value = value.upper()
[1177]738            datastructure['student'] = None
[1146]739            while not err:
[1151]740                res = returning(matric_no = value)
[1146]741                if len(res) < 1:
[1573]742                    logger.info('matric_no %s not found' % value)
[1915]743                    err = 'No student with this matriculation number.'
[1747]744                    break
[1146]745                datastructure['student'] = res[0]
[1747]746                if results is not None:
747                    res = results(matric_no = value)
748                    if len(res) < 1:
[1891]749                        err = 'No results for this matriculation number'
[1747]750                        continue
751                    datastructure['results'] = res
[1146]752                break
[1189]753        if err:
754            datastructure.setError(widget_id, err)
755        else:
756            datamodel = datastructure.getDataModel()
757            datamodel[self.fields[0]] = value
758        return not err
[1146]759
760InitializeClass(MatricNoWidget)
761
762widgetRegistry.register(MatricNoWidget)
763###)
764
[1393]765class StudentIdWidget(CPSStringWidget): ###(
766    """ StudentId Widget"""
767    meta_type = "StudentId Widget"
768    def validate(self, datastructure, **kw):
769        """Validate datastructure and update datamodel."""
770        valid = CPSStringWidget.validate(self, datastructure, **kw)
[1571]771        logger = logging.getLogger('Widgets.StudentIdWidget.validate')
[1393]772        #import pdb;pdb.set_trace()
773        s_cat = self.students_catalog
774        err = 0
775        widget_id = self.getWidgetId()
776        value = datastructure[widget_id]
777        if not valid or not value:
[1891]778            err = 'Invalid Id string'
[1573]779            logger.info('Invalid id string %s' % value)
[1449]780            datastructure['student'] = None
[1393]781        else:
782            value = value.upper()
783            res = s_cat(id = value)
784            if not res:
[1573]785                logger.info('Student id %s not found' % value)
[1891]786                err = 'No student with this Id'
[1393]787                datastructure['student'] = None
788            else:
789                datastructure['student'] = res[0]
790        if err:
791            datastructure.setError(widget_id, err)
792        else:
793            datamodel = datastructure.getDataModel()
794            datamodel[self.fields[0]] = value
795        return not err
796
797InitializeClass(StudentIdWidget)
798
799widgetRegistry.register(StudentIdWidget)
800###)
801
[1146]802class WAeUPMultilineResultsWidget(CPSStringWidget): ###(
803    """ WAeUPMultilineResults Widget"""
804    meta_type = "WAeUp Multiline Results Widget"
805    _properties = CPSWidget._properties + (
806        {'id': 'nr_of_lines', 'type': 'int', 'mode': 'w',
807         'label': 'Nr of Lines'},
808         )
809    nr_of_lines = 5
810    def prepare(self, datastructure, **kw): ###(
811        """Prepare datastructure from datamodel."""
812        datamodel = datastructure.getDataModel()
813        #import pdb;pdb.set_trace()
814        widget_id = self.getWidgetId()
815        v = datamodel[self.fields[0]]
816        if type(v) is ListType and v:
817            nr_results = len(v)
818        else:
819            v = []
820            nr_results = 0
821        count = 1
822        for s,g in v:
823            wid = "%s%02d"% (widget_id,count)
824            datastructure[wid+'_s'] = s
825            datastructure[wid+'_g'] = g
826            count += 1
827        if nr_results < self.nr_of_lines:
828            for line in range(nr_results,self.nr_of_lines):
829                v.append(('',''))
830                wid = "%s%02d"% (widget_id,line)
831                datastructure[wid+'_s'] = ''
832                datastructure[wid+'_g'] = ''
833        datastructure[widget_id] = v
834        datastructure[widget_id+'_s'] = ''
835        datastructure[widget_id+'_g'] = ''
836    ###)
837
838    def validate(self, datastructure, **kw): ###(
839        """Validate datastructure and update datamodel."""
840        #import pdb;pdb.set_trace()
841        widget_id = self.getWidgetId()
842        err = 0
843        lines = []
844        for line in range(1,30):
845            wid = "%s%02d"% (widget_id,line)
846            if not datastructure.has_key(wid+'_s'):
847                break
848            lines.append((datastructure[wid+'_s'].strip(),
849                         datastructure[wid+'_g'].strip()))
[1155]850
[1146]851        s = datastructure[widget_id+'_s'].strip()
852        g = datastructure[widget_id+'_g'].strip()
853        if s and g:
854            lines.append((s,g))
855        active = []
856        for s,g in lines:
857            if g != "":
858                active.append((s,g))
859        if err:
860            datastructure.setError(widget_id, err)
861        else:
862            datamodel = datastructure.getDataModel()
863            datamodel[self.fields[0]] = active
864        return not err
865    ###)
866
867    def render(self, mode, datastructure, **kw): ###(
868        """Render in mode from datastructure."""
869        render_method = 'widget_waeup_multiline_result_render'
870        meth = getattr(self, render_method, None)
871        if meth is None:
872            raise RuntimeError("Unknown Render Method %s for widget type %s"
873                               % (render_method, self.getId()))
874        #import pdb;pdb.set_trace()
875        datamodel = datastructure.getDataModel()
876        widget_id = self.getWidgetId()
877        lines = datamodel[self.fields[0]]
878        if len(lines) < self.nr_of_lines:
879            for line in range(len(lines),self.nr_of_lines + 1):
880                lines.append(('',''))
881        datastructure[widget_id] = lines
882        datastructure[widget_id+'_s'] = ''
883        datastructure[widget_id+'_g'] = ''
884##        count = 1
885##        for s,g in v:
886##            wid = "%s%02d"% (widget_id,count)
887##            count += 1
888        return meth(mode=mode,
889                    datastructure=datastructure,
890                    )
891    ###)
892
893
894InitializeClass(WAeUPMultilineResultsWidget)
895widgetRegistry.register(WAeUPMultilineResultsWidget)
896###)
897
[794]898class WAeUPResultsWidget(CPSStringWidget): ###(
899    """ WAeUPResults Widget"""
900    meta_type = "WAeUp Results Widget"
901
902    def prepare(self, datastructure, **kw): ###(
903        """Prepare datastructure from datamodel."""
904        datamodel = datastructure.getDataModel()
905        v = datamodel[self.fields[0]]
906        #import pdb;pdb.set_trace()
907        widget_id = self.getWidgetId()
[807]908        datastructure[widget_id] = v
[794]909        datastructure[widget_id+'_s'] = ''
910        datastructure[widget_id+'_g'] = ''
911    ###)
912
913    def validate(self, datastructure, **kw): ###(
914        """Validate datastructure and update datamodel."""
[807]915        #import pdb;pdb.set_trace()
[794]916        widget_id = self.getWidgetId()
917        v = datastructure[widget_id]
918        err = 0
919        s = datastructure[widget_id+'_s'].strip()
920        g = datastructure[widget_id+'_g'].strip()
[807]921        while 1:
922            if not s and g:
[1891]923                err = "No subject grade for subject"
[807]924                break
925            i = 0
926            done = False
927            for sv,gv in v:
928                if sv == s:
929                    done = True
930                    if not g:
931                        v.pop(i)
932                        break
933                    v[i] = (s,g)
934                    break
935                i += 1
936            if done:
937                break
938            if s and g:
939                v.append((s,g))
940            break
[794]941        if err:
942            datastructure.setError(widget_id, err)
943        else:
944            datamodel = datastructure.getDataModel()
945            datamodel[self.fields[0]] = v
946            datastructure[widget_id+'_s'] = s
947            datastructure[widget_id+'_g'] = g
948        return not err
949    ###)
950
951    def render(self, mode, datastructure, **kw): ###(
952        """Render in mode from datastructure."""
953        render_method = 'widget_waeup_result_render'
954        meth = getattr(self, render_method, None)
955        if meth is None:
956            raise RuntimeError("Unknown Render Method %s for widget type %s"
957                               % (render_method, self.getId()))
958        #import pdb;pdb.set_trace()
959        datamodel = datastructure.getDataModel()
960        widget_id = self.getWidgetId()
961        datastructure[widget_id+'_s'] = ''
962        datastructure[widget_id+'_g'] = ''
963        return meth(mode=mode,
964                    datastructure=datastructure,
965                    )
966    ###)
967
968
969InitializeClass(WAeUPResultsWidget)
970widgetRegistry.register(WAeUPResultsWidget)
971###)
972
[488]973class ScratchCardPin: ###(
974    """the ScratchCardPin"""
975    def __init__(self,prefix,batch_no,number):
[990]976        if not batch_no and not number:
977            s = prefix
[996]978            if len(s) > 3:
[990]979                prefix,batch_no,number = s[:3],s[3:-10],s[-10:]
980            else:
[996]981                prefix,batch_no,number = s,'',''
[488]982        self.p = prefix
983        self.b = batch_no
984        self.n = number
985
986    def __str__(self):
987        return "%s-%s-%s" % (self.p,self.b,self.n)
[1146]988
989    def __repr__(self):
990        return "%s%s%s" % (self.p,self.b,self.n)
[488]991###)
992
[47]993class ScratchcardPinWidget(CPSStringWidget): ###(
[22]994    """ ScratchcardPin Widget"""
[199]995    meta_type = "Scratchcard Pin Widget"
[488]996    _properties = CPSWidget._properties + (
997        {'id': 'prefix', 'type': 'string', 'mode': 'w',
998         'label': 'Prefix'},
999         {'id': 'reference', 'type': 'string', 'mode': 'w',
1000         'label': 'Reference Field'},
1001        )
1002    prefix = ''
1003    reference = ''
[502]1004    def prepare(self, datastructure, **kw): ###(
[488]1005        """Prepare datastructure from datamodel."""
1006        datamodel = datastructure.getDataModel()
1007        v = datamodel[self.fields[0]]
1008        widget_id = self.getWidgetId()
[742]1009        #import pdb;pdb.set_trace()
[747]1010        if v and type(v) is StringType:
[990]1011            try:
1012                p,b,n = v.split('-')
1013                v = ScratchCardPin(p,b,n)
1014            except ValueError:
1015                v = ScratchCardPin(v,'','')
[488]1016        if v:
[742]1017            p = '%s' % v.p
[488]1018            b = '%s' % v.b
1019            n = '%s' % v.n
1020        else:
[742]1021            p = self.prefix
1022            if p.startswith('@'):
1023                p = getattr(self,self.prefix[1:])()
[488]1024            b = n = ''
[742]1025            v = ScratchCardPin(p,b,n)
[488]1026        datastructure[widget_id] = v
[742]1027        datastructure[widget_id+'_p'] = p
[488]1028        datastructure[widget_id+'_b'] = b
1029        datastructure[widget_id+'_n'] = n
[1376]1030    ###)
[758]1031
[1169]1032    def validate(self, datastructure, **kw): ###(
[22]1033        """Validate datastructure and update datamodel."""
[1571]1034        s_logger = logging.getLogger('Widgets.ScratchcardPinWidget.validate')
[488]1035        widget_id = self.getWidgetId()
1036        v = datastructure[widget_id]
[996]1037        #import pdb;pdb.set_trace()
[488]1038        err = 0
[1907]1039        mapping = {}
[742]1040        prefix= self.prefix
1041        if prefix.startswith('@'):
1042            prefix= getattr(self,self.prefix[1:])()
[488]1043        b = datastructure[widget_id+'_b'].strip()
1044        n = datastructure[widget_id+'_n'].strip()
[502]1045        pins = self.portal_pins
[742]1046        pin = "%(prefix)s%(b)s%(n)s" % vars()
[1376]1047        pin_str = "%(prefix)s-%(b)-s%(n)s" % vars()
[816]1048        do = 1
[1025]1049        s_id = str(self.portal_membership.getAuthenticatedMember())
[1326]1050        #import pdb;pdb.set_trace()
[1343]1051        if self.isStaff():
[1326]1052            do = 0
1053            err ='You are not a Student. PIN neither checked nor used.'
[1571]1054            s_logger.info('%s tried to use scratch card %s' % (s_id,pin_str))
[1557]1055        elif len(b) > 1 and b.find('-') > -1:
1056            do = 0
[1573]1057            err = 'PIN must not contain "-"'
1058            s_logger.info('%s entered invalid PIN  containing "-"' % (s_id))
[1557]1059        elif n.find('-') > -1:
1060            do = 0
[1573]1061            err = 'PIN must not contain "-"'
1062            s_logger.info('%s entered invalid PIN  containing "-"' % (s_id))
[1376]1063        elif len(n) != 10:
1064            do = 0
[1891]1065            err = 'Invalid PIN length'
[1573]1066            s_logger.info('%s entered invalid PIN with length %d' % (s_id,len(n)))
[1326]1067        elif self.reference == "":
[1030]1068            ref = s_id
[635]1069        else:
1070            ref = datastructure[self.reference]
[843]1071            if datastructure.errors:
[816]1072                do = 0
[1805]1073                datastructure.setError(widget_id, 'PIN neither checked nor used.')
[1571]1074                s_logger.info('%s/%s entered wrong data together with PIN %s' % (s_id,ref,pin_str))
[816]1075        while do:
1076            ok = pins.searchAndSetRecord(pin,ref,prefix)
[1030]1077            if ok < -2 or ok > 2:
1078                err = 'Unknown error, please report!'
[1571]1079                s_logger.info('%s/%s caused unknown error with PIN %s' % (s_id,ref,pin_str))
[1030]1080                break
1081            elif ok == -2:
[1783]1082                err = 'Service already is activated but with a different PIN.'
[1571]1083                s_logger.info('%s/%s repeatedly activated service but with different PIN %s' % (s_id,ref,pin_str))
[710]1084                break
1085            elif ok == -1:
[1793]1086                err = 'Invalid PIN'
[1571]1087                s_logger.info('%s/%s entered invalid PIN %s' % (s_id,ref,pin_str))
[502]1088                break
1089            if ok == 0:
[1571]1090                err = 'PIN already used'
1091                s_logger.info('%s/%s entered used PIN %s' % (s_id,ref,pin_str))
[502]1092                break
1093            if ok >= 1:
[710]1094                #import pdb;pdb.set_trace()
[635]1095                if self.isStudent():
[992]1096                    if self.reference == "jamb_reg_no":
[997]1097                        err = "You are already logged in."
[1573]1098                        s_logger.info('%s/%s checked admission with PIN %s though logged in' % (s_id,ref,pin_str))
[992]1099                        break
[1082]1100                    if ok == 1:
[1571]1101                        s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str))
[1032]1102                    else:
[1571]1103                        s_logger.info('%s/%s repeatedly used PIN %s' % (s_id,ref,pin_str))
[637]1104                    break
[635]1105                else:
1106                    student = getStudentByRegNo(self,ref)
[1571]1107                    s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str))
[502]1108                if student is None:
[1793]1109                    err = "Student not found"
[1571]1110                    s_logger.info('%s not found in admission list' % (ref))
[502]1111                    break
[648]1112                s_id = student.getId()
[502]1113                if ok == 2:
[990]1114                    if self.reference == "jamb_reg_no":
1115                        if hasattr(self.portal_directories.students,s_id):
[1907]1116                            err = "Please login with your Student Id ${id} and 10-digit PIN."
1117                            mapping = {'id': s_id}
[1571]1118                            s_logger.info('%s/%s repeatedly checked admission with PIN %s' % (s_id,ref,pin_str))
[990]1119                            break
1120                        else:
[1571]1121                            s_logger.info('%s/%s (non-member) repeatedly checked admission with PIN %s' % (s_id,ref,pin_str))
[990]1122                    else:
[1891]1123                        err = "Unknown error"
[1571]1124                        s_logger.info('%s/%s repeatedly activated service with PIN %s' % (s_id,ref,pin_str))
[986]1125                        break
1126                try:
1127                    student.getContent().makeStudentMember(s_id,password=pin[4:])
[1571]1128                    s_logger.info('%s/%s has been created using PIN %s' % (s_id,ref,pin_str))
[986]1129                except:
[1907]1130                    err = "Please login with your Student Id ${id} and 10-digit PIN."
1131                    mapping = {'id': s_id}
[1571]1132                    s_logger.info('%s/%s could not be made a member with PIN %s' % (s_id,ref,pin_str))
[986]1133                    break
[502]1134            break
[488]1135        if err:
[1907]1136            datastructure.setError(widget_id, err,mapping)
[488]1137        else:
1138            datamodel = datastructure.getDataModel()
[742]1139            datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n)
1140            datastructure[widget_id] = ScratchCardPin(prefix,b,n)
1141            datastructure[widget_id+'_p'] = prefix
[488]1142            datastructure[widget_id+'_b'] = b
1143            datastructure[widget_id+'_n'] = n
[502]1144            datastructure['s_id'] = s_id
[488]1145        return not err
[444]1146
[1169]1147###)
1148
[502]1149    def render(self, mode, datastructure, **kw): ###(
[488]1150        """Render in mode from datastructure."""
1151        render_method = 'widget_scratch_card_pin_render'
1152        meth = getattr(self, render_method, None)
1153        if meth is None:
1154            raise RuntimeError("Unknown Render Method %s for widget type %s"
1155                               % (render_method, self.getId()))
1156
1157        # XXX AT: datastructure has to be set again here, in case we're in edit
1158        # or create mode, because a default value has to be provided.
1159        #import pdb;pdb.set_trace()
1160        datamodel = datastructure.getDataModel()
1161        v = datamodel[self.fields[0]]
[502]1162        if v and type(v) is StringType:
[990]1163            try:
1164                p,b,n = v.split('-')
1165                v = ScratchCardPin(p,b,n)
1166            except ValueError:
[996]1167                v = ScratchCardPin(self.prefix,'1',v)
1168                pass
[488]1169        if v:
[742]1170            prefix= '%s' % v.p
[488]1171            b = '%s' % v.b
1172            n = '%s' % v.n
[22]1173        else:
[742]1174            prefix= self.prefix
1175            if prefix.startswith('@'):
1176                prefix= getattr(self,self.prefix[1:])()
[488]1177            b = n = ''
[742]1178            v = ScratchCardPin(prefix,b,n)
1179        widget_id = self.getWidgetId()
1180        datastructure[widget_id] = v
1181        datastructure[widget_id+'_p'] = prefix
1182        datastructure[widget_id+'_b'] = b
1183        datastructure[widget_id+'_n'] = n
[758]1184        return meth(mode=mode,
1185                    datastructure=datastructure,
[488]1186                    )
[523]1187    ###)
[488]1188
1189
[22]1190InitializeClass(ScratchcardPinWidget)
[199]1191widgetRegistry.register(ScratchcardPinWidget)
[22]1192
[47]1193
1194###)
1195
[1169]1196class WAeUPImageWidget(CPSImageWidget): ###(
[537]1197    """Photo widget."""
1198    meta_type = 'WAeUP Image Widget'
1199
1200    def render(self, mode, datastructure, **kw):
1201        render_method = 'widget_waeup_image_render'
1202        meth = getattr(self, render_method, None)
1203        if meth is None:
1204            raise RuntimeError("Unknown Render Method %s for widget type %s"
1205                               % (render_method, self.getId()))
1206        img_info = self.getImageInfo(datastructure)
1207        return meth(mode=mode, datastructure=datastructure, **img_info)
1208
1209InitializeClass(WAeUPImageWidget)
1210
1211widgetRegistry.register(WAeUPImageWidget)
[1804]1212###)
[537]1213
1214
[22]1215###########
1216
Note: See TracBrowser for help on using the repository browser.