source: main/waeup.kofa/trunk/src/waeup/kofa/university/course.py @ 9554

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

If a former course attribute is set, we make sure that referrers in a
certificatescontainer are removed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.4 KB
RevLine 
[7195]1## $Id: course.py 9227 2012-09-23 12:23:42Z 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##
[4789]18"""Courses.
19"""
20import grok
[7207]21from zope.catalog.interfaces import ICatalog
[4789]22from zope.interface import implementedBy
[9220]23from zope.schema import getFields
24from zope.intid.interfaces import IIntIds
[7207]25from zope.component import getUtility
26from zope.component.interfaces import IFactory, ComponentLookupError
[9220]27from waeup.kofa.interfaces import IKofaPluggable
[7811]28from waeup.kofa.university.interfaces import ICourse, ICourseAdd
[4789]29
30class Course(grok.Model):
31    """A university course.
32    """
[5953]33    grok.implements(ICourse, ICourseAdd)
[4789]34
[9002]35    local_roles = ['waeup.local.Lecturer']
[8996]36
[4789]37    def __init__(self,
38                 title=u'Unnamed Course',
39                 code=u'NA',
40                 credits=0,
41                 passmark=40,
[9220]42                 semester=1,
43                 former_course=False,
44                 **kw):
[4789]45        super(Course, self).__init__(**kw)
46        self.title = title
47        self.code = code
48        self.credits = credits
49        self.passmark = passmark
50        self.semester = semester
[9220]51        self.former_course = former_course
[6566]52
[6008]53    def longtitle(self):
[6566]54        return "%s (%s)" % (self.title,self.code)
55
[4789]56class CourseFactory(grok.GlobalUtility):
57    """A factory for courses.
58    """
59    grok.implements(IFactory)
60    grok.name(u'waeup.Course')
61    title = u"Create a new course.",
62    description = u"This factory instantiates new course instances."
63
64    def __call__(self, *args, **kw):
65        return Course(*args, **kw)
66
67    def getInterfaces(self):
68        return implementedBy(Course)
[7207]69
70@grok.subscribe(ICourse, grok.IObjectRemovedEvent)
71def handle_course_removed(course, event):
72    """If a course is deleted, we make sure that also referrers in a
[7333]73       certificatescontainer are removed.
[7207]74    """
75    code = course.code
76
77    # Find all certificatecourses that refer to given course...
78    try:
79        cat = getUtility(ICatalog, name='certcourses_catalog')
80    except ComponentLookupError:
81        # catalog not available. This might happen during tests.
82        return
83
84    results = cat.searchResults(course_code=(code, code))
85    for certcourse in results:
86        # Remove that referrer...
87        cert = certcourse.__parent__
[8920]88        cert.delCertCourse(code)
[7207]89        cert._p_changed = True
[7811]90    return
[9220]91
[9227]92@grok.subscribe(ICourse, grok.IObjectModifiedEvent)
93def handle_set_former_course(course, event):
94    """If a former course attribute is set, we make sure that referrers in a
95       certificatescontainer are removed.
96    """
97    if event.object.former_course:
98        handle_course_removed(course, event)
99    return
100
[9220]101class CoursesPlugin(grok.GlobalUtility):
102    """A plugin that updates courses.
103    """
104
105    grok.implements(IKofaPluggable)
106    grok.name('courses')
107
108    deprecated_attributes = []
109
110    def setup(self, site, name, logger):
111        return
112
113    def update(self, site, name, logger):
114        cat = getUtility(ICatalog, name='courses_catalog')
115        results = cat.apply({'code':(None,None)})
116        uidutil = getUtility(IIntIds, context=cat)
117        items = getFields(ICourse).items()
118        for r in results:
119            o = uidutil.getObject(r)
120            # Add new attributes
121            for i in items:
122                if not hasattr(o,i[0]):
123                    setattr(o,i[0],i[1].missing_value)
124                    logger.info(
125                        'CoursesPlugin: %s attribute %s added.' % (
126                        o.code,i[0]))
127            # Remove deprecated attributes
128            for i in self.deprecated_attributes:
129                try:
130                    delattr(o,i)
131                    logger.info(
132                        'CoursesPlugin: %s attribute %s deleted.' % (
133                        o.code,i))
134                except AttributeError:
135                    pass
136        return
Note: See TracBrowser for help on using the repository browser.