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 getUtility |
---|
8 | from waeup.sirp.university.interfaces import ICourseContainer, ICourse |
---|
9 | |
---|
10 | class CourseContainer(grok.Container): |
---|
11 | """See interfaces for description. |
---|
12 | """ |
---|
13 | grok.implements(ICourseContainer) |
---|
14 | grok.require('waeup.manageUniversity') |
---|
15 | |
---|
16 | def addCourse(self, course): |
---|
17 | """Add a course. |
---|
18 | """ |
---|
19 | if not ICourse.providedBy(course): |
---|
20 | raise TypeError('CourseContainers contain only ICourse instances') |
---|
21 | try: |
---|
22 | cat = getUtility(ICatalog, name='courses_catalog') |
---|
23 | except ComponentLookupError: |
---|
24 | # catalog not available. This might happen during tests. |
---|
25 | self[course.code] = course |
---|
26 | return 'Course added (for tests only).' |
---|
27 | results = list(cat.searchResults(code=(course.code,course.code))) |
---|
28 | if results: |
---|
29 | dep = results[0].__parent__.__parent__.longtitle() |
---|
30 | fac = results[0].__parent__.__parent__.__parent__.longtitle() |
---|
31 | return 'Course exists already in %s / %s.' % (fac,dep) |
---|
32 | self[course.code] = course |
---|
33 | return 'Course added.' |
---|
34 | |
---|
35 | def clear(self): |
---|
36 | """Remove all objects from course container. |
---|
37 | """ |
---|
38 | keys = self.keys() |
---|
39 | for key in keys: |
---|
40 | del self[key] |
---|
41 | |
---|
42 | class CourseContainerFactory(grok.GlobalUtility): |
---|
43 | """A factory for course containers. |
---|
44 | """ |
---|
45 | grok.implements(IFactory) |
---|
46 | grok.name(u'waeup.CourseContainer') |
---|
47 | title = u"Create a new course container.", |
---|
48 | description = u"This factory instantiates new course containers." |
---|
49 | |
---|
50 | def __call__(self, *args, **kw): |
---|
51 | return CourseContainer(*args, **kw) |
---|
52 | |
---|
53 | def getInterfaces(self): |
---|
54 | return implementedBy(CourseContainer) |
---|