[7195] | 1 | ## $Id: course.py 9002 2012-07-13 16:36:21Z 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 | """ |
---|
| 20 | import grok |
---|
[7207] | 21 | from zope.catalog.interfaces import ICatalog |
---|
[4789] | 22 | from zope.interface import implementedBy |
---|
[7207] | 23 | from zope.component import getUtility |
---|
| 24 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
[7811] | 25 | from waeup.kofa.university.interfaces import ICourse, ICourseAdd |
---|
[4789] | 26 | |
---|
| 27 | class Course(grok.Model): |
---|
| 28 | """A university course. |
---|
| 29 | """ |
---|
[5953] | 30 | grok.implements(ICourse, ICourseAdd) |
---|
[4789] | 31 | |
---|
[9002] | 32 | local_roles = ['waeup.local.Lecturer'] |
---|
[8996] | 33 | |
---|
[4789] | 34 | def __init__(self, |
---|
| 35 | title=u'Unnamed Course', |
---|
| 36 | code=u'NA', |
---|
| 37 | credits=0, |
---|
| 38 | passmark=40, |
---|
| 39 | semester=1, **kw): |
---|
| 40 | super(Course, self).__init__(**kw) |
---|
| 41 | self.title = title |
---|
| 42 | self.code = code |
---|
| 43 | self.credits = credits |
---|
| 44 | self.passmark = passmark |
---|
| 45 | self.semester = semester |
---|
[6566] | 46 | |
---|
[6008] | 47 | def longtitle(self): |
---|
[6566] | 48 | return "%s (%s)" % (self.title,self.code) |
---|
| 49 | |
---|
[4789] | 50 | class CourseFactory(grok.GlobalUtility): |
---|
| 51 | """A factory for courses. |
---|
| 52 | """ |
---|
| 53 | grok.implements(IFactory) |
---|
| 54 | grok.name(u'waeup.Course') |
---|
| 55 | title = u"Create a new course.", |
---|
| 56 | description = u"This factory instantiates new course instances." |
---|
| 57 | |
---|
| 58 | def __call__(self, *args, **kw): |
---|
| 59 | return Course(*args, **kw) |
---|
| 60 | |
---|
| 61 | def getInterfaces(self): |
---|
| 62 | return implementedBy(Course) |
---|
[7207] | 63 | |
---|
| 64 | @grok.subscribe(ICourse, grok.IObjectRemovedEvent) |
---|
| 65 | def handle_course_removed(course, event): |
---|
| 66 | """If a course is deleted, we make sure that also referrers in a |
---|
[7333] | 67 | certificatescontainer are removed. |
---|
[7207] | 68 | """ |
---|
| 69 | code = course.code |
---|
| 70 | |
---|
| 71 | # Find all certificatecourses that refer to given course... |
---|
| 72 | try: |
---|
| 73 | cat = getUtility(ICatalog, name='certcourses_catalog') |
---|
| 74 | except ComponentLookupError: |
---|
| 75 | # catalog not available. This might happen during tests. |
---|
| 76 | return |
---|
| 77 | |
---|
| 78 | results = cat.searchResults(course_code=(code, code)) |
---|
| 79 | for certcourse in results: |
---|
| 80 | # Remove that referrer... |
---|
| 81 | cert = certcourse.__parent__ |
---|
[8920] | 82 | cert.delCertCourse(code) |
---|
[7207] | 83 | cert._p_changed = True |
---|
[7811] | 84 | return |
---|