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

Last change on this file since 9098 was 8993, checked in by Henrik Bettermann, 12 years ago

local_roles should be a class attribute.

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