1 | ## $Id: coursescontainer.py 7811 2012-03-08 19:00:51Z 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 | """Course containers. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from zope.interface import implementedBy |
---|
22 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
23 | from zope.catalog.interfaces import ICatalog |
---|
24 | from zope.component import queryUtility |
---|
25 | from waeup.kofa.interfaces import DuplicationError |
---|
26 | from waeup.kofa.university.interfaces import ICoursesContainer, ICourse |
---|
27 | |
---|
28 | class CoursesContainer(grok.Container): |
---|
29 | """See interfaces for description. |
---|
30 | """ |
---|
31 | grok.implements(ICoursesContainer) |
---|
32 | grok.require('waeup.manageUniversity') |
---|
33 | |
---|
34 | def __setitem__(self, name, course): |
---|
35 | """See corresponding docstring in certificatescontainer.py. |
---|
36 | """ |
---|
37 | if not ICourse.providedBy(course): |
---|
38 | raise TypeError('CoursesContainers contain only ' |
---|
39 | 'ICourse instances') |
---|
40 | |
---|
41 | # Only accept courses with code == key. |
---|
42 | if course.code != name: |
---|
43 | raise ValueError('key must match course code: ' |
---|
44 | '%s, %s' % (name, course.code)) |
---|
45 | |
---|
46 | # Lookup catalog. If we find none: no duplicates possible. |
---|
47 | cat = queryUtility(ICatalog, name='courses_catalog', default=None) |
---|
48 | if cat is not None: |
---|
49 | entries = cat.searchResults( |
---|
50 | code=(course.code,course.code)) |
---|
51 | if len(entries) > 0: |
---|
52 | raise DuplicationError( |
---|
53 | 'Course exists already elsewhere.', entries) |
---|
54 | else: |
---|
55 | # No catalog, then this addition won't do harm to anything. |
---|
56 | pass |
---|
57 | super(CoursesContainer, self).__setitem__(name, course) |
---|
58 | |
---|
59 | def addCourse(self, course): |
---|
60 | """See corresponding docstring in certificatescontainer.py. |
---|
61 | """ |
---|
62 | self[getattr(course, 'code', None)] = course |
---|
63 | |
---|
64 | def clear(self): |
---|
65 | """See corresponding docstring and comments in certificatescontainer.py. |
---|
66 | """ |
---|
67 | self._SampleContainer__data.clear() |
---|
68 | del self.__dict__['_BTreeContainer__len'] |
---|
69 | |
---|
70 | class CoursesContainerFactory(grok.GlobalUtility): |
---|
71 | """A factory for course containers. |
---|
72 | """ |
---|
73 | grok.implements(IFactory) |
---|
74 | grok.name(u'waeup.CoursesContainer') |
---|
75 | title = u"Create a new container for courses.", |
---|
76 | description = u"This factory instantiates new containers for courses." |
---|
77 | |
---|
78 | def __call__(self, *args, **kw): |
---|
79 | return CoursesContainer(*args, **kw) |
---|
80 | |
---|
81 | def getInterfaces(self): |
---|
82 | return implementedBy(CoursesContainer) |
---|