[7195] | 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 | ## |
---|
[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 | grok.require('waeup.manageUniversity') |
---|
| 33 | |
---|
[6245] | 34 | def __setitem__(self, name, course): |
---|
[7333] | 35 | """See corresponding docstring in certificatescontainer.py. |
---|
[4738] | 36 | """ |
---|
[4255] | 37 | if not ICourse.providedBy(course): |
---|
[7333] | 38 | raise TypeError('CoursesContainers contain only ' |
---|
[6245] | 39 | 'ICourse instances') |
---|
[4255] | 40 | |
---|
[6245] | 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 |
---|
[7333] | 57 | super(CoursesContainer, self).__setitem__(name, course) |
---|
[6245] | 58 | |
---|
| 59 | def addCourse(self, course): |
---|
[7333] | 60 | """See corresponding docstring in certificatescontainer.py. |
---|
[6245] | 61 | """ |
---|
| 62 | self[getattr(course, 'code', None)] = course |
---|
| 63 | |
---|
[4255] | 64 | def clear(self): |
---|
[7333] | 65 | """See corresponding docstring and comments in certificatescontainer.py. |
---|
[4738] | 66 | """ |
---|
[6245] | 67 | self._SampleContainer__data.clear() |
---|
| 68 | del self.__dict__['_BTreeContainer__len'] |
---|
[4255] | 69 | |
---|
[7333] | 70 | class CoursesContainerFactory(grok.GlobalUtility): |
---|
[4255] | 71 | """A factory for course containers. |
---|
| 72 | """ |
---|
| 73 | grok.implements(IFactory) |
---|
[7333] | 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." |
---|
[4255] | 77 | |
---|
[4363] | 78 | def __call__(self, *args, **kw): |
---|
[7333] | 79 | return CoursesContainer(*args, **kw) |
---|
[4255] | 80 | |
---|
| 81 | def getInterfaces(self): |
---|
[7333] | 82 | return implementedBy(CoursesContainer) |
---|