- Timestamp:
- 30 May 2011, 07:41:18 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/university/coursecontainer.py
r6216 r6245 5 5 from zope.component.interfaces import IFactory, ComponentLookupError 6 6 from zope.catalog.interfaces import ICatalog 7 from zope.component import getUtility 7 from zope.component import queryUtility 8 from waeup.sirp.interfaces import DuplicationError 8 9 from waeup.sirp.university.interfaces import ICourseContainer, ICourse 9 10 … … 14 15 grok.require('waeup.manageUniversity') 15 16 16 def addCourse(self, course):17 """ Add a course.17 def __setitem__(self, name, course): 18 """See corresponding docstring in certificatecontainer.py. 18 19 """ 19 20 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.' 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 34 46 35 47 def clear(self): 36 """ Remove all objects from course container.48 """See corresponding docstring and comments in certificatecontainer.py. 37 49 """ 38 keys = self.keys() 39 for key in keys: 40 del self[key] 50 self._SampleContainer__data.clear() 51 del self.__dict__['_BTreeContainer__len'] 41 52 42 53 class CourseContainerFactory(grok.GlobalUtility):
Note: See TracChangeset for help on using the changeset viewer.