source: main/waeup.sirp/trunk/src/waeup/sirp/university/certificatecontainer.py @ 6216

Last change on this file since 6216 was 6216, checked in by Henrik Bettermann, 13 years ago

Course codes must be unique. Check catalog when courses are added. Flash the place where course exists.

File size: 1.9 KB
Line 
1"""Containers for certificates.
2"""
3import grok
4from waeup.sirp.university.interfaces import (
5    ICertificateContainer, ICertificate)
6from zope.component.interfaces import IFactory, ComponentLookupError
7from zope.catalog.interfaces import ICatalog
8from zope.component import getUtility
9from zope.interface import implementedBy
10
11class 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
40class 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)
Note: See TracBrowser for help on using the repository browser.