[4789] | 1 | """WAeUP portal certificates |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[6296] | 4 | from zope.catalog.interfaces import ICatalog |
---|
[4789] | 5 | from zope.component import getUtility |
---|
| 6 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
| 7 | from zope.interface import implementedBy |
---|
[5005] | 8 | from waeup.sirp.university.interfaces import ( |
---|
[5953] | 9 | ICertificate, ICertificateAdd, ICertificateCourse, ICourse) |
---|
[6008] | 10 | from waeup.sirp.university.vocabularies import course_levels |
---|
[4789] | 11 | |
---|
| 12 | class Certificate(grok.Container): |
---|
| 13 | """A certificate. |
---|
| 14 | """ |
---|
[5953] | 15 | grok.implements(ICertificate, ICertificateAdd) |
---|
[4789] | 16 | |
---|
| 17 | def __init__(self, code=u'NA', title=u'Unnamed Certificate', |
---|
[4993] | 18 | study_mode=None, start_level=None, |
---|
[5947] | 19 | end_level=None, application_category=None): |
---|
[4789] | 20 | super(Certificate, self).__init__() |
---|
| 21 | self.code = code |
---|
| 22 | self.title = title |
---|
| 23 | self.study_mode = study_mode |
---|
| 24 | self.start_level = start_level |
---|
| 25 | self.end_level = end_level |
---|
| 26 | self.application_category = application_category |
---|
| 27 | |
---|
[6008] | 28 | def longtitle(self): |
---|
| 29 | return "%s (%s)" % (self.title,self.code) |
---|
| 30 | |
---|
[4789] | 31 | def addCourseRef(self, course, level=100, core_or_elective=True): |
---|
[5977] | 32 | """Add a course referrer. |
---|
[4789] | 33 | """ |
---|
| 34 | code = "%s_%s" % (course.code, level) |
---|
| 35 | self[code] = CertificateCourse(course, level, core_or_elective) |
---|
| 36 | self[code].__parent__ = self |
---|
| 37 | self[code].__name__ = code |
---|
| 38 | self._p_changed = True |
---|
| 39 | |
---|
| 40 | def delCourseRef(self, code, level=None): |
---|
[5977] | 41 | """Delete a course referrer denoted by its code. |
---|
[4789] | 42 | """ |
---|
[6980] | 43 | keys = list(self.keys()) # create list copy |
---|
| 44 | for key in keys: |
---|
[4789] | 45 | if self[key].getCourseCode() != code: |
---|
| 46 | continue |
---|
| 47 | if level is not None and str(self[key].level) != str(level): |
---|
| 48 | # found a course with correct key but wrong level... |
---|
| 49 | continue |
---|
| 50 | del self[key] |
---|
| 51 | self._p_changed = True |
---|
| 52 | return |
---|
| 53 | |
---|
| 54 | class CertificateFactory(grok.GlobalUtility): |
---|
| 55 | """A factory for certificates. |
---|
| 56 | """ |
---|
| 57 | grok.implements(IFactory) |
---|
| 58 | grok.name(u'waeup.Certificate') |
---|
| 59 | title = u"Create a new certificate.", |
---|
| 60 | description = u"This factory instantiates new certificate instances." |
---|
| 61 | |
---|
| 62 | def __call__(self, *args, **kw): |
---|
| 63 | return Certificate(*args, **kw) |
---|
| 64 | |
---|
| 65 | def getInterfaces(self): |
---|
| 66 | return implementedBy(Certificate) |
---|
| 67 | |
---|
| 68 | class CertificateCourse(grok.Model): |
---|
| 69 | grok.implements(ICertificateCourse) |
---|
| 70 | |
---|
| 71 | def __init__(self, course=None, level=100, core_or_elective=True): |
---|
| 72 | self.course = course |
---|
| 73 | self.level = level |
---|
| 74 | self.core_or_elective = core_or_elective |
---|
| 75 | |
---|
| 76 | def getCourseCode(self): |
---|
| 77 | """Get code of a course. |
---|
| 78 | """ |
---|
| 79 | return self.course.code |
---|
[6008] | 80 | |
---|
| 81 | def longtitle(self): |
---|
| 82 | return "%s in level %s" % (self.course.code, |
---|
| 83 | course_levels.getTerm(self.level).title) |
---|
[4789] | 84 | |
---|
| 85 | class CertificateCourseFactory(grok.GlobalUtility): |
---|
| 86 | """A factory for certificate courses. |
---|
| 87 | """ |
---|
| 88 | grok.implements(IFactory) |
---|
| 89 | grok.name(u'waeup.CertificateCourse') |
---|
| 90 | title = u"Create a new certificate course.", |
---|
| 91 | description = u"This factory instantiates new certificate courses." |
---|
| 92 | |
---|
| 93 | def __call__(self, *args, **kw): |
---|
| 94 | return CertificateCourse(*args, **kw) |
---|
| 95 | |
---|
| 96 | def getInterfaces(self): |
---|
| 97 | return implementedBy(CertificateCourse) |
---|
| 98 | |
---|
| 99 | @grok.subscribe(ICourse, grok.IObjectRemovedEvent) |
---|
[6839] | 100 | def handle_course_removed(course, event): |
---|
[5977] | 101 | """If a course is deleted, we make sure that also referrers in a |
---|
[4789] | 102 | certificatecontainer are removed. |
---|
| 103 | """ |
---|
| 104 | code = course.code |
---|
| 105 | |
---|
[5977] | 106 | # Find all certificatecourses that refer to given course... |
---|
[4789] | 107 | try: |
---|
| 108 | cat = getUtility(ICatalog, name='certcourses_catalog') |
---|
| 109 | except ComponentLookupError: |
---|
| 110 | # catalog not available. This might happen during tests. |
---|
| 111 | return |
---|
| 112 | |
---|
| 113 | results = cat.searchResults(course_code=(code, code)) |
---|
| 114 | for certcourse in results: |
---|
[5977] | 115 | # Remove that referrer... |
---|
[4789] | 116 | cert = certcourse.__parent__ |
---|
| 117 | cert.delCourseRef(code) |
---|
| 118 | cert._p_changed = True |
---|
| 119 | return |
---|