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

Last change on this file since 5053 was 5052, checked in by uli, 15 years ago

BBB imports for older groktoolkit.

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1"""WAeUP portal certificates
2"""
3import grok
4from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
5try:
6    from zope.catalog.interfaces import ICatalog
7except ImportError:
8    # BBB
9    from zope.app.catalog.interfaces import ICatalog
10from zope.component import getUtility
11from zope.component.interfaces import IFactory, ComponentLookupError
12from zope.interface import implementedBy
13from waeup.sirp.university.interfaces import (
14    ICertificate, ICertificateCourse, ICourse)
15
16class Certificate(grok.Container):
17    """A certificate.
18    """
19    grok.implements(ICertificate)
20
21    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
22                 study_mode=None, start_level=None,
23                 end_level=None, application_category=None, max_pass = u''):
24        super(Certificate, self).__init__()
25        self.code = code
26        self.title = title
27        self.study_mode = study_mode
28        self.start_level = start_level
29        self.end_level = end_level
30        self.application_category = application_category
31        self.max_pass = max_pass
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 addCourseRef(self, course, level=100, core_or_elective=True):
61        """Add a course reference.
62        """
63        code = "%s_%s" % (course.code, level)
64        self[code] = CertificateCourse(course, level, core_or_elective)
65        self[code].__parent__ = self
66        self[code].__name__ = code
67        self._p_changed = True
68
69    def delCourseRef(self, code, level=None):
70        """Delete a course denoted by its code.
71        """
72        for key in self.keys():
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
83
84
85class 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
99class 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   
112class CertificateCourseFactory(grok.GlobalUtility):
113    """A factory for certificate courses.
114    """
115    grok.implements(IFactory)
116    grok.name(u'waeup.CertificateCourse')
117    title = u"Create a new certificate course.",
118    description = u"This factory instantiates new certificate courses."
119
120    def __call__(self, *args, **kw):
121        return CertificateCourse(*args, **kw)
122
123    def getInterfaces(self):
124        return implementedBy(CertificateCourse)
125
126@grok.subscribe(ICourse, grok.IObjectRemovedEvent)
127def removedCourseHandler(course, event):
128    """If a course is deleted, we make sure that also references in a
129       certificatecontainer are removed.
130    """
131    code = course.code
132
133    # Find all certcourses that reference the given course...
134    try:
135        cat = getUtility(ICatalog, name='certcourses_catalog')
136    except ComponentLookupError:
137        # catalog not available. This might happen during tests.
138        return
139       
140    results = cat.searchResults(course_code=(code, code))
141    for certcourse in results:
142        # Remove that reference...
143        cert = certcourse.__parent__
144        cert.delCourseRef(code)
145        cert._p_changed = True
146    return
Note: See TracBrowser for help on using the repository browser.