Changeset 6221 for main


Ignore:
Timestamp:
29 May 2011, 13:34:06 (14 years ago)
Author:
uli
Message:
  • 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:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/university/certificatecontainer.py

    r6216 r6221  
    22"""
    33import grok
     4from zope.catalog.interfaces import ICatalog
     5from zope.component.interfaces import IFactory
     6from zope.component import queryUtility
     7from zope.interface import implementedBy
    48from waeup.sirp.university.interfaces import (
    59    ICertificateContainer, ICertificate)
    6 from zope.component.interfaces import IFactory, ComponentLookupError
    7 from zope.catalog.interfaces import ICatalog
    8 from zope.component import getUtility
    9 from zope.interface import implementedBy
    1010
    1111class CertificateContainer(grok.Container):
     
    1616
    1717    def addCertificate(self, certificate):
     18        """XXX: docstring missing.
     19        """
    1820        if not ICertificate.providedBy(certificate):
    1921            raise TypeError('CertificateContainers contain only '
    2022                            'ICertificate instances')
    21         try:
    22             cat = getUtility(ICatalog, name='certificates_catalog')
    23         except ComponentLookupError:
    24             # catalog not available. This might happen during tests.
    25             self[certificate.code] = certificate
    26             return 'Certificate added (for tests only).'
    27         results = list(cat.searchResults(code=(certificate.code,certificate.code)))
    28         if results:
    29             dep = results[0].__parent__.__parent__.longtitle()
    30             fac = results[0].__parent__.__parent__.__parent__.longtitle()
    31             return 'Certificate exists already in %s / %s.' % (fac,dep)
     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
    3233        self[certificate.code] = certificate
    33         return 'Certificate added.'
    3434
    3535    def clear(self):
     
    5151    def getInterfaces(self):
    5252        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 TracChangeset for help on using the changeset viewer.