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

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

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1## $Id: coursecontainer.py 7195 2011-11-25 07:34:07Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Course containers.
19"""
20import grok
21from zope.interface import implementedBy
22from zope.component.interfaces import IFactory, ComponentLookupError
23from zope.catalog.interfaces import ICatalog
24from zope.component import queryUtility
25from waeup.sirp.interfaces import DuplicationError
26from waeup.sirp.university.interfaces import ICourseContainer, ICourse
27
28class CourseContainer(grok.Container):
29    """See interfaces for description.
30    """
31    grok.implements(ICourseContainer)
32    grok.require('waeup.manageUniversity')
33
34    def __setitem__(self, name, course):
35        """See corresponding docstring in certificatecontainer.py.
36        """
37        if not ICourse.providedBy(course):
38            raise TypeError('CourseContainers contain only '
39                            'ICourse instances')
40
41        # Only accept courses with code == key.
42        if course.code != name:
43            raise ValueError('key must match course code: '
44                             '%s, %s' % (name, course.code))
45
46        # Lookup catalog. If we find none: no duplicates possible.
47        cat = queryUtility(ICatalog, name='courses_catalog', default=None)
48        if cat is not None:
49            entries = cat.searchResults(
50                code=(course.code,course.code))
51            if len(entries) > 0:
52                raise DuplicationError(
53                    'Course exists already elsewhere.', entries)
54        else:
55            # No catalog, then this addition won't do harm to anything.
56            pass
57        super(CourseContainer, self).__setitem__(name, course)
58
59    def addCourse(self, course):
60        """See corresponding docstring in certificatecontainer.py.
61        """
62        self[getattr(course, 'code', None)] = course
63
64    def clear(self):
65        """See corresponding docstring and comments in certificatecontainer.py.
66        """
67        self._SampleContainer__data.clear()
68        del self.__dict__['_BTreeContainer__len']
69
70class CourseContainerFactory(grok.GlobalUtility):
71    """A factory for course containers.
72    """
73    grok.implements(IFactory)
74    grok.name(u'waeup.CourseContainer')
75    title = u"Create a new course container.",
76    description = u"This factory instantiates new course containers."
77
78    def __call__(self, *args, **kw):
79        return CourseContainer(*args, **kw)
80
81    def getInterfaces(self):
82        return implementedBy(CourseContainer)
Note: See TracBrowser for help on using the repository browser.