[4289] | 1 | """Containers for certificates. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[5005] | 4 | from waeup.sirp.university.interfaces import ( |
---|
| 5 | ICertificateContainer, ICertificate) |
---|
[4289] | 6 | from zope.component.interfaces import IFactory |
---|
[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') |
---|
[6210] | 21 | cat = getUtility(ICatalog, name='certificates_catalog') |
---|
| 22 | results = list(cat.searchResults(code=(certificate.code,certificate.code))) |
---|
| 23 | if results: |
---|
| 24 | dep = results[0].__parent__.__parent__.longtitle() |
---|
| 25 | fac = results[0].__parent__.__parent__.__parent__.longtitle() |
---|
| 26 | return 'Certificate exists in %s / %s.' % (fac,dep) |
---|
[4289] | 27 | self[certificate.code] = certificate |
---|
[6210] | 28 | return 'Certificate added.' |
---|
[4289] | 29 | |
---|
| 30 | def clear(self): |
---|
| 31 | keys = self.keys() |
---|
| 32 | for key in keys: |
---|
| 33 | del self[key] |
---|
| 34 | |
---|
| 35 | class CertificateContainerFactory(grok.GlobalUtility): |
---|
| 36 | """A factory for certificate containers. |
---|
| 37 | """ |
---|
| 38 | grok.implements(IFactory) |
---|
| 39 | grok.name(u'waeup.CertificateContainer') |
---|
| 40 | title = u"Create a new certificate container.", |
---|
| 41 | description = u"This factory instantiates new certificate containers." |
---|
| 42 | |
---|
[4366] | 43 | def __call__(self, *args, **kw): |
---|
| 44 | return CertificateContainer(*args, **kw) |
---|
[4289] | 45 | |
---|
| 46 | def getInterfaces(self): |
---|
| 47 | return implementedBy(CertificateContainer) |
---|