## $Id: app.py 7193 2011-11-25 07:21:29Z henrik $ ## ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## import grok from zope.authentication.interfaces import IAuthentication from zope.component import getUtilitiesFor from zope.component.interfaces import ObjectEvent from zope.pluggableauth import PluggableAuthentication from waeup.sirp.authentication import setup_authentication from waeup.sirp.datacenter import DataCenter from waeup.sirp.students.container import StudentsContainer from waeup.sirp.hostels.container import HostelsContainer from waeup.sirp.interfaces import ( IUniversity, IWAeUPSIRPPluggable, IObjectUpgradeEvent, ) from waeup.sirp.userscontainer import UsersContainer from waeup.sirp.utils.logger import Logger from waeup.sirp.utils.helpers import attrs_to_fields from waeup.sirp.configuration import ConfigurationContainer class University(grok.Application, grok.Container, Logger): """A university. """ grok.implements(IUniversity) # Setup authentication for this app. Note: this is only # initialized, when a new instance of this app is created. grok.local_utility( PluggableAuthentication, provides = IAuthentication, setup = setup_authentication,) def __init__(self, *args, **kw): super(University, self).__init__(*args, **kw) self.setup() return def setup(self): """Setup some hard-wired components. Create local datacenter, containers for users, students and the like. """ self['users'] = UsersContainer() self['datacenter'] = DataCenter() self['students'] = StudentsContainer() self['configuration'] = ConfigurationContainer() self['hostels'] = HostelsContainer() self._createPlugins() def _createPlugins(self): """Create instances of all plugins defined somewhere. """ for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): plugin.setup(self, name, self.logger) return def updatePlugins(self): """Lookup all plugins and call their `update()` method. """ name = getattr(self, '__name__', '') self.logger.info('Fire upgrade event for site %s' % name) grok.notify(ObjectUpgradeEvent(self)) self.logger.info('Done.') self.logger.info('Now upgrading any plugins.') for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): plugin.update(self, name, self.logger) self.logger.info('Plugin update finished.') return attrs_to_fields(University) class ObjectUpgradeEvent(ObjectEvent): """An event fired, when datacenter storage moves. """ grok.implements(IObjectUpgradeEvent) @grok.subscribe(University, grok.IObjectAddedEvent) def handle_university_added(app, event): app.logger.info('University `%s` added.' % getattr(app, '__name__', None)) return