source: main/waeup.sirp/trunk/src/waeup/sirp/university/certificate.py @ 4997

Last change on this file since 4997 was 4993, checked in by Henrik Bettermann, 15 years ago

Remove category attribute.
max_pass and application_category must not be required.

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