[4289] | 1 | """Containers for certificates. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[6221] | 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 |
---|
[5005] | 8 | from waeup.sirp.university.interfaces import ( |
---|
| 9 | ICertificateContainer, ICertificate) |
---|
[4289] | 10 | |
---|
| 11 | class CertificateContainer(grok.Container): |
---|
| 12 | """See interfaces for description. |
---|
| 13 | """ |
---|
| 14 | grok.implements(ICertificateContainer) |
---|
| 15 | grok.require('waeup.manageUniversity') |
---|
| 16 | |
---|
| 17 | def addCertificate(self, certificate): |
---|
[6221] | 18 | """XXX: docstring missing. |
---|
| 19 | """ |
---|
[4289] | 20 | if not ICertificate.providedBy(certificate): |
---|
| 21 | raise TypeError('CertificateContainers contain only ' |
---|
| 22 | 'ICertificate instances') |
---|
[6221] | 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 |
---|
[4289] | 33 | self[certificate.code] = certificate |
---|
| 34 | |
---|
| 35 | def clear(self): |
---|
| 36 | keys = self.keys() |
---|
| 37 | for key in keys: |
---|
| 38 | del self[key] |
---|
| 39 | |
---|
| 40 | class 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 | |
---|
[4366] | 48 | def __call__(self, *args, **kw): |
---|
| 49 | return CertificateContainer(*args, **kw) |
---|
[4289] | 50 | |
---|
| 51 | def getInterfaces(self): |
---|
| 52 | return implementedBy(CertificateContainer) |
---|
[6221] | 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 | |
---|