Changeset 6221 for main/waeup.sirp/trunk/src
- Timestamp:
- 29 May 2011, 13:34:06 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/university/certificatecontainer.py
r6216 r6221 2 2 """ 3 3 import grok 4 from zope.catalog.interfaces import ICatalog 5 from zope.component.interfaces import IFactory 6 from zope.component import queryUtility 7 from zope.interface import implementedBy 4 8 from waeup.sirp.university.interfaces import ( 5 9 ICertificateContainer, ICertificate) 6 from zope.component.interfaces import IFactory, ComponentLookupError7 from zope.catalog.interfaces import ICatalog8 from zope.component import getUtility9 from zope.interface import implementedBy10 10 11 11 class CertificateContainer(grok.Container): … … 16 16 17 17 def addCertificate(self, certificate): 18 """XXX: docstring missing. 19 """ 18 20 if not ICertificate.providedBy(certificate): 19 21 raise TypeError('CertificateContainers contain only ' 20 22 '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 32 33 self[certificate.code] = certificate 33 return 'Certificate added.'34 34 35 35 def clear(self): … … 51 51 def getInterfaces(self): 52 52 return implementedBy(CertificateContainer) 53 54 @grok.subscribe(ICertificate, grok.IObjectAddedEvent) 55 def 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.