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

Last change on this file since 4990 was 4920, checked in by uli, 15 years ago

Make unit tests run again with the new package layout.

  • 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.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                 category=None, 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.category = category
23        self.study_mode = study_mode
24        self.start_level = start_level
25        self.end_level = end_level
26        self.application_category = application_category
27        self.max_pass = max_pass
28        # Initialize workflow state...
29        self.setReviewState('unchecked')
30
31    def check(self):
32        """Set Review State to ``checked``.
33        """
34        self.setReviewState(state='checked')
35
36    def getReviewState(self):
37        return IWorkflowState(self).getState()
38
39    def setReviewState(self, state=None):
40        """Try to set new state.
41
42        Instead of simply setting a value, we fire a transition of a
43        workflow. This might fail if, for instance, the required state
44        is not part of the workflow or unreachable from current state.
45        """
46        if state == self.getReviewState():
47            # Accept staying in the same state, even if workflow does
48            # not allow this.
49            return
50        info = IWorkflowInfo(self)
51        info.fireTransitionToward(state)
52        return
53
54    review_state = property(getReviewState, setReviewState)
55   
56    def addCourseRef(self, course, level=100, core_or_elective=True):
57        """Add a course reference.
58        """
59        code = "%s_%s" % (course.code, level)
60        self[code] = CertificateCourse(course, level, core_or_elective)
61        self[code].__parent__ = self
62        self[code].__name__ = code
63        self._p_changed = True
64
65    def delCourseRef(self, code, level=None):
66        """Delete a course denoted by its code.
67        """
68        for key in self.keys():
69            if self[key].getCourseCode() != code:
70                continue
71            if level is not None and str(self[key].level) != str(level):
72                # found a course with correct key but wrong level...
73                continue
74            del self[key]
75            self._p_changed = True
76        return
77
78
79
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):
124    """If a course is deleted, we make sure that also references in a
125       certificatecontainer are removed.
126    """
127    code = course.code
128
129    # Find all certcourses that reference the given course...
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:
138        # Remove that reference...
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.