source: main/waeup.sirp/trunk/src/waeup/sirp/university/certificate.py @ 7445

Last change on this file since 7445 was 7349, checked in by uli, 13 years ago

As I am here: remove trailing whitespace.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
RevLine 
[7195]1## $Id: certificate.py 7349 2011-12-15 11:18:39Z 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##
[7321]18"""SIRP certificates
[4789]19"""
20import grok
[7209]21from zope.event import notify
[6296]22from zope.catalog.interfaces import ICatalog
[4789]23from zope.component import getUtility
24from zope.component.interfaces import IFactory, ComponentLookupError
25from zope.interface import implementedBy
[5005]26from waeup.sirp.university.interfaces import (
[7207]27    ICertificate, ICertificateAdd, ICertificateCourse)
[7349]28from waeup.sirp.university.vocabularies import course_levels
[4789]29
30class Certificate(grok.Container):
31    """A certificate.
32    """
[5953]33    grok.implements(ICertificate, ICertificateAdd)
[4789]34
[7334]35    @property       # Make this method read_only and looking like an attr.
36    def local_roles(cls):
37        return ['waeup.local.CourseAdviser100',
38                'waeup.local.CourseAdviser200',
39                'waeup.local.CourseAdviser300',
40                'waeup.local.CourseAdviser400',
41                'waeup.local.CourseAdviser500',
42                'waeup.local.CourseAdviser600',
43                ]
44
[4789]45    def __init__(self, code=u'NA', title=u'Unnamed Certificate',
[4993]46                 study_mode=None, start_level=None,
[5947]47                 end_level=None, application_category=None):
[4789]48        super(Certificate, self).__init__()
49        self.code = code
50        self.title = title
51        self.study_mode = study_mode
52        self.start_level = start_level
53        self.end_level = end_level
54        self.application_category = application_category
55
[6008]56    def longtitle(self):
[7349]57        return "%s (%s)" % (self.title,self.code)
58
[4789]59    def addCourseRef(self, course, level=100, core_or_elective=True):
[5977]60        """Add a course referrer.
[4789]61        """
62        code = "%s_%s" % (course.code, level)
63        self[code] = CertificateCourse(course, level, core_or_elective)
64        self[code].__parent__ = self
65        self[code].__name__ = code
66        self._p_changed = True
67
68    def delCourseRef(self, code, level=None):
[5977]69        """Delete a course referrer denoted by its code.
[4789]70        """
[6980]71        keys = list(self.keys()) # create list copy
72        for key in keys:
[4789]73            if self[key].getCourseCode() != code:
74                continue
75            if level is not None and str(self[key].level) != str(level):
76                # found a course with correct key but wrong level...
77                continue
78            del self[key]
79            self._p_changed = True
80        return
81
82class CertificateFactory(grok.GlobalUtility):
83    """A factory for certificates.
84    """
85    grok.implements(IFactory)
86    grok.name(u'waeup.Certificate')
87    title = u"Create a new certificate.",
88    description = u"This factory instantiates new certificate instances."
89
90    def __call__(self, *args, **kw):
91        return Certificate(*args, **kw)
92
93    def getInterfaces(self):
94        return implementedBy(Certificate)
95
96class CertificateCourse(grok.Model):
97    grok.implements(ICertificateCourse)
98
99    def __init__(self, course=None, level=100, core_or_elective=True):
100        self.course = course
101        self.level = level
102        self.core_or_elective = core_or_elective
103
104    def getCourseCode(self):
105        """Get code of a course.
106        """
107        return self.course.code
[7349]108
[6008]109    def longtitle(self):
110        return "%s in level %s" % (self.course.code,
[7349]111                   course_levels.getTerm(self.level).title)
112
[4789]113class CertificateCourseFactory(grok.GlobalUtility):
114    """A factory for certificate courses.
115    """
116    grok.implements(IFactory)
117    grok.name(u'waeup.CertificateCourse')
118    title = u"Create a new certificate course.",
119    description = u"This factory instantiates new certificate courses."
120
121    def __call__(self, *args, **kw):
122        return CertificateCourse(*args, **kw)
123
124    def getInterfaces(self):
125        return implementedBy(CertificateCourse)
[7209]126
127@grok.subscribe(ICertificate, grok.IObjectRemovedEvent)
128def handle_certificate_removed(certificate, event):
129    """If a certificate is deleted, we make sure that also referrers to
130    student studycourse objects are removed.
131    """
132    code = certificate.code
133
134    # Find all student studycourses that refer to given certificate...
135    try:
136        cat = getUtility(ICatalog, name='students_catalog')
137    except ComponentLookupError:
138        # catalog not available. This might happen during tests.
139        return
140
141    results = cat.searchResults(certcode=(code, code))
142    for student in results:
143        # Remove that referrer...
144        studycourse = student['studycourse']
145        studycourse.certificate = None
146        notify(grok.ObjectModifiedEvent(student))
[7210]147        student.loggerInfo('ObjectRemovedEvent', 'removed: certificate')
[7349]148    return
Note: See TracBrowser for help on using the repository browser.