1 | """WAeUP portal certificates |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
5 | try: |
---|
6 | from zope.catalog.interfaces import ICatalog |
---|
7 | except ImportError: |
---|
8 | # BBB |
---|
9 | from zope.app.catalog.interfaces import ICatalog |
---|
10 | from zope.component import getUtility |
---|
11 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
12 | from zope.interface import implementedBy |
---|
13 | from waeup.sirp.university.interfaces import ( |
---|
14 | ICertificate, ICertificateAdd, ICertificateCourse, ICourse) |
---|
15 | from waeup.sirp.university.vocabularies import course_levels |
---|
16 | |
---|
17 | class Certificate(grok.Container): |
---|
18 | """A certificate. |
---|
19 | """ |
---|
20 | grok.implements(ICertificate, ICertificateAdd) |
---|
21 | |
---|
22 | def __init__(self, code=u'NA', title=u'Unnamed Certificate', |
---|
23 | study_mode=None, start_level=None, |
---|
24 | end_level=None, application_category=None): |
---|
25 | super(Certificate, self).__init__() |
---|
26 | self.code = code |
---|
27 | self.title = title |
---|
28 | self.study_mode = study_mode |
---|
29 | self.start_level = start_level |
---|
30 | self.end_level = end_level |
---|
31 | self.application_category = application_category |
---|
32 | # Initialize workflow state... |
---|
33 | # self.setReviewState('unchecked') |
---|
34 | |
---|
35 | #def check(self): |
---|
36 | # """Set Review State to ``checked``. |
---|
37 | # """ |
---|
38 | # self.setReviewState(state='checked') |
---|
39 | |
---|
40 | #def getReviewState(self): |
---|
41 | # return IWorkflowState(self).getState() |
---|
42 | |
---|
43 | #def setReviewState(self, state=None): |
---|
44 | # """Try to set new state. |
---|
45 | |
---|
46 | # Instead of simply setting a value, we fire a transition of a |
---|
47 | # workflow. This might fail if, for instance, the required state |
---|
48 | # is not part of the workflow or unreachable from current state. |
---|
49 | # """ |
---|
50 | # if state == self.getReviewState(): |
---|
51 | # # Accept staying in the same state, even if workflow does |
---|
52 | # # not allow this. |
---|
53 | # return |
---|
54 | # info = IWorkflowInfo(self) |
---|
55 | # info.fireTransitionToward(state) |
---|
56 | # return |
---|
57 | |
---|
58 | #review_state = property(getReviewState, setReviewState) |
---|
59 | |
---|
60 | def longtitle(self): |
---|
61 | return "%s (%s)" % (self.title,self.code) |
---|
62 | |
---|
63 | def addCourseRef(self, course, level=100, core_or_elective=True): |
---|
64 | """Add a course referrer. |
---|
65 | """ |
---|
66 | code = "%s_%s" % (course.code, level) |
---|
67 | self[code] = CertificateCourse(course, level, core_or_elective) |
---|
68 | self[code].__parent__ = self |
---|
69 | self[code].__name__ = code |
---|
70 | self._p_changed = True |
---|
71 | |
---|
72 | def delCourseRef(self, code, level=None): |
---|
73 | """Delete a course referrer denoted by its code. |
---|
74 | """ |
---|
75 | for key in self.keys(): |
---|
76 | if self[key].getCourseCode() != code: |
---|
77 | continue |
---|
78 | if level is not None and str(self[key].level) != str(level): |
---|
79 | # found a course with correct key but wrong level... |
---|
80 | continue |
---|
81 | del self[key] |
---|
82 | self._p_changed = True |
---|
83 | return |
---|
84 | |
---|
85 | class CertificateFactory(grok.GlobalUtility): |
---|
86 | """A factory for certificates. |
---|
87 | """ |
---|
88 | grok.implements(IFactory) |
---|
89 | grok.name(u'waeup.Certificate') |
---|
90 | title = u"Create a new certificate.", |
---|
91 | description = u"This factory instantiates new certificate instances." |
---|
92 | |
---|
93 | def __call__(self, *args, **kw): |
---|
94 | return Certificate(*args, **kw) |
---|
95 | |
---|
96 | def getInterfaces(self): |
---|
97 | return implementedBy(Certificate) |
---|
98 | |
---|
99 | class CertificateCourse(grok.Model): |
---|
100 | grok.implements(ICertificateCourse) |
---|
101 | |
---|
102 | def __init__(self, course=None, level=100, core_or_elective=True): |
---|
103 | self.course = course |
---|
104 | self.level = level |
---|
105 | self.core_or_elective = core_or_elective |
---|
106 | |
---|
107 | def getCourseCode(self): |
---|
108 | """Get code of a course. |
---|
109 | """ |
---|
110 | return self.course.code |
---|
111 | |
---|
112 | def longtitle(self): |
---|
113 | return "%s in level %s" % (self.course.code, |
---|
114 | course_levels.getTerm(self.level).title) |
---|
115 | |
---|
116 | class CertificateCourseFactory(grok.GlobalUtility): |
---|
117 | """A factory for certificate courses. |
---|
118 | """ |
---|
119 | grok.implements(IFactory) |
---|
120 | grok.name(u'waeup.CertificateCourse') |
---|
121 | title = u"Create a new certificate course.", |
---|
122 | description = u"This factory instantiates new certificate courses." |
---|
123 | |
---|
124 | def __call__(self, *args, **kw): |
---|
125 | return CertificateCourse(*args, **kw) |
---|
126 | |
---|
127 | def getInterfaces(self): |
---|
128 | return implementedBy(CertificateCourse) |
---|
129 | |
---|
130 | @grok.subscribe(ICourse, grok.IObjectRemovedEvent) |
---|
131 | def removedCourseHandler(course, event): |
---|
132 | """If a course is deleted, we make sure that also referrers in a |
---|
133 | certificatecontainer are removed. |
---|
134 | """ |
---|
135 | code = course.code |
---|
136 | |
---|
137 | # Find all certificatecourses that refer to given course... |
---|
138 | try: |
---|
139 | cat = getUtility(ICatalog, name='certcourses_catalog') |
---|
140 | except ComponentLookupError: |
---|
141 | # catalog not available. This might happen during tests. |
---|
142 | return |
---|
143 | |
---|
144 | results = cat.searchResults(course_code=(code, code)) |
---|
145 | for certcourse in results: |
---|
146 | # Remove that referrer... |
---|
147 | cert = certcourse.__parent__ |
---|
148 | cert.delCourseRef(code) |
---|
149 | cert._p_changed = True |
---|
150 | return |
---|