source: main/waeup.sirp/trunk/src/waeup/sirp/university/certificatescontainer.py @ 7871

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

Rename certificatecontainer, coursecontainer and facultycontainer. Now we have e.g. a container for faculties. Synonyms are 'facultiescontainer', 'faculties', 'academics' or 'Academic Section'. The faculty container is the faculty itself which contains the departments.

  • Property svn:keywords set to Id
File size: 5.3 KB
RevLine 
[7195]1## $Id: certificatescontainer.py 7333 2011-12-12 07:01:54Z 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##
[4289]18"""Containers for certificates.
19"""
20import grok
[6221]21from zope.catalog.interfaces import ICatalog
22from zope.component.interfaces import IFactory
23from zope.component import queryUtility
24from zope.interface import implementedBy
[6227]25from waeup.sirp.interfaces import DuplicationError
[5005]26from waeup.sirp.university.interfaces import (
[7333]27    ICertificatesContainer, ICertificate)
[4289]28
[7333]29class CertificatesContainer(grok.Container):
[6240]30    """A storage for certificates.
31
[7333]32    A :class:`CertificatesContainer` stores
[6240]33    :class:`waeup.sirp.university.Certificate` instances.
34
35    It is a :class:`grok.Container` basically acting like a standard
36    Python dictionary. That means you can add certificates like items
37    in a normal dictionary and also get certificates by using
38    :meth:`values`, :meth:`keys`, and :meth:`items`.
39
40    This container type is picky about its contents: only real
41    certificates can be stored here and each new certificate must
42    provide a unique `code`. See :meth:`addCertificate` for details.
43
[7333]44    Each :class:`CertificatesContainer` provides
45    :class:`ICertificatesContainer`.
[4289]46    """
[7333]47    grok.implements(ICertificatesContainer)
[4289]48    grok.require('waeup.manageUniversity')
49
[6223]50    def __setitem__(self, name, certificate):
[6240]51        """Insert `certificate` with `name` as key into container.
52
[6242]53        The `certificate` must be an object implementing
[7063]54        :class:`waeup.sirp.university.interfaces.ICertificate`. If
[6240]55        not, a :exc:`TypeError` is raised.
56
57        If the certificate `code` does not equal `name` a
58        :exc:`ValueError` is raised.
59
60        If the `code` attribute of `certificate` is already in use by
61        another certificate stored in the local site
62        (:class:`waeup.sirp.app.University` instance), then a
63        :exc:`waeup.sirp.interfaces.DuplicationError` will be raised.
64
65        If `name` is already used as a key, a :exc:`KeyError` will be
66        raised.
[6221]67        """
[4289]68        if not ICertificate.providedBy(certificate):
[7333]69            raise TypeError('CertificatesContainers contain only '
[4289]70                            'ICertificate instances')
[6240]71
72        # Only accept certs with code == key.
73        if certificate.code != name:
74            raise ValueError('key must match certificate code: '
75                             '%s, %s' % (name, certificate.code))
76
[6221]77        # Lookup catalog. If we find none: no duplicates possible.
78        cat = queryUtility(ICatalog, name='certificates_catalog', default=None)
79        if cat is not None:
80            entries = cat.searchResults(
81                code=(certificate.code,certificate.code))
82            if len(entries) > 0:
[6227]83                raise DuplicationError(
[6243]84                    'Certificate exists already elsewhere.', entries)
[6221]85        else:
86            # No catalog, then this addition won't do harm to anything.
87            pass
[7333]88        super(CertificatesContainer, self).__setitem__(name, certificate)
[4289]89
[6223]90    def addCertificate(self, certificate):
[6240]91        """Add `certificate` to the container.
92
93        The certificate must be an object implementing
[7063]94        :class:`waeup.sirp.university.interfaces.ICertificate`. If
[6240]95        not, a :exc:`TypeError` is raised.
96
97        The certificate will be stored in the container with its
98        `code` attribute as key. If this key is already used for
99        another certificate stored in the local site
100        (:class:`waeup.sirp.app.University` instance), then a
101        :exc:`waeup.sirp.interfaces.DuplicationError` will be raised.
102        """
[6223]103        self[getattr(certificate, 'code', None)] = certificate
104
[4289]105    def clear(self):
[6233]106        """Remove all contents from the certificate container.
[6242]107
108        This methods is pretty fast and optimized. Use it instead of
109        removing all items manually yourself.
[6233]110        """
111        # This internal function is implemented in C and thus much
112        # faster as we could do it in pure Python.
113        self._SampleContainer__data.clear()
114        # The length attribute is 'lazy'. See `zope.container` for details.
115        # This way we make sure, the next time len() is called, it returns
116        # the real value and not a cached one.
117        del self.__dict__['_BTreeContainer__len']
[4289]118
[7333]119class CertificatesContainerFactory(grok.GlobalUtility):
[4289]120    """A factory for certificate containers.
121    """
122    grok.implements(IFactory)
[7333]123    grok.name(u'waeup.CertificatesContainer')
124    title = u"Create a new container for certificates.",
125    description = u"This factory instantiates new containers for certificates."
[4289]126
[4366]127    def __call__(self, *args, **kw):
[7333]128        return CertificatesContainer(*args, **kw)
[4289]129
130    def getInterfaces(self):
[7333]131        return implementedBy(CertificatesContainer)
Note: See TracBrowser for help on using the repository browser.