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