source: WAeUP_SRP/trunk/Widgets.py @ 827

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

mail2admin for testing

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