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