source: main/waeup.sirp/trunk/src/waeup/sirp/university/certificatecontainer.py @ 6236

Last change on this file since 6236 was 6236, checked in by uli, 13 years ago

Remove unwanted shit.

File size: 2.4 KB
Line 
1"""Containers for certificates.
2"""
3import grok
4from zope.catalog.interfaces import ICatalog
5from zope.component.interfaces import IFactory
6from zope.component import queryUtility
7from zope.interface import implementedBy
8from waeup.sirp.interfaces import DuplicationError
9from waeup.sirp.university.interfaces import (
10    ICertificateContainer, ICertificate)
11
12class CertificateContainer(grok.Container):
13    """See interfaces for description.
14    """
15    grok.implements(ICertificateContainer)
16    grok.require('waeup.manageUniversity')
17
18    def __setitem__(self, name, certificate):
19        """XXX: docstring missing.
20        """
21        if not ICertificate.providedBy(certificate):
22            raise TypeError('CertificateContainers contain only '
23                            'ICertificate instances')
24        # Lookup catalog. If we find none: no duplicates possible.
25        cat = queryUtility(ICatalog, name='certificates_catalog', default=None)
26        if cat is not None:
27            entries = cat.searchResults(
28                code=(certificate.code,certificate.code))
29            if len(entries) > 0:
30                raise DuplicationError(
31                    'Certificate exists already elsewhere', entries)
32        else:
33            # No catalog, then this addition won't do harm to anything.
34            pass
35        super(CertificateContainer, self).__setitem__(name, certificate)
36
37    def addCertificate(self, certificate):
38        self[getattr(certificate, 'code', None)] = certificate
39
40    def clear(self):
41        """Remove all contents from the certificate container.
42        """
43        # This internal function is implemented in C and thus much
44        # faster as we could do it in pure Python.
45        self._SampleContainer__data.clear()
46        # The length attribute is 'lazy'. See `zope.container` for details.
47        # This way we make sure, the next time len() is called, it returns
48        # the real value and not a cached one.
49        del self.__dict__['_BTreeContainer__len']
50
51class CertificateContainerFactory(grok.GlobalUtility):
52    """A factory for certificate containers.
53    """
54    grok.implements(IFactory)
55    grok.name(u'waeup.CertificateContainer')
56    title = u"Create a new certificate container.",
57    description = u"This factory instantiates new certificate containers."
58
59    def __call__(self, *args, **kw):
60        return CertificateContainer(*args, **kw)
61
62    def getInterfaces(self):
63        return implementedBy(CertificateContainer)
Note: See TracBrowser for help on using the repository browser.