source: WAeUP_SRP/trunk/Widgets.py @ 1999

Last change on this file since 1999 was 1986, checked in by Henrik Bettermann, 17 years ago

show sessions, courselevel and entrymodes vocabularies in correct order, please check
(btw courselevel should be renamed to levels)

  • Property svn:keywords set to Id
File size: 43.6 KB
Line 
1#-*- mode: python; mode: fold -*-
2# $Id: Widgets.py 1986 2007-07-05 09:10:38Z henrik $
3from cgi import escape
4from types import *
5from Globals import InitializeClass
6##from Products.CPSSchemas.Widget import CPSWidgetType
7from Products.CMFCore.utils import getToolByName
8from Products.CPSSchemas.BasicWidgets import CPSBooleanWidget, CPSWidget, CPSStringWidget, CPSEmailWidget,CPSImageWidget
9from Products.CPSSchemas.BasicWidgets import renderHtmlTag,CPSSelectWidget, CPSStringWidget
10from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget
11from Products.CPSSchemas.Widget import widgetRegistry
12##from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry
13from DateTime.DateTime import DateTime
14from AccessControl import getSecurityManager
15from Products.WAeUP_SRP.Students import getStudentByRegNo
16from Products.WAeUP_SRP.Academics import makeCertificateCode
17from re import compile
18import logging
19import operator
20
21#from zLOG import LOG, DEBUG
22
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()
48            res = renderHtmlTag('select',
49                                name='%s.%s:records' % (self.record_id,html_widget_id),
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
77class CPSStringWidgetForRecord(CPSStringWidget): ###(
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,
99                  'name': '%s.%s:records' % (self.record_id,html_widget_id),
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
112###)
113
114class CertificateCourseIdWidget(CPSStringWidget): ###(
115    """ CertificateCourseId Widget"""
116    meta_type = "CertificateCourseId Widget"
117
118    def validate(self, datastructure, **kw):
119        """Validate datastructure and update datamodel."""
120
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"})]
129            if hasattr(self.aq_parent,value):
130                err = 'Course already exists'
131            elif value not in c_ids:
132                err = 'Course does not exist'
133            if err:
134                datastructure.setError(widget_id, err)
135            else:
136                datamodel = datastructure.getDataModel()
137                datamodel[self.fields[0]] = value
138
139            return not err
140
141InitializeClass(CertificateCourseIdWidget)
142
143widgetRegistry.register(CertificateCourseIdWidget)
144###)
145
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:
162                err = 'Course already exists'
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
176###)
177
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:
201            v = value = 'ume_ft'
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
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 = {'-1': 'N/A'}
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
267
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(0))
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 = {'000':'N/A'}
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
337
338        if not value:
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(0))
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
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:
476                reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',value)
477                                     if r]
478            except (ValueError,IndexError),msg:
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
492class WAeUPIdWidget(CPSStringWidget): ###(
493    """ WAeUPId Widget"""
494    meta_type = "WAeUPId Widget"
495
496    def validate(self, datastructure, **kw):
497        """Validate datastructure and update datamodel."""
498
499        valid = CPSStringWidget.validate(self, datastructure, **kw)
500        id_pat_str = r"\S"
501        inv_id_pat = compile(r"^%s$" % id_pat_str)
502        if not valid:
503            return 0
504        else:
505            portal_type_query = {'query':['Faculty',
506                                          'Department',
507                                          'Course',
508                                          'Certificate',
509                                          'CertificateCourse',]}
510            widget_id = self.getWidgetId()
511            value = datastructure[widget_id].upper()
512            err = 0
513            mapping = {}
514            if len(value.split()) > 1:
515                err = 'Invalid Id, Id contains space(s).'
516            elif self.portal_catalog(portal_type=portal_type_query,id=value):
517                brain = self.portal_catalog(portal_type=portal_type_query,id=value)[0]
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                           }
523            if err:
524                datastructure.setError(widget_id, err, mapping)
525            else:
526                datamodel = datastructure.getDataModel()
527                datamodel[self.fields[0]] = value
528
529            return not err
530
531InitializeClass(WAeUPIdWidget)
532
533widgetRegistry.register(WAeUPIdWidget)
534
535
536###)
537
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()
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()
551        id_pat_str = r"\S"
552        inv_id_pat = compile(r"^%s$" % id_pat_str)
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)
560        else:
561            datamodel = datastructure.getDataModel()
562            datamodel[self.fields[0]] = value
563        return not err
564
565InitializeClass(StudyCourseWidget)
566
567widgetRegistry.register(StudyCourseWidget)
568###)
569
570class JambRegNoWidget(CPSStringWidget): ###(
571    """ JambRegNo Widget"""
572    meta_type = "JambRegNo Widget"
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 = ""
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)
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)
594            err = 'Invalid number'
595        elif self.reference == '':
596            #s = getStudentByRegNo(self,value)
597            pume = jamb_nr_catalog(jamb_reg_no = value)
598            if len(pume) < 1:
599                err = 'No student record with this registration number.'
600            else:
601                datastructure['pume'] = pume[0]
602        else:
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:
608                err = 'Registration number does not match.'
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
615
616InitializeClass(JambRegNoWidget)
617
618widgetRegistry.register(JambRegNoWidget)
619###)
620
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"
631    matric_no_catalog = 'returning_import'
632    check_fields = ("Firstname", "Middlename","Lastname")
633    def validate(self, datastructure, **kw):
634        """Validate datastructure and update datamodel."""
635        logger = logging.getLogger('Widgets.SecretWidget.validate')
636        valid = CPSStringWidget.validate(self, datastructure, **kw)
637        widget_id = self.getWidgetId()
638        value = datastructure[widget_id].upper()
639        err = 0
640        record = datastructure.get(self.reference,None)
641        #import pdb;pdb.set_trace()
642        if not valid or len(value) < 2:
643            err = 'Invalid string'
644        elif not record or datastructure.errors:
645            err = 0
646        else:
647            found = False
648            cvs = []
649            for field in self.check_fields:
650                cv = getattr(record,field).upper()
651                if len(cv.split()) > 1:
652                    for splited in cv.split():
653                        cvs.append(splited.strip())
654                else:
655                    cvs.append(cv)
656            for cv in cvs:
657                if cv  == value.upper():
658                    found = True
659                    break
660            matric_no = record.matric_no
661            name = " ".join(cvs)
662            if not found:
663                logger.info('%(matric_no)s did not find %(value)s in %(name)s' % vars())
664                err = 'No name does match.'
665            else:
666                logger.info('%(matric_no)s found %(value)s in %(name)s' % vars())
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
679class WAeUPSexWidget(CPSBooleanWidget): ###(
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
708###)
709
710class MatricNoWidget(CPSStringWidget): ###(
711    """ MatricNo Widget"""
712    meta_type = "MatricNo Widget"
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
721    results_catalog = "results_import" #results catalog
722
723    def validate(self, datastructure, **kw):
724        """Validate datastructure and update datamodel."""
725        #import pdb;pdb.set_trace()
726        valid = CPSStringWidget.validate(self, datastructure, **kw)
727        logger = logging.getLogger('Widgets.MatricNoWidget.validate')
728        returning = getattr(self,self.matric_no_catalog)
729        results = getattr(self,self.results_catalog,None)
730        err = 0
731        widget_id = self.getWidgetId()
732        value = datastructure[widget_id]
733        if not valid or not value:
734            err = 'Invalid string'
735            logger.info('Invalid matric_no string %s' % value)
736        else:
737            value = value.upper()
738            datastructure['student'] = None
739            while not err:
740                res = returning(matric_no = value)
741                if len(res) < 1:
742                    logger.info('matric_no %s not found' % value)
743                    err = 'No student with this matriculation number.'
744                    break
745                datastructure['student'] = res[0]
746                if results is not None:
747                    res = results(matric_no = value)
748                    if len(res) < 1:
749                        err = 'No results for this matriculation number'
750                        continue
751                    datastructure['results'] = res
752                break
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
759
760InitializeClass(MatricNoWidget)
761
762widgetRegistry.register(MatricNoWidget)
763###)
764
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)
771        logger = logging.getLogger('Widgets.StudentIdWidget.validate')
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:
778            err = 'Invalid Id string'
779            logger.info('Invalid id string %s' % value)
780            datastructure['student'] = None
781        else:
782            value = value.upper()
783            res = s_cat(id = value)
784            if not res:
785                logger.info('Student id %s not found' % value)
786                err = 'No student with this Id'
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
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()))
850
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
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()
908        datastructure[widget_id] = v
909        datastructure[widget_id+'_s'] = ''
910        datastructure[widget_id+'_g'] = ''
911    ###)
912
913    def validate(self, datastructure, **kw): ###(
914        """Validate datastructure and update datamodel."""
915        #import pdb;pdb.set_trace()
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()
921        while 1:
922            if not s and g:
923                err = "No subject grade for subject"
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
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
973class ScratchCardPin: ###(
974    """the ScratchCardPin"""
975    def __init__(self,prefix,batch_no,number):
976        if not batch_no and not number:
977            s = prefix
978            if len(s) > 3:
979                prefix,batch_no,number = s[:3],s[3:-10],s[-10:]
980            else:
981                prefix,batch_no,number = s,'',''
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)
988
989    def __repr__(self):
990        return "%s%s%s" % (self.p,self.b,self.n)
991###)
992
993class ScratchcardPinWidget(CPSStringWidget): ###(
994    """ ScratchcardPin Widget"""
995    meta_type = "Scratchcard Pin Widget"
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 = ''
1004    def prepare(self, datastructure, **kw): ###(
1005        """Prepare datastructure from datamodel."""
1006        datamodel = datastructure.getDataModel()
1007        v = datamodel[self.fields[0]]
1008        widget_id = self.getWidgetId()
1009        #import pdb;pdb.set_trace()
1010        if v and type(v) is StringType:
1011            try:
1012                p,b,n = v.split('-')
1013                v = ScratchCardPin(p,b,n)
1014            except ValueError:
1015                v = ScratchCardPin(v,'','')
1016        if v:
1017            p = '%s' % v.p
1018            b = '%s' % v.b
1019            n = '%s' % v.n
1020        else:
1021            p = self.prefix
1022            if p.startswith('@'):
1023                p = getattr(self,self.prefix[1:])()
1024            b = n = ''
1025            v = ScratchCardPin(p,b,n)
1026        datastructure[widget_id] = v
1027        datastructure[widget_id+'_p'] = p
1028        datastructure[widget_id+'_b'] = b
1029        datastructure[widget_id+'_n'] = n
1030    ###)
1031
1032    def validate(self, datastructure, **kw): ###(
1033        """Validate datastructure and update datamodel."""
1034        s_logger = logging.getLogger('Widgets.ScratchcardPinWidget.validate')
1035        widget_id = self.getWidgetId()
1036        v = datastructure[widget_id]
1037        #import pdb;pdb.set_trace()
1038        err = 0
1039        mapping = {}
1040        prefix= self.prefix
1041        if prefix.startswith('@'):
1042            prefix= getattr(self,self.prefix[1:])()
1043        b = datastructure[widget_id+'_b'].strip()
1044        n = datastructure[widget_id+'_n'].strip()
1045        pins = self.portal_pins
1046        pin = "%(prefix)s%(b)s%(n)s" % vars()
1047        pin_str = "%(prefix)s-%(b)-s%(n)s" % vars()
1048        do = 1
1049        s_id = str(self.portal_membership.getAuthenticatedMember())
1050        #import pdb;pdb.set_trace()
1051        if self.isStaff():
1052            do = 0
1053            err ='You are not a Student. PIN neither checked nor used.'
1054            s_logger.info('%s tried to use scratch card %s' % (s_id,pin_str))
1055        elif len(b) > 1 and b.find('-') > -1:
1056            do = 0
1057            err = 'PIN must not contain "-"'
1058            s_logger.info('%s entered invalid PIN  containing "-"' % (s_id))
1059        elif n.find('-') > -1:
1060            do = 0
1061            err = 'PIN must not contain "-"'
1062            s_logger.info('%s entered invalid PIN  containing "-"' % (s_id))
1063        elif len(n) != 10:
1064            do = 0
1065            err = 'Invalid PIN length'
1066            s_logger.info('%s entered invalid PIN with length %d' % (s_id,len(n)))
1067        elif self.reference == "":
1068            ref = s_id
1069        else:
1070            ref = datastructure[self.reference]
1071            if datastructure.errors:
1072                do = 0
1073                datastructure.setError(widget_id, 'PIN neither checked nor used.')
1074                s_logger.info('%s/%s entered wrong data together with PIN %s' % (s_id,ref,pin_str))
1075        while do:
1076            ok = pins.searchAndSetRecord(pin,ref,prefix)
1077            if ok < -2 or ok > 2:
1078                err = 'Unknown error, please report!'
1079                s_logger.info('%s/%s caused unknown error with PIN %s' % (s_id,ref,pin_str))
1080                break
1081            elif ok == -2:
1082                err = 'Service already is activated but with a different PIN.'
1083                s_logger.info('%s/%s repeatedly activated service but with different PIN %s' % (s_id,ref,pin_str))
1084                break
1085            elif ok == -1:
1086                err = 'Invalid PIN'
1087                s_logger.info('%s/%s entered invalid PIN %s' % (s_id,ref,pin_str))
1088                break
1089            if ok == 0:
1090                err = 'PIN already used'
1091                s_logger.info('%s/%s entered used PIN %s' % (s_id,ref,pin_str))
1092                break
1093            if ok >= 1:
1094                #import pdb;pdb.set_trace()
1095                if self.isStudent():
1096                    if self.reference == "jamb_reg_no":
1097                        err = "You are already logged in."
1098                        s_logger.info('%s/%s checked admission with PIN %s though logged in' % (s_id,ref,pin_str))
1099                        break
1100                    if ok == 1:
1101                        s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str))
1102                    else:
1103                        s_logger.info('%s/%s repeatedly used PIN %s' % (s_id,ref,pin_str))
1104                    break
1105                else:
1106                    student = getStudentByRegNo(self,ref)
1107                    s_logger.info('%s/%s successfully used PIN %s' % (s_id,ref,pin_str))
1108                if student is None:
1109                    err = "Student not found"
1110                    s_logger.info('%s not found in admission list' % (ref))
1111                    break
1112                s_id = student.getId()
1113                if ok == 2:
1114                    if self.reference == "jamb_reg_no":
1115                        if hasattr(self.portal_directories.students,s_id):
1116                            err = "Please login with your Student Id ${id} and 10-digit PIN."
1117                            mapping = {'id': s_id}
1118                            s_logger.info('%s/%s repeatedly checked admission with PIN %s' % (s_id,ref,pin_str))
1119                            break
1120                        else:
1121                            s_logger.info('%s/%s (non-member) repeatedly checked admission with PIN %s' % (s_id,ref,pin_str))
1122                    else:
1123                        err = "Unknown error"
1124                        s_logger.info('%s/%s repeatedly activated service with PIN %s' % (s_id,ref,pin_str))
1125                        break
1126                try:
1127                    student.getContent().makeStudentMember(s_id,password=pin[4:])
1128                    s_logger.info('%s/%s has been created using PIN %s' % (s_id,ref,pin_str))
1129                except:
1130                    err = "Please login with your Student Id ${id} and 10-digit PIN."
1131                    mapping = {'id': s_id}
1132                    s_logger.info('%s/%s could not be made a member with PIN %s' % (s_id,ref,pin_str))
1133                    break
1134            break
1135        if err:
1136            datastructure.setError(widget_id, err,mapping)
1137        else:
1138            datamodel = datastructure.getDataModel()
1139            datamodel[self.fields[0]] = ScratchCardPin(prefix,b,n)
1140            datastructure[widget_id] = ScratchCardPin(prefix,b,n)
1141            datastructure[widget_id+'_p'] = prefix
1142            datastructure[widget_id+'_b'] = b
1143            datastructure[widget_id+'_n'] = n
1144            datastructure['s_id'] = s_id
1145        return not err
1146
1147###)
1148
1149    def render(self, mode, datastructure, **kw): ###(
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]]
1162        if v and type(v) is StringType:
1163            try:
1164                p,b,n = v.split('-')
1165                v = ScratchCardPin(p,b,n)
1166            except ValueError:
1167                v = ScratchCardPin(self.prefix,'1',v)
1168                pass
1169        if v:
1170            prefix= '%s' % v.p
1171            b = '%s' % v.b
1172            n = '%s' % v.n
1173        else:
1174            prefix= self.prefix
1175            if prefix.startswith('@'):
1176                prefix= getattr(self,self.prefix[1:])()
1177            b = n = ''
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
1184        return meth(mode=mode,
1185                    datastructure=datastructure,
1186                    )
1187    ###)
1188
1189
1190InitializeClass(ScratchcardPinWidget)
1191widgetRegistry.register(ScratchcardPinWidget)
1192
1193
1194###)
1195
1196class WAeUPImageWidget(CPSImageWidget): ###(
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)
1212###)
1213
1214
1215###########
1216
Note: See TracBrowser for help on using the repository browser.