source: main/waeup.kofa/trunk/src/waeup/kofa/university/certificate.py @ 8643

Last change on this file since 8643 was 8310, checked in by Henrik Bettermann, 13 years ago

Subject for a discussion with Uli.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1## $Id: certificate.py 8310 2012-04-29 13:41:45Z henrik $
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"""Kofa certificates
19"""
20import grok
21from zope.event import notify
22from zope.catalog.interfaces import ICatalog
23from zope.intid.interfaces import IIntIds
24from zope.schema import getFields
25from zope.component import getUtility
26from zope.component.interfaces import IFactory, ComponentLookupError
27from zope.interface import implementedBy
28from waeup.kofa.interfaces import IKofaPluggable
29from waeup.kofa.university.interfaces import (
30    ICertificate, ICertificateAdd, ICertificateCourse)
31from waeup.kofa.university.vocabularies import course_levels
32
33class Certificate(grok.Container):
34    """A certificate.
35    """
36    grok.implements(ICertificate, ICertificateAdd)
37
38    @property       # Make this method read_only and looking like an attr.
39    def local_roles(self):
40        return ['waeup.local.CourseAdviser100',
41                'waeup.local.CourseAdviser200',
42                'waeup.local.CourseAdviser300',
43                'waeup.local.CourseAdviser400',
44                'waeup.local.CourseAdviser500',
45                'waeup.local.CourseAdviser600',
46                ]
47
48    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
49                 study_mode=None, start_level=None,
50                 end_level=None, application_category=None,
51                 school_fee_1=None, school_fee_2=None):
52        super(Certificate, self).__init__()
53        self.code = code
54        self.title = title
55        self.study_mode = study_mode
56        self.start_level = start_level
57        self.end_level = end_level
58        self.application_category = application_category
59        self.school_fee_1 = school_fee_1
60        self.school_fee_2 = school_fee_2
61
62    def longtitle(self):
63        return "%s (%s)" % (self.title,self.code)
64
65    def addCourseRef(self, course, level=100, mandatory=True):
66        """Add a course referrer.
67        """
68        code = "%s_%s" % (course.code, level)
69        self[code] = CertificateCourse(course, level, mandatory)
70        self[code].__parent__ = self
71        self[code].__name__ = code
72        self._p_changed = True
73
74    def delCourseRef(self, code, level=None):
75        """Delete a course referrer denoted by its code.
76        """
77        keys = list(self.keys()) # create list copy
78        for key in keys:
79            if self[key].getCourseCode() != code:
80                continue
81            if level is not None and str(self[key].level) != str(level):
82                # found a course with correct key but wrong level...
83                continue
84            del self[key]
85            self._p_changed = True
86        return
87
88class CertificateFactory(grok.GlobalUtility):
89    """A factory for certificates.
90    """
91    grok.implements(IFactory)
92    grok.name(u'waeup.Certificate')
93    title = u"Create a new certificate.",
94    description = u"This factory instantiates new certificate instances."
95
96    def __call__(self, *args, **kw):
97        return Certificate(*args, **kw)
98
99    def getInterfaces(self):
100        return implementedBy(Certificate)
101
102class CertificateCourse(grok.Model):
103    grok.implements(ICertificateCourse)
104
105    def __init__(self, course=None, level=100, mandatory=True):
106        self.course = course
107        self.level = level
108        self.mandatory = mandatory
109
110    def getCourseCode(self):
111        """Get code of a course.
112        """
113        return self.course.code
114
115    def longtitle(self):
116        return "%s in level %s" % (self.course.code,
117                   course_levels.getTerm(self.level).title)
118
119class CertificateCourseFactory(grok.GlobalUtility):
120    """A factory for certificate courses.
121    """
122    grok.implements(IFactory)
123    grok.name(u'waeup.CertificateCourse')
124    title = u"Create a new certificate course.",
125    description = u"This factory instantiates new certificate courses."
126
127    def __call__(self, *args, **kw):
128        return CertificateCourse(*args, **kw)
129
130    def getInterfaces(self):
131        return implementedBy(CertificateCourse)
132
133@grok.subscribe(ICertificate, grok.IObjectRemovedEvent)
134def handle_certificate_removed(certificate, event):
135    """If a certificate is deleted, we make sure that also referrers to
136    student studycourse objects are removed.
137    """
138    code = certificate.code
139
140    # Find all student studycourses that refer to given certificate...
141    try:
142        cat = getUtility(ICatalog, name='students_catalog')
143    except ComponentLookupError:
144        # catalog not available. This might happen during tests.
145        return
146
147    results = cat.searchResults(certcode=(code, code))
148    for student in results:
149        # Remove that referrer...
150        studycourse = student['studycourse']
151        studycourse.certificate = None
152        notify(grok.ObjectModifiedEvent(student))
153        student.loggerInfo('ObjectRemovedEvent', 'removed: certificate')
154    return
155
156class CertificatesPlugin(grok.GlobalUtility):
157    """A plugin that updates certificates.
158    """
159
160    grok.implements(IKofaPluggable)
161    grok.name('certificates')
162
163    deprecated_attributes = []
164
165    def setup(self, site, name, logger):
166        return
167
168    def update(self, site, name, logger):
169        cat = getUtility(ICatalog, name='certificates_catalog')
170        results = cat.apply({'code':(None,None)})
171        uidutil = getUtility(IIntIds, context=cat)
172        items = getFields(ICertificate).items()
173        for r in results:
174            o = uidutil.getObject(r)
175            # Add new attributes
176            for i in items:
177                if not hasattr(o,i[0]):
178                    setattr(o,i[0],i[1].missing_value)
179                    logger.info(
180                        'CertificatesPlugin: %s attribute %s added.' % (
181                        o.code,i[0]))
182            # Remove deprecated attributes
183            for i in self.deprecated_attributes:
184                try:
185                    delattr(o,i)
186                    logger.info(
187                        'CertificatesPlugin: %s attribute %s deleted.' % (
188                        o.code,i))
189                except AttributeError:
190                    pass
191        return
Note: See TracBrowser for help on using the repository browser.