import grok from hurry.file.interfaces import IFileRetrieval from zope.authentication.interfaces import IAuthentication from zope.component import createObject, 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.imagestorage import ImageStorageFileRetrieval, ImageStorage from waeup.sirp.interfaces import ( IUniversity, IWAeUPSIRPPluggable, IObjectUpgradeEvent, ) from waeup.sirp.users import UserContainer from waeup.sirp.utils.logger import Logger from waeup.sirp.utils.helpers import attrs_to_fields 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,) grok.local_utility( ImageStorageFileRetrieval, provides = IFileRetrieval) def __init__(self, name=None, skin=None, title=None, frontpage=None, **kw): super(University, self).__init__(**kw) if name is not None: self.name = name if skin is not None: self.skin = skin if title is not None: self.title = title if frontpage is not None: self.frontpage = frontpage self.setup() def setup(self): """Setup some hard-wired components. Create local datacenter, containers for users, students and the like. """ self['users'] = UserContainer() self['datacenter'] = DataCenter() self['images'] = ImageStorage() self['students'] = createObject(u'waeup.StudentContainer') self['hostels'] = createObject(u'waeup.HostelContainer') 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_add(app, event): app.logger.info('University `%s` added.' % getattr(app, '__name__', None)) return