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

Last change on this file since 6222 was 6221, checked in by uli, 14 years ago
  • Reorder imports.
  • Don't use strings to signal status but raise exceptions instead.
  • Add a subscriber for certificate addition. This one might be placed better in certificates module in the long run.

This changes break a lot of tests but might take us further towards a
clean implementation.

File size: 2.5 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.university.interfaces import (
9    ICertificateContainer, ICertificate)
10
11class CertificateContainer(grok.Container):
12    """See interfaces for description.
13    """
14    grok.implements(ICertificateContainer)
15    grok.require('waeup.manageUniversity')
16
17    def addCertificate(self, certificate):
18        """XXX: docstring missing.
19        """
20        if not ICertificate.providedBy(certificate):
21            raise TypeError('CertificateContainers contain only '
22                            'ICertificate instances')
23        # Lookup catalog. If we find none: no duplicates possible.
24        cat = queryUtility(ICatalog, name='certificates_catalog', default=None)
25        if cat is not None:
26            entries = cat.searchResults(
27                code=(certificate.code,certificate.code))
28            if len(entries) > 0:
29                raise ValueError('Certificate exists already elsewhere')
30        else:
31            # No catalog, then this addition won't do harm to anything.
32            pass
33        self[certificate.code] = certificate
34
35    def clear(self):
36        keys = self.keys()
37        for key in keys:
38            del self[key]
39
40class CertificateContainerFactory(grok.GlobalUtility):
41    """A factory for certificate containers.
42    """
43    grok.implements(IFactory)
44    grok.name(u'waeup.CertificateContainer')
45    title = u"Create a new certificate container.",
46    description = u"This factory instantiates new certificate containers."
47
48    def __call__(self, *args, **kw):
49        return CertificateContainer(*args, **kw)
50
51    def getInterfaces(self):
52        return implementedBy(CertificateContainer)
53
54@grok.subscribe(ICertificate, grok.IObjectAddedEvent)
55def handle_cert_addition(certificate, event):
56    cat = queryUtility(ICatalog, name='certificates_catalog', default=None)
57    if cat is None:
58        return
59    entries = cat.searchResults(
60        code=(certificate.code,certificate.code))
61    if len(entries) == 0:
62        return
63    for entry in entries:
64        if entry is not certificate:
65            # XXX: Evil! We should find a way to block addition _before_
66            #      it actually happens.
67            del certificate.__parent__[certificate.__name__]
68            raise ValueError('Certificate exists already elsewhere: % s' %
69                             (certificate.code, ))
70
Note: See TracBrowser for help on using the repository browser.