"""Course containers. """ import grok from zope.interface import implementedBy from zope.component.interfaces import IFactory, ComponentLookupError from zope.catalog.interfaces import ICatalog from zope.component import getUtility from waeup.sirp.university.interfaces import ICourseContainer, ICourse class CourseContainer(grok.Container): """See interfaces for description. """ grok.implements(ICourseContainer) grok.require('waeup.manageUniversity') def addCourse(self, course): """Add a course. """ if not ICourse.providedBy(course): raise TypeError('CourseContainers contain only ICourse instances') try: cat = getUtility(ICatalog, name='courses_catalog') except ComponentLookupError: # catalog not available. This might happen during tests. self[course.code] = course return 'Course added (for tests only).' results = list(cat.searchResults(code=(course.code,course.code))) if results: dep = results[0].__parent__.__parent__.longtitle() fac = results[0].__parent__.__parent__.__parent__.longtitle() return 'Course exists already in %s / %s.' % (fac,dep) self[course.code] = course return 'Course added.' def clear(self): """Remove all objects from course container. """ keys = self.keys() for key in keys: del self[key] class CourseContainerFactory(grok.GlobalUtility): """A factory for course containers. """ grok.implements(IFactory) grok.name(u'waeup.CourseContainer') title = u"Create a new course container.", description = u"This factory instantiates new course containers." def __call__(self, *args, **kw): return CourseContainer(*args, **kw) def getInterfaces(self): return implementedBy(CourseContainer)