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

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

Use and raise DuplicationError? where appropriate.

File size: 2.8 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        keys = self.keys()
42        for key in keys:
43            del self[key]
44
45class CertificateContainerFactory(grok.GlobalUtility):
46    """A factory for certificate containers.
47    """
48    grok.implements(IFactory)
49    grok.name(u'waeup.CertificateContainer')
50    title = u"Create a new certificate container.",
51    description = u"This factory instantiates new certificate containers."
52
53    def __call__(self, *args, **kw):
54        return CertificateContainer(*args, **kw)
55
56    def getInterfaces(self):
57        return implementedBy(CertificateContainer)
58
59@grok.subscribe(ICertificate, grok.IObjectAddedEvent)
60def handle_cert_addition(certificate, event):
61    """XXX: Move me to certificates.py when everything works as expected.
62    """
63    cat = queryUtility(ICatalog, name='certificates_catalog', default=None)
64    if cat is None:
65        return
66    entries = cat.searchResults(
67        code=(certificate.code,certificate.code))
68    if len(entries) == 0:
69        return
70    for entry in entries:
71        if entry is not certificate:
72            # XXX: Evil! We should find a way to block addition _before_
73            #      it actually happens.
74            del certificate.__parent__[certificate.__name__]
75            raise ValueError('Certificate exists already elsewhere: % s' %
76                             (certificate.code, ))
77
Note: See TracBrowser for help on using the repository browser.