[7195] | 1 | ## $Id: certificate.py 9826 2013-01-05 14:07:47Z 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 | ## |
---|
[7819] | 18 | """Kofa certificates |
---|
[4789] | 19 | """ |
---|
| 20 | import grok |
---|
[7209] | 21 | from zope.event import notify |
---|
[6296] | 22 | from zope.catalog.interfaces import ICatalog |
---|
[8299] | 23 | from zope.intid.interfaces import IIntIds |
---|
| 24 | from zope.schema import getFields |
---|
[4789] | 25 | from zope.component import getUtility |
---|
| 26 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
| 27 | from zope.interface import implementedBy |
---|
[8299] | 28 | from waeup.kofa.interfaces import IKofaPluggable |
---|
[7811] | 29 | from waeup.kofa.university.interfaces import ( |
---|
[7207] | 30 | ICertificate, ICertificateAdd, ICertificateCourse) |
---|
[7811] | 31 | from waeup.kofa.university.vocabularies import course_levels |
---|
[4789] | 32 | |
---|
| 33 | class Certificate(grok.Container): |
---|
| 34 | """A certificate. |
---|
| 35 | """ |
---|
[5953] | 36 | grok.implements(ICertificate, ICertificateAdd) |
---|
[4789] | 37 | |
---|
[8993] | 38 | local_roles = [ |
---|
| 39 | 'waeup.local.CourseAdviser100', |
---|
| 40 | 'waeup.local.CourseAdviser200', |
---|
| 41 | 'waeup.local.CourseAdviser300', |
---|
| 42 | 'waeup.local.CourseAdviser400', |
---|
| 43 | 'waeup.local.CourseAdviser500', |
---|
| 44 | 'waeup.local.CourseAdviser600', |
---|
| 45 | ] |
---|
[7334] | 46 | |
---|
[4789] | 47 | def __init__(self, code=u'NA', title=u'Unnamed Certificate', |
---|
[4993] | 48 | study_mode=None, start_level=None, |
---|
[8299] | 49 | end_level=None, application_category=None, |
---|
[8967] | 50 | school_fee_1=None, school_fee_2=None, |
---|
| 51 | school_fee_3=None, school_fee_4=None): |
---|
[4789] | 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 |
---|
[8299] | 59 | self.school_fee_1 = school_fee_1 |
---|
| 60 | self.school_fee_2 = school_fee_2 |
---|
[8967] | 61 | self.school_fee_3 = school_fee_3 |
---|
| 62 | self.school_fee_4 = school_fee_4 |
---|
[4789] | 63 | |
---|
[6008] | 64 | def longtitle(self): |
---|
[7349] | 65 | return "%s (%s)" % (self.title,self.code) |
---|
| 66 | |
---|
[8920] | 67 | def addCertCourse(self, course, level=100, mandatory=True): |
---|
| 68 | """Add a certificate course. |
---|
[4789] | 69 | """ |
---|
| 70 | code = "%s_%s" % (course.code, level) |
---|
[7665] | 71 | self[code] = CertificateCourse(course, level, mandatory) |
---|
[4789] | 72 | self[code].__parent__ = self |
---|
| 73 | self[code].__name__ = code |
---|
| 74 | self._p_changed = True |
---|
| 75 | |
---|
[9826] | 76 | def delCertCourses(self, code, level=None): |
---|
[9824] | 77 | """Delete certificate courses. |
---|
| 78 | |
---|
| 79 | We might have more than one certificate course for a course. |
---|
| 80 | If level is not provided all certificate courses referring |
---|
| 81 | to the same course will be deleted. |
---|
[4789] | 82 | """ |
---|
[6980] | 83 | keys = list(self.keys()) # create list copy |
---|
| 84 | for key in keys: |
---|
[4789] | 85 | if self[key].getCourseCode() != code: |
---|
| 86 | continue |
---|
| 87 | if level is not None and str(self[key].level) != str(level): |
---|
| 88 | # found a course with correct key but wrong level... |
---|
| 89 | continue |
---|
| 90 | del self[key] |
---|
| 91 | self._p_changed = True |
---|
| 92 | return |
---|
| 93 | |
---|
[9341] | 94 | def moveCertificate(self, fac, dep): |
---|
| 95 | self.moved = True |
---|
| 96 | cert = self |
---|
| 97 | del self.__parent__[cert.code] |
---|
| 98 | grok.getSite()['faculties'][fac][dep].certificates[cert.code] = cert |
---|
| 99 | self.__parent__._p_changed = True |
---|
| 100 | cat = getUtility(ICatalog, name='students_catalog') |
---|
| 101 | results = cat.searchResults(certcode=(cert.code, cert.code)) |
---|
| 102 | for student in results: |
---|
| 103 | notify(grok.ObjectModifiedEvent(student)) |
---|
| 104 | student.__parent__.logger.info( |
---|
| 105 | '%s - Certificate moved' % student.__name__) |
---|
| 106 | |
---|
| 107 | return |
---|
| 108 | |
---|
[4789] | 109 | class CertificateFactory(grok.GlobalUtility): |
---|
| 110 | """A factory for certificates. |
---|
| 111 | """ |
---|
| 112 | grok.implements(IFactory) |
---|
| 113 | grok.name(u'waeup.Certificate') |
---|
| 114 | title = u"Create a new certificate.", |
---|
| 115 | description = u"This factory instantiates new certificate instances." |
---|
| 116 | |
---|
| 117 | def __call__(self, *args, **kw): |
---|
| 118 | return Certificate(*args, **kw) |
---|
| 119 | |
---|
| 120 | def getInterfaces(self): |
---|
| 121 | return implementedBy(Certificate) |
---|
| 122 | |
---|
| 123 | class CertificateCourse(grok.Model): |
---|
| 124 | grok.implements(ICertificateCourse) |
---|
| 125 | |
---|
[7665] | 126 | def __init__(self, course=None, level=100, mandatory=True): |
---|
[4789] | 127 | self.course = course |
---|
| 128 | self.level = level |
---|
[7665] | 129 | self.mandatory = mandatory |
---|
[4789] | 130 | |
---|
| 131 | def getCourseCode(self): |
---|
| 132 | """Get code of a course. |
---|
| 133 | """ |
---|
| 134 | return self.course.code |
---|
[7349] | 135 | |
---|
[6008] | 136 | def longtitle(self): |
---|
| 137 | return "%s in level %s" % (self.course.code, |
---|
[7349] | 138 | course_levels.getTerm(self.level).title) |
---|
| 139 | |
---|
[4789] | 140 | class CertificateCourseFactory(grok.GlobalUtility): |
---|
| 141 | """A factory for certificate courses. |
---|
| 142 | """ |
---|
| 143 | grok.implements(IFactory) |
---|
| 144 | grok.name(u'waeup.CertificateCourse') |
---|
| 145 | title = u"Create a new certificate course.", |
---|
| 146 | description = u"This factory instantiates new certificate courses." |
---|
| 147 | |
---|
| 148 | def __call__(self, *args, **kw): |
---|
| 149 | return CertificateCourse(*args, **kw) |
---|
| 150 | |
---|
| 151 | def getInterfaces(self): |
---|
| 152 | return implementedBy(CertificateCourse) |
---|
[7209] | 153 | |
---|
| 154 | @grok.subscribe(ICertificate, grok.IObjectRemovedEvent) |
---|
| 155 | def handle_certificate_removed(certificate, event): |
---|
| 156 | """If a certificate is deleted, we make sure that also referrers to |
---|
| 157 | student studycourse objects are removed. |
---|
| 158 | """ |
---|
[9341] | 159 | # Do not remove referrer if certificate is going to move |
---|
| 160 | if getattr(certificate, 'moved', False): |
---|
| 161 | return |
---|
| 162 | |
---|
[7209] | 163 | code = certificate.code |
---|
| 164 | |
---|
| 165 | # Find all student studycourses that refer to given certificate... |
---|
| 166 | try: |
---|
| 167 | cat = getUtility(ICatalog, name='students_catalog') |
---|
| 168 | except ComponentLookupError: |
---|
| 169 | # catalog not available. This might happen during tests. |
---|
| 170 | return |
---|
| 171 | |
---|
| 172 | results = cat.searchResults(certcode=(code, code)) |
---|
| 173 | for student in results: |
---|
| 174 | # Remove that referrer... |
---|
| 175 | studycourse = student['studycourse'] |
---|
| 176 | studycourse.certificate = None |
---|
| 177 | notify(grok.ObjectModifiedEvent(student)) |
---|
[8737] | 178 | student.__parent__.logger.info( |
---|
| 179 | 'ObjectRemovedEvent - %s - removed: certificate' % student.__name__) |
---|
[7349] | 180 | return |
---|
[8299] | 181 | |
---|
| 182 | class CertificatesPlugin(grok.GlobalUtility): |
---|
| 183 | """A plugin that updates certificates. |
---|
| 184 | """ |
---|
| 185 | |
---|
| 186 | grok.implements(IKofaPluggable) |
---|
| 187 | grok.name('certificates') |
---|
| 188 | |
---|
[8310] | 189 | deprecated_attributes = [] |
---|
| 190 | |
---|
[8299] | 191 | def setup(self, site, name, logger): |
---|
| 192 | return |
---|
| 193 | |
---|
| 194 | def update(self, site, name, logger): |
---|
| 195 | cat = getUtility(ICatalog, name='certificates_catalog') |
---|
| 196 | results = cat.apply({'code':(None,None)}) |
---|
| 197 | uidutil = getUtility(IIntIds, context=cat) |
---|
| 198 | items = getFields(ICertificate).items() |
---|
| 199 | for r in results: |
---|
| 200 | o = uidutil.getObject(r) |
---|
[8310] | 201 | # Add new attributes |
---|
[8299] | 202 | for i in items: |
---|
[8310] | 203 | if not hasattr(o,i[0]): |
---|
[8299] | 204 | setattr(o,i[0],i[1].missing_value) |
---|
| 205 | logger.info( |
---|
| 206 | 'CertificatesPlugin: %s attribute %s added.' % ( |
---|
| 207 | o.code,i[0])) |
---|
[8310] | 208 | # Remove deprecated attributes |
---|
| 209 | for i in self.deprecated_attributes: |
---|
| 210 | try: |
---|
| 211 | delattr(o,i) |
---|
| 212 | logger.info( |
---|
| 213 | 'CertificatesPlugin: %s attribute %s deleted.' % ( |
---|
| 214 | o.code,i)) |
---|
| 215 | except AttributeError: |
---|
| 216 | pass |
---|
[8299] | 217 | return |
---|