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

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

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

File size: 1.9 KB
RevLine 
[4255]1"""Course containers.
2"""
3import grok
4from zope.interface import implementedBy
[6216]5from zope.component.interfaces import IFactory, ComponentLookupError
6from zope.catalog.interfaces import ICatalog
7from zope.component import getUtility
[5005]8from waeup.sirp.university.interfaces import ICourseContainer, ICourse
[4255]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):
[4738]17        """Add a course.
18        """
[4255]19        if not ICourse.providedBy(course):
20            raise TypeError('CourseContainers contain only ICourse instances')
[6216]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)
[4255]32        self[course.code] = course
[6216]33        return 'Course added.'
[4255]34
35    def clear(self):
[4738]36        """Remove all objects from course container.
37        """
[4255]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
[4363]50    def __call__(self, *args, **kw):
51        return CourseContainer(*args, **kw)
[4255]52
53    def getInterfaces(self):
54        return implementedBy(CourseContainer)
Note: See TracBrowser for help on using the repository browser.