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

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

Rename course referrers to certificate courses (according to the discussion long time ago).

Edit some button labels and titles.

Move code from view to content components.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1## $Id: certificate.py 8920 2012-07-05 14:48:51Z 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 addCertCourse(self, course, level=100, mandatory=True):
66        """Add a certificate course.
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 delCertCourse(self, code, level=None):
75        """Delete a certificate course 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.__parent__.logger.info(
154            'ObjectRemovedEvent - %s - removed: certificate' % student.__name__)
155    return
156
157class CertificatesPlugin(grok.GlobalUtility):
158    """A plugin that updates certificates.
159    """
160
161    grok.implements(IKofaPluggable)
162    grok.name('certificates')
163
164    deprecated_attributes = []
165
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)
176            # Add new attributes
177            for i in items:
178                if not hasattr(o,i[0]):
179                    setattr(o,i[0],i[1].missing_value)
180                    logger.info(
181                        'CertificatesPlugin: %s attribute %s added.' % (
182                        o.code,i[0]))
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
192        return
Note: See TracBrowser for help on using the repository browser.