source: main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/university/certificate.py @ 10021

Last change on this file since 10021 was 9211, checked in by uli, 12 years ago

Rollback r9209. Looks like multiple merges from trunk confuse svn when merging back into trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
RevLine 
[7195]1## $Id: certificate.py 9211 2012-09-21 08:19:35Z 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##
[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
[9211]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                ]
[7334]47
[4789]48    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
[4993]49                 study_mode=None, start_level=None,
[8299]50                 end_level=None, application_category=None,
[9211]51                 school_fee_1=None, school_fee_2=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
[4789]61
[6008]62    def longtitle(self):
[7349]63        return "%s (%s)" % (self.title,self.code)
64
[9211]65    def addCourseRef(self, course, level=100, mandatory=True):
66        """Add a course referrer.
[4789]67        """
68        code = "%s_%s" % (course.code, level)
[7665]69        self[code] = CertificateCourse(course, level, mandatory)
[4789]70        self[code].__parent__ = self
71        self[code].__name__ = code
72        self._p_changed = True
73
[9211]74    def delCourseRef(self, code, level=None):
75        """Delete a course referrer denoted by its code.
[4789]76        """
[6980]77        keys = list(self.keys()) # create list copy
78        for key in keys:
[4789]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
[7665]105    def __init__(self, course=None, level=100, mandatory=True):
[4789]106        self.course = course
107        self.level = level
[7665]108        self.mandatory = mandatory
[4789]109
110    def getCourseCode(self):
111        """Get code of a course.
112        """
113        return self.course.code
[7349]114
[6008]115    def longtitle(self):
116        return "%s in level %s" % (self.course.code,
[7349]117                   course_levels.getTerm(self.level).title)
118
[4789]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)
[7209]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))
[8737]153        student.__parent__.logger.info(
154            'ObjectRemovedEvent - %s - removed: certificate' % student.__name__)
[7349]155    return
[8299]156
157class CertificatesPlugin(grok.GlobalUtility):
158    """A plugin that updates certificates.
159    """
160
161    grok.implements(IKofaPluggable)
162    grok.name('certificates')
163
[8310]164    deprecated_attributes = []
165
[8299]166    def setup(self, site, name, logger):
167        return
168
169    def update(self, site, name, logger):
170        cat = getUtility(ICatalog, name='certificates_catalog')
171        results = cat.apply({'code':(None,None)})
172        uidutil = getUtility(IIntIds, context=cat)
173        items = getFields(ICertificate).items()
174        for r in results:
175            o = uidutil.getObject(r)
[8310]176            # Add new attributes
[8299]177            for i in items:
[8310]178                if not hasattr(o,i[0]):
[8299]179                    setattr(o,i[0],i[1].missing_value)
180                    logger.info(
181                        'CertificatesPlugin: %s attribute %s added.' % (
182                        o.code,i[0]))
[8310]183            # Remove deprecated attributes
184            for i in self.deprecated_attributes:
185                try:
186                    delattr(o,i)
187                    logger.info(
188                        'CertificatesPlugin: %s attribute %s deleted.' % (
189                        o.code,i))
190                except AttributeError:
191                    pass
[8299]192        return
Note: See TracBrowser for help on using the repository browser.