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

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

More copyright adjustments.

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