source: main/waeup.sirp/trunk/src/waeup/sirp/app.py @ 6579

Last change on this file since 6579 was 6578, checked in by uli, 13 years ago

Implement a new logging technique and use it.
Details are explained in waeup.sirp.utils.logger.

  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
1import grok
2from hurry.file.interfaces import IFileRetrieval
3from zope.authentication.interfaces import IAuthentication
4from zope.component import createObject, getUtilitiesFor
5from zope.component.interfaces import ObjectEvent
6from zope.pluggableauth import PluggableAuthentication
7from waeup.sirp.authentication import setup_authentication
8from waeup.sirp.datacenter import DataCenter
9from waeup.sirp.imagestorage import ImageStorageFileRetrieval, ImageStorage
10from waeup.sirp.interfaces import (
11    IUniversity, IWAeUPSIRPPluggable, IObjectUpgradeEvent, )
12from waeup.sirp.users import UserContainer
13from waeup.sirp.utils.logger import Logger
14from waeup.sirp.utils.helpers import attrs_to_fields
15
16class University(grok.Application, grok.Container, Logger):
17    """A university.
18    """
19    grok.implements(IUniversity)
20
21    # Setup authentication for this app. Note: this is only
22    # initialized, when a new instance of this app is created.
23    grok.local_utility(
24        PluggableAuthentication, provides = IAuthentication,
25        setup = setup_authentication,)
26
27    grok.local_utility(
28        ImageStorageFileRetrieval, provides = IFileRetrieval)
29
30    def __init__(self, name=None, skin=None, title=None,
31                 frontpage=None, **kw):
32        super(University, self).__init__(**kw)
33        if name is not None:
34            self.name = name
35        if skin is not None:
36            self.skin = skin
37        if title is not None:
38            self.title = title
39        if frontpage is not None:
40            self.frontpage = frontpage
41        self.setup()
42
43    def setup(self):
44        """Setup some hard-wired components.
45
46        Create local datacenter, containers for users, students and
47        the like.
48        """
49        self['users'] = UserContainer()
50        self['datacenter'] = DataCenter()
51        self['images'] = ImageStorage()
52
53        self['students'] = createObject(u'waeup.StudentContainer')
54        self['hostels'] = createObject(u'waeup.HostelContainer')
55        self._createPlugins()
56
57    def _createPlugins(self):
58        """Create instances of all plugins defined somewhere.
59        """
60        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
61            plugin.setup(self, name, self.logger)
62        return
63
64    def updatePlugins(self):
65        """Lookup all plugins and call their `update()` method.
66        """
67        name = getattr(self, '__name__', '<Unnamed>')
68        self.logger.info('Fire upgrade event for site %s' % name)
69        grok.notify(ObjectUpgradeEvent(self))
70        self.logger.info('Done.')
71        self.logger.info('Now upgrading any plugins.')
72        for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable):
73            plugin.update(self, name, self.logger)
74        self.logger.info('Plugin update finished.')
75        return
76attrs_to_fields(University)
77
78class ObjectUpgradeEvent(ObjectEvent):
79    """An event fired, when datacenter storage moves.
80    """
81    grok.implements(IObjectUpgradeEvent)
82
83@grok.subscribe(University, grok.IObjectAddedEvent)
84def handle_university_add(app, event):
85    app.logger.info('University `%s` added.' % getattr(app, '__name__', None))
86    return
Note: See TracBrowser for help on using the repository browser.