1 | ## $Id: certificate.py 9211 2012-09-21 08:19:35Z uli $ |
---|
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 | """Kofa certificates |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from zope.event import notify |
---|
22 | from zope.catalog.interfaces import ICatalog |
---|
23 | from zope.intid.interfaces import IIntIds |
---|
24 | from zope.schema import getFields |
---|
25 | from zope.component import getUtility |
---|
26 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
27 | from zope.interface import implementedBy |
---|
28 | from waeup.kofa.interfaces import IKofaPluggable |
---|
29 | from waeup.kofa.university.interfaces import ( |
---|
30 | ICertificate, ICertificateAdd, ICertificateCourse) |
---|
31 | from waeup.kofa.university.vocabularies import course_levels |
---|
32 | |
---|
33 | class Certificate(grok.Container): |
---|
34 | """A certificate. |
---|
35 | """ |
---|
36 | grok.implements(ICertificate, ICertificateAdd) |
---|
37 | |
---|
38 | @property # Make this method read_only and looking like an attr. |
---|
39 | def local_roles(self): |
---|
40 | return ['waeup.local.CourseAdviser100', |
---|
41 | 'waeup.local.CourseAdviser200', |
---|
42 | 'waeup.local.CourseAdviser300', |
---|
43 | 'waeup.local.CourseAdviser400', |
---|
44 | 'waeup.local.CourseAdviser500', |
---|
45 | 'waeup.local.CourseAdviser600', |
---|
46 | ] |
---|
47 | |
---|
48 | def __init__(self, code=u'NA', title=u'Unnamed Certificate', |
---|
49 | study_mode=None, start_level=None, |
---|
50 | end_level=None, application_category=None, |
---|
51 | school_fee_1=None, school_fee_2=None): |
---|
52 | super(Certificate, self).__init__() |
---|
53 | self.code = code |
---|
54 | self.title = title |
---|
55 | self.study_mode = study_mode |
---|
56 | self.start_level = start_level |
---|
57 | self.end_level = end_level |
---|
58 | self.application_category = application_category |
---|
59 | self.school_fee_1 = school_fee_1 |
---|
60 | self.school_fee_2 = school_fee_2 |
---|
61 | |
---|
62 | def longtitle(self): |
---|
63 | return "%s (%s)" % (self.title,self.code) |
---|
64 | |
---|
65 | def addCourseRef(self, course, level=100, mandatory=True): |
---|
66 | """Add a course referrer. |
---|
67 | """ |
---|
68 | code = "%s_%s" % (course.code, level) |
---|
69 | self[code] = CertificateCourse(course, level, mandatory) |
---|
70 | self[code].__parent__ = self |
---|
71 | self[code].__name__ = code |
---|
72 | self._p_changed = True |
---|
73 | |
---|
74 | def delCourseRef(self, code, level=None): |
---|
75 | """Delete a course referrer denoted by its code. |
---|
76 | """ |
---|
77 | keys = list(self.keys()) # create list copy |
---|
78 | for key in keys: |
---|
79 | if self[key].getCourseCode() != code: |
---|
80 | continue |
---|
81 | if level is not None and str(self[key].level) != str(level): |
---|
82 | # found a course with correct key but wrong level... |
---|
83 | continue |
---|
84 | del self[key] |
---|
85 | self._p_changed = True |
---|
86 | return |
---|
87 | |
---|
88 | class CertificateFactory(grok.GlobalUtility): |
---|
89 | """A factory for certificates. |
---|
90 | """ |
---|
91 | grok.implements(IFactory) |
---|
92 | grok.name(u'waeup.Certificate') |
---|
93 | title = u"Create a new certificate.", |
---|
94 | description = u"This factory instantiates new certificate instances." |
---|
95 | |
---|
96 | def __call__(self, *args, **kw): |
---|
97 | return Certificate(*args, **kw) |
---|
98 | |
---|
99 | def getInterfaces(self): |
---|
100 | return implementedBy(Certificate) |
---|
101 | |
---|
102 | class CertificateCourse(grok.Model): |
---|
103 | grok.implements(ICertificateCourse) |
---|
104 | |
---|
105 | def __init__(self, course=None, level=100, mandatory=True): |
---|
106 | self.course = course |
---|
107 | self.level = level |
---|
108 | self.mandatory = mandatory |
---|
109 | |
---|
110 | def getCourseCode(self): |
---|
111 | """Get code of a course. |
---|
112 | """ |
---|
113 | return self.course.code |
---|
114 | |
---|
115 | def longtitle(self): |
---|
116 | return "%s in level %s" % (self.course.code, |
---|
117 | course_levels.getTerm(self.level).title) |
---|
118 | |
---|
119 | class CertificateCourseFactory(grok.GlobalUtility): |
---|
120 | """A factory for certificate courses. |
---|
121 | """ |
---|
122 | grok.implements(IFactory) |
---|
123 | grok.name(u'waeup.CertificateCourse') |
---|
124 | title = u"Create a new certificate course.", |
---|
125 | description = u"This factory instantiates new certificate courses." |
---|
126 | |
---|
127 | def __call__(self, *args, **kw): |
---|
128 | return CertificateCourse(*args, **kw) |
---|
129 | |
---|
130 | def getInterfaces(self): |
---|
131 | return implementedBy(CertificateCourse) |
---|
132 | |
---|
133 | @grok.subscribe(ICertificate, grok.IObjectRemovedEvent) |
---|
134 | def handle_certificate_removed(certificate, event): |
---|
135 | """If a certificate is deleted, we make sure that also referrers to |
---|
136 | student studycourse objects are removed. |
---|
137 | """ |
---|
138 | code = certificate.code |
---|
139 | |
---|
140 | # Find all student studycourses that refer to given certificate... |
---|
141 | try: |
---|
142 | cat = getUtility(ICatalog, name='students_catalog') |
---|
143 | except ComponentLookupError: |
---|
144 | # catalog not available. This might happen during tests. |
---|
145 | return |
---|
146 | |
---|
147 | results = cat.searchResults(certcode=(code, code)) |
---|
148 | for student in results: |
---|
149 | # Remove that referrer... |
---|
150 | studycourse = student['studycourse'] |
---|
151 | studycourse.certificate = None |
---|
152 | notify(grok.ObjectModifiedEvent(student)) |
---|
153 | student.__parent__.logger.info( |
---|
154 | 'ObjectRemovedEvent - %s - removed: certificate' % student.__name__) |
---|
155 | return |
---|
156 | |
---|
157 | class CertificatesPlugin(grok.GlobalUtility): |
---|
158 | """A plugin that updates certificates. |
---|
159 | """ |
---|
160 | |
---|
161 | grok.implements(IKofaPluggable) |
---|
162 | grok.name('certificates') |
---|
163 | |
---|
164 | deprecated_attributes = [] |
---|
165 | |
---|
166 | def setup(self, site, name, logger): |
---|
167 | return |
---|
168 | |
---|
169 | def update(self, site, name, logger): |
---|
170 | cat = getUtility(ICatalog, name='certificates_catalog') |
---|
171 | results = cat.apply({'code':(None,None)}) |
---|
172 | uidutil = getUtility(IIntIds, context=cat) |
---|
173 | items = getFields(ICertificate).items() |
---|
174 | for r in results: |
---|
175 | o = uidutil.getObject(r) |
---|
176 | # Add new attributes |
---|
177 | for i in items: |
---|
178 | if not hasattr(o,i[0]): |
---|
179 | setattr(o,i[0],i[1].missing_value) |
---|
180 | logger.info( |
---|
181 | 'CertificatesPlugin: %s attribute %s added.' % ( |
---|
182 | o.code,i[0])) |
---|
183 | # Remove deprecated attributes |
---|
184 | for i in self.deprecated_attributes: |
---|
185 | try: |
---|
186 | delattr(o,i) |
---|
187 | logger.info( |
---|
188 | 'CertificatesPlugin: %s attribute %s deleted.' % ( |
---|
189 | o.code,i)) |
---|
190 | except AttributeError: |
---|
191 | pass |
---|
192 | return |
---|