1 | ## $Id: course.py 9170 2012-09-11 09:13:41Z 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 | """Courses. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from zope.catalog.interfaces import ICatalog |
---|
22 | from zope.interface import implementedBy |
---|
23 | from zope.schema import getFields |
---|
24 | from zope.intid.interfaces import IIntIds |
---|
25 | from zope.component import getUtility |
---|
26 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
27 | from waeup.kofa.interfaces import IKofaPluggable |
---|
28 | from waeup.kofa.university.interfaces import ICourse, ICourseAdd |
---|
29 | |
---|
30 | class 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 | |
---|
56 | class 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) |
---|
71 | def 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 | for certcourse in results: |
---|
86 | # Remove that referrer... |
---|
87 | cert = certcourse.__parent__ |
---|
88 | cert.delCertCourse(code) |
---|
89 | cert._p_changed = True |
---|
90 | return |
---|
91 | |
---|
92 | class CoursesPlugin(grok.GlobalUtility): |
---|
93 | """A plugin that updates courses. |
---|
94 | """ |
---|
95 | |
---|
96 | grok.implements(IKofaPluggable) |
---|
97 | grok.name('courses') |
---|
98 | |
---|
99 | deprecated_attributes = [] |
---|
100 | |
---|
101 | def setup(self, site, name, logger): |
---|
102 | return |
---|
103 | |
---|
104 | def update(self, site, name, logger): |
---|
105 | cat = getUtility(ICatalog, name='courses_catalog') |
---|
106 | results = cat.apply({'code':(None,None)}) |
---|
107 | uidutil = getUtility(IIntIds, context=cat) |
---|
108 | items = getFields(ICourse).items() |
---|
109 | for r in results: |
---|
110 | o = uidutil.getObject(r) |
---|
111 | # Add new attributes |
---|
112 | for i in items: |
---|
113 | if not hasattr(o,i[0]): |
---|
114 | setattr(o,i[0],i[1].missing_value) |
---|
115 | logger.info( |
---|
116 | 'CoursesPlugin: %s attribute %s added.' % ( |
---|
117 | o.code,i[0])) |
---|
118 | # Remove deprecated attributes |
---|
119 | for i in self.deprecated_attributes: |
---|
120 | try: |
---|
121 | delattr(o,i) |
---|
122 | logger.info( |
---|
123 | 'CoursesPlugin: %s attribute %s deleted.' % ( |
---|
124 | o.code,i)) |
---|
125 | except AttributeError: |
---|
126 | pass |
---|
127 | return |
---|