[7195] | 1 | ## $Id: certificate.py 7665 2012-02-17 12:06:10Z 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 | ## |
---|
[7321] | 18 | """SIRP certificates |
---|
[4789] | 19 | """ |
---|
| 20 | import grok |
---|
[7209] | 21 | from zope.event import notify |
---|
[6296] | 22 | from zope.catalog.interfaces import ICatalog |
---|
[4789] | 23 | from zope.component import getUtility |
---|
| 24 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
| 25 | from zope.interface import implementedBy |
---|
[5005] | 26 | from waeup.sirp.university.interfaces import ( |
---|
[7207] | 27 | ICertificate, ICertificateAdd, ICertificateCourse) |
---|
[7349] | 28 | from waeup.sirp.university.vocabularies import course_levels |
---|
[4789] | 29 | |
---|
| 30 | class Certificate(grok.Container): |
---|
| 31 | """A certificate. |
---|
| 32 | """ |
---|
[5953] | 33 | grok.implements(ICertificate, ICertificateAdd) |
---|
[4789] | 34 | |
---|
[7334] | 35 | @property # Make this method read_only and looking like an attr. |
---|
| 36 | def local_roles(cls): |
---|
| 37 | return ['waeup.local.CourseAdviser100', |
---|
| 38 | 'waeup.local.CourseAdviser200', |
---|
| 39 | 'waeup.local.CourseAdviser300', |
---|
| 40 | 'waeup.local.CourseAdviser400', |
---|
| 41 | 'waeup.local.CourseAdviser500', |
---|
| 42 | 'waeup.local.CourseAdviser600', |
---|
| 43 | ] |
---|
| 44 | |
---|
[4789] | 45 | def __init__(self, code=u'NA', title=u'Unnamed Certificate', |
---|
[4993] | 46 | study_mode=None, start_level=None, |
---|
[5947] | 47 | end_level=None, application_category=None): |
---|
[4789] | 48 | super(Certificate, self).__init__() |
---|
| 49 | self.code = code |
---|
| 50 | self.title = title |
---|
| 51 | self.study_mode = study_mode |
---|
| 52 | self.start_level = start_level |
---|
| 53 | self.end_level = end_level |
---|
| 54 | self.application_category = application_category |
---|
| 55 | |
---|
[6008] | 56 | def longtitle(self): |
---|
[7349] | 57 | return "%s (%s)" % (self.title,self.code) |
---|
| 58 | |
---|
[7665] | 59 | def addCourseRef(self, course, level=100, mandatory=True): |
---|
[5977] | 60 | """Add a course referrer. |
---|
[4789] | 61 | """ |
---|
| 62 | code = "%s_%s" % (course.code, level) |
---|
[7665] | 63 | self[code] = CertificateCourse(course, level, mandatory) |
---|
[4789] | 64 | self[code].__parent__ = self |
---|
| 65 | self[code].__name__ = code |
---|
| 66 | self._p_changed = True |
---|
| 67 | |
---|
| 68 | def delCourseRef(self, code, level=None): |
---|
[5977] | 69 | """Delete a course referrer denoted by its code. |
---|
[4789] | 70 | """ |
---|
[6980] | 71 | keys = list(self.keys()) # create list copy |
---|
| 72 | for key in keys: |
---|
[4789] | 73 | if self[key].getCourseCode() != code: |
---|
| 74 | continue |
---|
| 75 | if level is not None and str(self[key].level) != str(level): |
---|
| 76 | # found a course with correct key but wrong level... |
---|
| 77 | continue |
---|
| 78 | del self[key] |
---|
| 79 | self._p_changed = True |
---|
| 80 | return |
---|
| 81 | |
---|
| 82 | class CertificateFactory(grok.GlobalUtility): |
---|
| 83 | """A factory for certificates. |
---|
| 84 | """ |
---|
| 85 | grok.implements(IFactory) |
---|
| 86 | grok.name(u'waeup.Certificate') |
---|
| 87 | title = u"Create a new certificate.", |
---|
| 88 | description = u"This factory instantiates new certificate instances." |
---|
| 89 | |
---|
| 90 | def __call__(self, *args, **kw): |
---|
| 91 | return Certificate(*args, **kw) |
---|
| 92 | |
---|
| 93 | def getInterfaces(self): |
---|
| 94 | return implementedBy(Certificate) |
---|
| 95 | |
---|
| 96 | class CertificateCourse(grok.Model): |
---|
| 97 | grok.implements(ICertificateCourse) |
---|
| 98 | |
---|
[7665] | 99 | def __init__(self, course=None, level=100, mandatory=True): |
---|
[4789] | 100 | self.course = course |
---|
| 101 | self.level = level |
---|
[7665] | 102 | self.mandatory = mandatory |
---|
[4789] | 103 | |
---|
| 104 | def getCourseCode(self): |
---|
| 105 | """Get code of a course. |
---|
| 106 | """ |
---|
| 107 | return self.course.code |
---|
[7349] | 108 | |
---|
[6008] | 109 | def longtitle(self): |
---|
| 110 | return "%s in level %s" % (self.course.code, |
---|
[7349] | 111 | course_levels.getTerm(self.level).title) |
---|
| 112 | |
---|
[4789] | 113 | class CertificateCourseFactory(grok.GlobalUtility): |
---|
| 114 | """A factory for certificate courses. |
---|
| 115 | """ |
---|
| 116 | grok.implements(IFactory) |
---|
| 117 | grok.name(u'waeup.CertificateCourse') |
---|
| 118 | title = u"Create a new certificate course.", |
---|
| 119 | description = u"This factory instantiates new certificate courses." |
---|
| 120 | |
---|
| 121 | def __call__(self, *args, **kw): |
---|
| 122 | return CertificateCourse(*args, **kw) |
---|
| 123 | |
---|
| 124 | def getInterfaces(self): |
---|
| 125 | return implementedBy(CertificateCourse) |
---|
[7209] | 126 | |
---|
| 127 | @grok.subscribe(ICertificate, grok.IObjectRemovedEvent) |
---|
| 128 | def handle_certificate_removed(certificate, event): |
---|
| 129 | """If a certificate is deleted, we make sure that also referrers to |
---|
| 130 | student studycourse objects are removed. |
---|
| 131 | """ |
---|
| 132 | code = certificate.code |
---|
| 133 | |
---|
| 134 | # Find all student studycourses that refer to given certificate... |
---|
| 135 | try: |
---|
| 136 | cat = getUtility(ICatalog, name='students_catalog') |
---|
| 137 | except ComponentLookupError: |
---|
| 138 | # catalog not available. This might happen during tests. |
---|
| 139 | return |
---|
| 140 | |
---|
| 141 | results = cat.searchResults(certcode=(code, code)) |
---|
| 142 | for student in results: |
---|
| 143 | # Remove that referrer... |
---|
| 144 | studycourse = student['studycourse'] |
---|
| 145 | studycourse.certificate = None |
---|
| 146 | notify(grok.ObjectModifiedEvent(student)) |
---|
[7210] | 147 | student.loggerInfo('ObjectRemovedEvent', 'removed: certificate') |
---|
[7349] | 148 | return |
---|