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

Last change on this file since 5988 was 5977, checked in by Henrik Bettermann, 14 years ago

Implement a vocabulary module in university and move some vocabularies to it.

Rebuild the certificatepage page template.

Rename Course Reference to Course Referrer. It's very difficult to find a proper name for these 'certificatecourse' objects. But we have to clearly distinguish courses and referrers of courses as they are not the same.

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