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

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

school_fee_1=None, school_fee_2=None

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