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

Last change on this file since 11110 was 7751, checked in by uli, 13 years ago

local_roles is still an instance method.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1## $Id: certificate.py 7751 2012-03-02 13:32:47Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""SIRP certificates
19"""
20import grok
21from zope.event import notify
22from zope.catalog.interfaces import ICatalog
23from zope.component import getUtility
24from zope.component.interfaces import IFactory, ComponentLookupError
25from zope.interface import implementedBy
26from waeup.sirp.university.interfaces import (
27    ICertificate, ICertificateAdd, ICertificateCourse)
28from waeup.sirp.university.vocabularies import course_levels
29
30class Certificate(grok.Container):
31    """A certificate.
32    """
33    grok.implements(ICertificate, ICertificateAdd)
34
35    @property       # Make this method read_only and looking like an attr.
36    def local_roles(self):
37        return ['waeup.local.CourseAdviser100',
38                'waeup.local.CourseAdviser200',
39                'waeup.local.CourseAdviser300',
40                'waeup.local.CourseAdviser400',
41                'waeup.local.CourseAdviser500',
42                'waeup.local.CourseAdviser600',
43                ]
44
45    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
46                 study_mode=None, start_level=None,
47                 end_level=None, application_category=None):
48        super(Certificate, self).__init__()
49        self.code = code
50        self.title = title
51        self.study_mode = study_mode
52        self.start_level = start_level
53        self.end_level = end_level
54        self.application_category = application_category
55
56    def longtitle(self):
57        return "%s (%s)" % (self.title,self.code)
58
59    def addCourseRef(self, course, level=100, mandatory=True):
60        """Add a course referrer.
61        """
62        code = "%s_%s" % (course.code, level)
63        self[code] = CertificateCourse(course, level, mandatory)
64        self[code].__parent__ = self
65        self[code].__name__ = code
66        self._p_changed = True
67
68    def delCourseRef(self, code, level=None):
69        """Delete a course referrer denoted by its code.
70        """
71        keys = list(self.keys()) # create list copy
72        for key in 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
82class CertificateFactory(grok.GlobalUtility):
83    """A factory for certificates.
84    """
85    grok.implements(IFactory)
86    grok.name(u'waeup.Certificate')
87    title = u"Create a new certificate.",
88    description = u"This factory instantiates new certificate instances."
89
90    def __call__(self, *args, **kw):
91        return Certificate(*args, **kw)
92
93    def getInterfaces(self):
94        return implementedBy(Certificate)
95
96class CertificateCourse(grok.Model):
97    grok.implements(ICertificateCourse)
98
99    def __init__(self, course=None, level=100, mandatory=True):
100        self.course = course
101        self.level = level
102        self.mandatory = mandatory
103
104    def getCourseCode(self):
105        """Get code of a course.
106        """
107        return self.course.code
108
109    def longtitle(self):
110        return "%s in level %s" % (self.course.code,
111                   course_levels.getTerm(self.level).title)
112
113class CertificateCourseFactory(grok.GlobalUtility):
114    """A factory for certificate courses.
115    """
116    grok.implements(IFactory)
117    grok.name(u'waeup.CertificateCourse')
118    title = u"Create a new certificate course.",
119    description = u"This factory instantiates new certificate courses."
120
121    def __call__(self, *args, **kw):
122        return CertificateCourse(*args, **kw)
123
124    def getInterfaces(self):
125        return implementedBy(CertificateCourse)
126
127@grok.subscribe(ICertificate, grok.IObjectRemovedEvent)
128def handle_certificate_removed(certificate, event):
129    """If a certificate is deleted, we make sure that also referrers to
130    student studycourse objects are removed.
131    """
132    code = certificate.code
133
134    # Find all student studycourses that refer to given certificate...
135    try:
136        cat = getUtility(ICatalog, name='students_catalog')
137    except ComponentLookupError:
138        # catalog not available. This might happen during tests.
139        return
140
141    results = cat.searchResults(certcode=(code, code))
142    for student in results:
143        # Remove that referrer...
144        studycourse = student['studycourse']
145        studycourse.certificate = None
146        notify(grok.ObjectModifiedEvent(student))
147        student.loggerInfo('ObjectRemovedEvent', 'removed: certificate')
148    return
Note: See TracBrowser for help on using the repository browser.