1 | """Course containers. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from zope.interface import implementedBy |
---|
5 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
6 | from zope.catalog.interfaces import ICatalog |
---|
7 | from zope.component import queryUtility |
---|
8 | from waeup.sirp.interfaces import DuplicationError |
---|
9 | from waeup.sirp.university.interfaces import ICourseContainer, ICourse |
---|
10 | |
---|
11 | class CourseContainer(grok.Container): |
---|
12 | """See interfaces for description. |
---|
13 | """ |
---|
14 | grok.implements(ICourseContainer) |
---|
15 | grok.require('waeup.manageUniversity') |
---|
16 | |
---|
17 | def __setitem__(self, name, course): |
---|
18 | """See corresponding docstring in certificatecontainer.py. |
---|
19 | """ |
---|
20 | if not ICourse.providedBy(course): |
---|
21 | raise TypeError('CourseContainers contain only ' |
---|
22 | 'ICourse instances') |
---|
23 | |
---|
24 | # Only accept courses with code == key. |
---|
25 | if course.code != name: |
---|
26 | raise ValueError('key must match course code: ' |
---|
27 | '%s, %s' % (name, course.code)) |
---|
28 | |
---|
29 | # Lookup catalog. If we find none: no duplicates possible. |
---|
30 | cat = queryUtility(ICatalog, name='courses_catalog', default=None) |
---|
31 | if cat is not None: |
---|
32 | entries = cat.searchResults( |
---|
33 | code=(course.code,course.code)) |
---|
34 | if len(entries) > 0: |
---|
35 | raise DuplicationError( |
---|
36 | 'Course exists already elsewhere.', entries) |
---|
37 | else: |
---|
38 | # No catalog, then this addition won't do harm to anything. |
---|
39 | pass |
---|
40 | super(CourseContainer, self).__setitem__(name, course) |
---|
41 | |
---|
42 | def addCourse(self, course): |
---|
43 | """See corresponding docstring in certificatecontainer.py. |
---|
44 | """ |
---|
45 | self[getattr(course, 'code', None)] = course |
---|
46 | |
---|
47 | def clear(self): |
---|
48 | """See corresponding docstring and comments in certificatecontainer.py. |
---|
49 | """ |
---|
50 | self._SampleContainer__data.clear() |
---|
51 | del self.__dict__['_BTreeContainer__len'] |
---|
52 | |
---|
53 | class CourseContainerFactory(grok.GlobalUtility): |
---|
54 | """A factory for course containers. |
---|
55 | """ |
---|
56 | grok.implements(IFactory) |
---|
57 | grok.name(u'waeup.CourseContainer') |
---|
58 | title = u"Create a new course container.", |
---|
59 | description = u"This factory instantiates new course containers." |
---|
60 | |
---|
61 | def __call__(self, *args, **kw): |
---|
62 | return CourseContainer(*args, **kw) |
---|
63 | |
---|
64 | def getInterfaces(self): |
---|
65 | return implementedBy(CourseContainer) |
---|