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

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

Use different msave method in students and in university.

Simplify logging. We don't need the logger_info method.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1## $Id: certificate.py 8737 2012-06-17 07:32:08Z 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
88    #def writeLogMessage(self, view, message):
89    #    ob_class = view.__implemented__.__name__.replace('waeup.kofa.','')
90    #    return self.__parent__.logger_info(ob_class,self.__name__,comment=None)
91
92class CertificateFactory(grok.GlobalUtility):
93    """A factory for certificates.
94    """
95    grok.implements(IFactory)
96    grok.name(u'waeup.Certificate')
97    title = u"Create a new certificate.",
98    description = u"This factory instantiates new certificate instances."
99
100    def __call__(self, *args, **kw):
101        return Certificate(*args, **kw)
102
103    def getInterfaces(self):
104        return implementedBy(Certificate)
105
106class CertificateCourse(grok.Model):
107    grok.implements(ICertificateCourse)
108
109    def __init__(self, course=None, level=100, mandatory=True):
110        self.course = course
111        self.level = level
112        self.mandatory = mandatory
113
114    def getCourseCode(self):
115        """Get code of a course.
116        """
117        return self.course.code
118
119    def longtitle(self):
120        return "%s in level %s" % (self.course.code,
121                   course_levels.getTerm(self.level).title)
122
123class CertificateCourseFactory(grok.GlobalUtility):
124    """A factory for certificate courses.
125    """
126    grok.implements(IFactory)
127    grok.name(u'waeup.CertificateCourse')
128    title = u"Create a new certificate course.",
129    description = u"This factory instantiates new certificate courses."
130
131    def __call__(self, *args, **kw):
132        return CertificateCourse(*args, **kw)
133
134    def getInterfaces(self):
135        return implementedBy(CertificateCourse)
136
137@grok.subscribe(ICertificate, grok.IObjectRemovedEvent)
138def handle_certificate_removed(certificate, event):
139    """If a certificate is deleted, we make sure that also referrers to
140    student studycourse objects are removed.
141    """
142    code = certificate.code
143
144    # Find all student studycourses that refer to given certificate...
145    try:
146        cat = getUtility(ICatalog, name='students_catalog')
147    except ComponentLookupError:
148        # catalog not available. This might happen during tests.
149        return
150
151    results = cat.searchResults(certcode=(code, code))
152    for student in results:
153        # Remove that referrer...
154        studycourse = student['studycourse']
155        studycourse.certificate = None
156        notify(grok.ObjectModifiedEvent(student))
157        student.__parent__.logger.info(
158            'ObjectRemovedEvent - %s - removed: certificate' % student.__name__)
159    return
160
161class CertificatesPlugin(grok.GlobalUtility):
162    """A plugin that updates certificates.
163    """
164
165    grok.implements(IKofaPluggable)
166    grok.name('certificates')
167
168    deprecated_attributes = []
169
170    def setup(self, site, name, logger):
171        return
172
173    def update(self, site, name, logger):
174        cat = getUtility(ICatalog, name='certificates_catalog')
175        results = cat.apply({'code':(None,None)})
176        uidutil = getUtility(IIntIds, context=cat)
177        items = getFields(ICertificate).items()
178        for r in results:
179            o = uidutil.getObject(r)
180            # Add new attributes
181            for i in items:
182                if not hasattr(o,i[0]):
183                    setattr(o,i[0],i[1].missing_value)
184                    logger.info(
185                        'CertificatesPlugin: %s attribute %s added.' % (
186                        o.code,i[0]))
187            # Remove deprecated attributes
188            for i in self.deprecated_attributes:
189                try:
190                    delattr(o,i)
191                    logger.info(
192                        'CertificatesPlugin: %s attribute %s deleted.' % (
193                        o.code,i))
194                except AttributeError:
195                    pass
196        return
Note: See TracBrowser for help on using the repository browser.