source: main/waeup.sirp/trunk/src/waeup/sirp/university/coursecontainer.py @ 6219

Last change on this file since 6219 was 6216, checked in by Henrik Bettermann, 14 years ago

Course codes must be unique. Check catalog when courses are added. Flash the place where course exists.

File size: 1.9 KB
Line 
1"""Course containers.
2"""
3import grok
4from zope.interface import implementedBy
5from zope.component.interfaces import IFactory, ComponentLookupError
6from zope.catalog.interfaces import ICatalog
7from zope.component import getUtility
8from waeup.sirp.university.interfaces import ICourseContainer, ICourse
9
10class 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
42class 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)
Note: See TracBrowser for help on using the repository browser.