Ignore:
Timestamp:
30 May 2011, 07:41:18 (13 years ago)
Author:
Henrik Bettermann
Message:

Use and raise DuplicationError? for Courses too.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/university/coursecontainer.py

    r6216 r6245  
    55from zope.component.interfaces import IFactory, ComponentLookupError
    66from zope.catalog.interfaces import ICatalog
    7 from zope.component import getUtility
     7from zope.component import queryUtility
     8from waeup.sirp.interfaces import DuplicationError
    89from waeup.sirp.university.interfaces import ICourseContainer, ICourse
    910
     
    1415    grok.require('waeup.manageUniversity')
    1516
    16     def addCourse(self, course):
    17         """Add a course.
     17    def __setitem__(self, name, course):
     18        """See corresponding docstring in certificatecontainer.py.
    1819        """
    1920        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
    3446
    3547    def clear(self):
    36         """Remove all objects from course container.
     48        """See corresponding docstring and comments in certificatecontainer.py.
    3749        """
    38         keys = self.keys()
    39         for key in keys:
    40             del self[key]
     50        self._SampleContainer__data.clear()
     51        del self.__dict__['_BTreeContainer__len']
    4152
    4253class CourseContainerFactory(grok.GlobalUtility):
Note: See TracChangeset for help on using the changeset viewer.