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
RevLine 
[4289]1"""Containers for certificates.
2"""
3import grok
[5005]4from waeup.sirp.university.interfaces import (
5    ICertificateContainer, ICertificate)
[6216]6from zope.component.interfaces import IFactory, ComponentLookupError
[6210]7from zope.catalog.interfaces import ICatalog
8from zope.component import getUtility
[4365]9from zope.interface import implementedBy
[4289]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')
[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
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
[4366]48    def __call__(self, *args, **kw):
49        return CertificateContainer(*args, **kw)
[4289]50
51    def getInterfaces(self):
52        return implementedBy(CertificateContainer)
Note: See TracBrowser for help on using the repository browser.