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

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

Use common and coherent naming convention for all event handlers.

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1"""WAeUP portal certificates
2"""
3import grok
4from zope.catalog.interfaces import ICatalog
5from zope.component import getUtility
6from zope.component.interfaces import IFactory, ComponentLookupError
7from zope.interface import implementedBy
8from waeup.sirp.university.interfaces import (
9    ICertificate, ICertificateAdd, ICertificateCourse, ICourse)
10from waeup.sirp.university.vocabularies import course_levels   
11
12class Certificate(grok.Container):
13    """A certificate.
14    """
15    grok.implements(ICertificate, ICertificateAdd)
16
17    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
18                 study_mode=None, start_level=None,
19                 end_level=None, application_category=None):
20        super(Certificate, self).__init__()
21        self.code = code
22        self.title = title
23        self.study_mode = study_mode
24        self.start_level = start_level
25        self.end_level = end_level
26        self.application_category = application_category
27
28    def longtitle(self):
29        return "%s (%s)" % (self.title,self.code) 
30   
31    def addCourseRef(self, course, level=100, core_or_elective=True):
32        """Add a course referrer.
33        """
34        code = "%s_%s" % (course.code, level)
35        self[code] = CertificateCourse(course, level, core_or_elective)
36        self[code].__parent__ = self
37        self[code].__name__ = code
38        self._p_changed = True
39
40    def delCourseRef(self, code, level=None):
41        """Delete a course referrer denoted by its code.
42        """
43        for key in self.keys():
44            if self[key].getCourseCode() != code:
45                continue
46            if level is not None and str(self[key].level) != str(level):
47                # found a course with correct key but wrong level...
48                continue
49            del self[key]
50            self._p_changed = True
51        return
52
53class CertificateFactory(grok.GlobalUtility):
54    """A factory for certificates.
55    """
56    grok.implements(IFactory)
57    grok.name(u'waeup.Certificate')
58    title = u"Create a new certificate.",
59    description = u"This factory instantiates new certificate instances."
60
61    def __call__(self, *args, **kw):
62        return Certificate(*args, **kw)
63
64    def getInterfaces(self):
65        return implementedBy(Certificate)
66
67class CertificateCourse(grok.Model):
68    grok.implements(ICertificateCourse)
69
70    def __init__(self, course=None, level=100, core_or_elective=True):
71        self.course = course
72        self.level = level
73        self.core_or_elective = core_or_elective
74
75    def getCourseCode(self):
76        """Get code of a course.
77        """
78        return self.course.code
79       
80    def longtitle(self):
81        return "%s in level %s" % (self.course.code,
82                   course_levels.getTerm(self.level).title)   
83   
84class CertificateCourseFactory(grok.GlobalUtility):
85    """A factory for certificate courses.
86    """
87    grok.implements(IFactory)
88    grok.name(u'waeup.CertificateCourse')
89    title = u"Create a new certificate course.",
90    description = u"This factory instantiates new certificate courses."
91
92    def __call__(self, *args, **kw):
93        return CertificateCourse(*args, **kw)
94
95    def getInterfaces(self):
96        return implementedBy(CertificateCourse)
97
98@grok.subscribe(ICourse, grok.IObjectRemovedEvent)
99def handle_course_removed(course, event):
100    """If a course is deleted, we make sure that also referrers in a
101       certificatecontainer are removed.
102    """
103    code = course.code
104
105    # Find all certificatecourses that refer to given course...
106    try:
107        cat = getUtility(ICatalog, name='certcourses_catalog')
108    except ComponentLookupError:
109        # catalog not available. This might happen during tests.
110        return
111       
112    results = cat.searchResults(course_code=(code, code))
113    for certcourse in results:
114        # Remove that referrer...
115        cert = certcourse.__parent__
116        cert.delCourseRef(code)
117        cert._p_changed = True
118    return
Note: See TracBrowser for help on using the repository browser.