source: waeup/branches/ulif-namespace/src/sirp/university/certificate.py @ 4791

Last change on this file since 4791 was 4789, checked in by uli, 15 years ago

Merge changes from ulif-layout back into trunk (finally).

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