source: main/waeup.sirp/trunk/src/waeup/sirp/university/certificate.py @ 7258

Last change on this file since 7258 was 7210, checked in by Henrik Bettermann, 13 years ago

The event class name is always ObjectRemovedEvent?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1## $Id: certificate.py 7210 2011-11-26 09:48:30Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""WAeUP portal certificates
19"""
20import grok
21from zope.event import notify
22from zope.catalog.interfaces import ICatalog
23from zope.component import getUtility
24from zope.component.interfaces import IFactory, ComponentLookupError
25from zope.interface import implementedBy
26from waeup.sirp.university.interfaces import (
27    ICertificate, ICertificateAdd, ICertificateCourse)
28from waeup.sirp.university.vocabularies import course_levels   
29
30class Certificate(grok.Container):
31    """A certificate.
32    """
33    grok.implements(ICertificate, ICertificateAdd)
34
35    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
36                 study_mode=None, start_level=None,
37                 end_level=None, application_category=None):
38        super(Certificate, self).__init__()
39        self.code = code
40        self.title = title
41        self.study_mode = study_mode
42        self.start_level = start_level
43        self.end_level = end_level
44        self.application_category = application_category
45
46    def longtitle(self):
47        return "%s (%s)" % (self.title,self.code) 
48   
49    def addCourseRef(self, course, level=100, core_or_elective=True):
50        """Add a course referrer.
51        """
52        code = "%s_%s" % (course.code, level)
53        self[code] = CertificateCourse(course, level, core_or_elective)
54        self[code].__parent__ = self
55        self[code].__name__ = code
56        self._p_changed = True
57
58    def delCourseRef(self, code, level=None):
59        """Delete a course referrer denoted by its code.
60        """
61        keys = list(self.keys()) # create list copy
62        for key in keys:
63            if self[key].getCourseCode() != code:
64                continue
65            if level is not None and str(self[key].level) != str(level):
66                # found a course with correct key but wrong level...
67                continue
68            del self[key]
69            self._p_changed = True
70        return
71
72class CertificateFactory(grok.GlobalUtility):
73    """A factory for certificates.
74    """
75    grok.implements(IFactory)
76    grok.name(u'waeup.Certificate')
77    title = u"Create a new certificate.",
78    description = u"This factory instantiates new certificate instances."
79
80    def __call__(self, *args, **kw):
81        return Certificate(*args, **kw)
82
83    def getInterfaces(self):
84        return implementedBy(Certificate)
85
86class CertificateCourse(grok.Model):
87    grok.implements(ICertificateCourse)
88
89    def __init__(self, course=None, level=100, core_or_elective=True):
90        self.course = course
91        self.level = level
92        self.core_or_elective = core_or_elective
93
94    def getCourseCode(self):
95        """Get code of a course.
96        """
97        return self.course.code
98       
99    def longtitle(self):
100        return "%s in level %s" % (self.course.code,
101                   course_levels.getTerm(self.level).title)   
102   
103class CertificateCourseFactory(grok.GlobalUtility):
104    """A factory for certificate courses.
105    """
106    grok.implements(IFactory)
107    grok.name(u'waeup.CertificateCourse')
108    title = u"Create a new certificate course.",
109    description = u"This factory instantiates new certificate courses."
110
111    def __call__(self, *args, **kw):
112        return CertificateCourse(*args, **kw)
113
114    def getInterfaces(self):
115        return implementedBy(CertificateCourse)
116
117@grok.subscribe(ICertificate, grok.IObjectRemovedEvent)
118def handle_certificate_removed(certificate, event):
119    """If a certificate is deleted, we make sure that also referrers to
120    student studycourse objects are removed.
121    """
122    code = certificate.code
123
124    # Find all student studycourses that refer to given certificate...
125    try:
126        cat = getUtility(ICatalog, name='students_catalog')
127    except ComponentLookupError:
128        # catalog not available. This might happen during tests.
129        return
130
131    results = cat.searchResults(certcode=(code, code))
132    for student in results:
133        # Remove that referrer...
134        studycourse = student['studycourse']
135        studycourse.certificate = None
136        notify(grok.ObjectModifiedEvent(student))
137        #event_name = event.__implemented__.__name__.replace(
138        #    'zope.lifecycleevent.','')
139        student.loggerInfo('ObjectRemovedEvent', 'removed: certificate')
140    return
Note: See TracBrowser for help on using the repository browser.