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

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

Add logger and add hostels container to site root.

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