[7195] | 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 | ## |
---|
[4789] | 18 | """WAeUP portal certificates |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
[6296] | 21 | from zope.catalog.interfaces import ICatalog |
---|
[4789] | 22 | from zope.component import getUtility |
---|
| 23 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
| 24 | from zope.interface import implementedBy |
---|
[5005] | 25 | from waeup.sirp.university.interfaces import ( |
---|
[5953] | 26 | ICertificate, ICertificateAdd, ICertificateCourse, ICourse) |
---|
[6008] | 27 | from waeup.sirp.university.vocabularies import course_levels |
---|
[4789] | 28 | |
---|
| 29 | class Certificate(grok.Container): |
---|
| 30 | """A certificate. |
---|
| 31 | """ |
---|
[5953] | 32 | grok.implements(ICertificate, ICertificateAdd) |
---|
[4789] | 33 | |
---|
| 34 | def __init__(self, code=u'NA', title=u'Unnamed Certificate', |
---|
[4993] | 35 | study_mode=None, start_level=None, |
---|
[5947] | 36 | end_level=None, application_category=None): |
---|
[4789] | 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 | |
---|
[6008] | 45 | def longtitle(self): |
---|
| 46 | return "%s (%s)" % (self.title,self.code) |
---|
| 47 | |
---|
[4789] | 48 | def addCourseRef(self, course, level=100, core_or_elective=True): |
---|
[5977] | 49 | """Add a course referrer. |
---|
[4789] | 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): |
---|
[5977] | 58 | """Delete a course referrer denoted by its code. |
---|
[4789] | 59 | """ |
---|
[6980] | 60 | keys = list(self.keys()) # create list copy |
---|
| 61 | for key in keys: |
---|
[4789] | 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 | |
---|
| 71 | class 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 | |
---|
| 85 | class 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 |
---|
[6008] | 97 | |
---|
| 98 | def longtitle(self): |
---|
| 99 | return "%s in level %s" % (self.course.code, |
---|
| 100 | course_levels.getTerm(self.level).title) |
---|
[4789] | 101 | |
---|
| 102 | class 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) |
---|
[6839] | 117 | def handle_course_removed(course, event): |
---|
[5977] | 118 | """If a course is deleted, we make sure that also referrers in a |
---|
[4789] | 119 | certificatecontainer are removed. |
---|
| 120 | """ |
---|
| 121 | code = course.code |
---|
| 122 | |
---|
[5977] | 123 | # Find all certificatecourses that refer to given course... |
---|
[4789] | 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: |
---|
[5977] | 132 | # Remove that referrer... |
---|
[4789] | 133 | cert = certcourse.__parent__ |
---|
| 134 | cert.delCourseRef(code) |
---|
| 135 | cert._p_changed = True |
---|
| 136 | return |
---|