[7195] | 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 | ## |
---|
[4255] | 18 | """Course containers. |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
| 21 | from zope.interface import implementedBy |
---|
[6216] | 22 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
| 23 | from zope.catalog.interfaces import ICatalog |
---|
[6245] | 24 | from zope.component import queryUtility |
---|
[7811] | 25 | from waeup.kofa.interfaces import DuplicationError |
---|
| 26 | from waeup.kofa.university.interfaces import ICoursesContainer, ICourse |
---|
[4255] | 27 | |
---|
[7333] | 28 | class CoursesContainer(grok.Container): |
---|
[4255] | 29 | """See interfaces for description. |
---|
| 30 | """ |
---|
[7333] | 31 | grok.implements(ICoursesContainer) |
---|
[4255] | 32 | |
---|
[6245] | 33 | def __setitem__(self, name, course): |
---|
[7333] | 34 | """See corresponding docstring in certificatescontainer.py. |
---|
[4738] | 35 | """ |
---|
[4255] | 36 | if not ICourse.providedBy(course): |
---|
[7333] | 37 | raise TypeError('CoursesContainers contain only ' |
---|
[6245] | 38 | 'ICourse instances') |
---|
[4255] | 39 | |
---|
[6245] | 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 |
---|
[7333] | 56 | super(CoursesContainer, self).__setitem__(name, course) |
---|
[6245] | 57 | |
---|
| 58 | def addCourse(self, course): |
---|
[7333] | 59 | """See corresponding docstring in certificatescontainer.py. |
---|
[6245] | 60 | """ |
---|
| 61 | self[getattr(course, 'code', None)] = course |
---|
| 62 | |
---|
[4255] | 63 | def clear(self): |
---|
[7333] | 64 | """See corresponding docstring and comments in certificatescontainer.py. |
---|
[4738] | 65 | """ |
---|
[6245] | 66 | self._SampleContainer__data.clear() |
---|
| 67 | del self.__dict__['_BTreeContainer__len'] |
---|
[4255] | 68 | |
---|
[7333] | 69 | class CoursesContainerFactory(grok.GlobalUtility): |
---|
[4255] | 70 | """A factory for course containers. |
---|
| 71 | """ |
---|
| 72 | grok.implements(IFactory) |
---|
[7333] | 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." |
---|
[4255] | 76 | |
---|
[4363] | 77 | def __call__(self, *args, **kw): |
---|
[7333] | 78 | return CoursesContainer(*args, **kw) |
---|
[4255] | 79 | |
---|
| 80 | def getInterfaces(self): |
---|
[7333] | 81 | return implementedBy(CoursesContainer) |
---|