1 | """Containers for certificates. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from waeup.sirp.university.interfaces import ( |
---|
5 | ICertificateContainer, ICertificate) |
---|
6 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
7 | from zope.catalog.interfaces import ICatalog |
---|
8 | from zope.component import getUtility |
---|
9 | from zope.interface import implementedBy |
---|
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') |
---|
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).' |
---|
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() |
---|
31 | return 'Certificate exists already in %s / %s.' % (fac,dep) |
---|
32 | self[certificate.code] = certificate |
---|
33 | return 'Certificate added.' |
---|
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 | |
---|
48 | def __call__(self, *args, **kw): |
---|
49 | return CertificateContainer(*args, **kw) |
---|
50 | |
---|
51 | def getInterfaces(self): |
---|
52 | return implementedBy(CertificateContainer) |
---|