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

Last change on this file since 9834 was 9828, checked in by uli, 12 years ago

Simplify last fix a bit and add regression test. Welcome home, by the way.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1## $Id: course.py 9828 2013-01-05 17:26:01Z 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##
18"""Courses.
19"""
20import grok
21from zope.catalog.interfaces import ICatalog
22from zope.interface import implementedBy
23from zope.schema import getFields
24from zope.intid.interfaces import IIntIds
25from zope.component import getUtility
26from zope.component.interfaces import IFactory, ComponentLookupError
27from waeup.kofa.interfaces import IKofaPluggable
28from waeup.kofa.university.interfaces import ICourse, ICourseAdd
29
30class Course(grok.Model):
31    """A university course.
32    """
33    grok.implements(ICourse, ICourseAdd)
34
35    local_roles = ['waeup.local.Lecturer']
36
37    def __init__(self,
38                 title=u'Unnamed Course',
39                 code=u'NA',
40                 credits=0,
41                 passmark=40,
42                 semester=1,
43                 former_course=False,
44                 **kw):
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
51        self.former_course = former_course
52
53    def longtitle(self):
54        return "%s (%s)" % (self.title,self.code)
55
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)
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
73       certificatescontainer are removed.
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
86    # remove each found referrer (certs might refer to same course multiple
87    # times)
88    certs = [x.__parent__ for x in results]
89    unique_certs = list(set(certs))
90    for cert in unique_certs:
91        cert.delCertCourses(code)
92    return
93
94@grok.subscribe(ICourse, grok.IObjectModifiedEvent)
95def handle_set_former_course(course, event):
96    """If a former course attribute is set, we make sure that referrers in a
97       certificatescontainer are removed.
98    """
99    if event.object.former_course:
100        handle_course_removed(course, event)
101    return
102
103class CoursesPlugin(grok.GlobalUtility):
104    """A plugin that updates courses.
105    """
106
107    grok.implements(IKofaPluggable)
108    grok.name('courses')
109
110    deprecated_attributes = []
111
112    def setup(self, site, name, logger):
113        return
114
115    def update(self, site, name, logger):
116        cat = getUtility(ICatalog, name='courses_catalog')
117        results = cat.apply({'code':(None,None)})
118        uidutil = getUtility(IIntIds, context=cat)
119        items = getFields(ICourse).items()
120        for r in results:
121            o = uidutil.getObject(r)
122            # Add new attributes
123            for i in items:
124                if not hasattr(o,i[0]):
125                    setattr(o,i[0],i[1].missing_value)
126                    logger.info(
127                        'CoursesPlugin: %s attribute %s added.' % (
128                        o.code,i[0]))
129            # Remove deprecated attributes
130            for i in self.deprecated_attributes:
131                try:
132                    delattr(o,i)
133                    logger.info(
134                        'CoursesPlugin: %s attribute %s deleted.' % (
135                        o.code,i))
136                except AttributeError:
137                    pass
138        return
Note: See TracBrowser for help on using the repository browser.