[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 | |
---|
[6223] | 17 | def __setitem__(self, name, 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 |
---|
[6223] | 33 | super(CertificateContainer, self).__setitem__(name, certificate) |
---|
[4289] | 34 | |
---|
[6223] | 35 | def addCertificate(self, certificate): |
---|
| 36 | self[getattr(certificate, 'code', None)] = certificate |
---|
| 37 | |
---|
[4289] | 38 | def clear(self): |
---|
| 39 | keys = self.keys() |
---|
| 40 | for key in keys: |
---|
| 41 | del self[key] |
---|
| 42 | |
---|
| 43 | class CertificateContainerFactory(grok.GlobalUtility): |
---|
| 44 | """A factory for certificate containers. |
---|
| 45 | """ |
---|
| 46 | grok.implements(IFactory) |
---|
| 47 | grok.name(u'waeup.CertificateContainer') |
---|
| 48 | title = u"Create a new certificate container.", |
---|
| 49 | description = u"This factory instantiates new certificate containers." |
---|
| 50 | |
---|
[4366] | 51 | def __call__(self, *args, **kw): |
---|
| 52 | return CertificateContainer(*args, **kw) |
---|
[4289] | 53 | |
---|
| 54 | def getInterfaces(self): |
---|
| 55 | return implementedBy(CertificateContainer) |
---|
[6221] | 56 | |
---|
| 57 | @grok.subscribe(ICertificate, grok.IObjectAddedEvent) |
---|
| 58 | def handle_cert_addition(certificate, event): |
---|
| 59 | cat = queryUtility(ICatalog, name='certificates_catalog', default=None) |
---|
| 60 | if cat is None: |
---|
| 61 | return |
---|
| 62 | entries = cat.searchResults( |
---|
| 63 | code=(certificate.code,certificate.code)) |
---|
| 64 | if len(entries) == 0: |
---|
| 65 | return |
---|
| 66 | for entry in entries: |
---|
| 67 | if entry is not certificate: |
---|
| 68 | # XXX: Evil! We should find a way to block addition _before_ |
---|
| 69 | # it actually happens. |
---|
| 70 | del certificate.__parent__[certificate.__name__] |
---|
| 71 | raise ValueError('Certificate exists already elsewhere: % s' % |
---|
| 72 | (certificate.code, )) |
---|
| 73 | |
---|