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