Changeset 6245


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

Use and raise DuplicationError? for Courses too.

Location:
main/waeup.sirp/trunk/src/waeup/sirp
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/browser.txt

    r6229 r6245  
    337337  >>> print browser.contents
    338338  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
    339   ...<li class="message">Course exists already in...</li>
     339  ...<li class="message">A course with same code already exists:...</li>
    340340  ...
    341341
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/pages.py

    r6243 r6245  
    12921292        course = createObject(u'waeup.Course')
    12931293        self.applyData(course, **data)
    1294         message = self.context.courses.addCourse(course)
     1294        try:
     1295            self.context.courses.addCourse(course)
     1296        except DuplicationError, error:
     1297            # signals duplication error
     1298            entries = error.entries
     1299            for entry in entries:
     1300                # do not use error.msg but render a more detailed message instead
     1301                # and show links to all certs with same code
     1302                message = 'A course with same code already exists: '
     1303                message += '<a href="%s">%s</a>' % (
     1304                    self.url(entry), getattr(entry, '__name__', u'Unnamed'))
     1305                self.flash(message)
     1306            self.redirect(self.url(self.context, u'@@addcourse'))
     1307            return
     1308        message = u'Course %s successfully created.' % (course.code)
    12951309        self.flash(message)
    12961310        self.redirect(self.url(self.context, u'@@manage')+'#tab-2')
  • 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):
  • main/waeup.sirp/trunk/src/waeup/sirp/university/coursecontainer.txt

    r6217 r6245  
    7171        >>> mycourse = Course(code='COURSE1')
    7272        >>> mycontainer.addCourse(mycourse)
    73         'Course added (for tests only).'
    7473        >>> list(mycontainer.keys())
    7574        [u'COURSE1']
Note: See TracChangeset for help on using the changeset viewer.