[4289] | 1 | """Containers for certificates. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[5005] | 4 | from waeup.sirp.university.interfaces import ( |
---|
| 5 | ICertificateContainer, ICertificate) |
---|
[6216] | 6 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
[6210] | 7 | from zope.catalog.interfaces import ICatalog |
---|
| 8 | from zope.component import getUtility |
---|
[4365] | 9 | from zope.interface import implementedBy |
---|
[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): |
---|
| 18 | if not ICertificate.providedBy(certificate): |
---|
| 19 | raise TypeError('CertificateContainers contain only ' |
---|
| 20 | 'ICertificate instances') |
---|
[6216] | 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).' |
---|
[6210] | 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() |
---|
[6213] | 31 | return 'Certificate exists already in %s / %s.' % (fac,dep) |
---|
[4289] | 32 | self[certificate.code] = certificate |
---|
[6210] | 33 | return 'Certificate added.' |
---|
[4289] | 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) |
---|