[7193] | 1 | ## $Id: app.py 7193 2011-11-25 07:21:29Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[3521] | 18 | import grok |
---|
[6362] | 19 | from zope.authentication.interfaces import IAuthentication |
---|
[6633] | 20 | from zope.component import getUtilitiesFor |
---|
[6137] | 21 | from zope.component.interfaces import ObjectEvent |
---|
[6362] | 22 | from zope.pluggableauth import PluggableAuthentication |
---|
[5054] | 23 | from waeup.sirp.authentication import setup_authentication |
---|
| 24 | from waeup.sirp.datacenter import DataCenter |
---|
[6633] | 25 | from waeup.sirp.students.container import StudentsContainer |
---|
[6952] | 26 | from waeup.sirp.hostels.container import HostelsContainer |
---|
[5016] | 27 | from waeup.sirp.interfaces import ( |
---|
[6578] | 28 | IUniversity, IWAeUPSIRPPluggable, IObjectUpgradeEvent, ) |
---|
[7172] | 29 | from waeup.sirp.userscontainer import UsersContainer |
---|
[6578] | 30 | from waeup.sirp.utils.logger import Logger |
---|
[6361] | 31 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
[6907] | 32 | from waeup.sirp.configuration import ConfigurationContainer |
---|
[4789] | 33 | |
---|
[6578] | 34 | class University(grok.Application, grok.Container, Logger): |
---|
[4789] | 35 | """A university. |
---|
| 36 | """ |
---|
[4968] | 37 | grok.implements(IUniversity) |
---|
[6129] | 38 | |
---|
[4789] | 39 | # Setup authentication for this app. Note: this is only |
---|
| 40 | # initialized, when a new instance of this app is created. |
---|
| 41 | grok.local_utility( |
---|
| 42 | PluggableAuthentication, provides = IAuthentication, |
---|
[5054] | 43 | setup = setup_authentication,) |
---|
[5345] | 44 | |
---|
[6592] | 45 | def __init__(self, *args, **kw): |
---|
| 46 | super(University, self).__init__(*args, **kw) |
---|
[4789] | 47 | self.setup() |
---|
[6592] | 48 | return |
---|
[3521] | 49 | |
---|
[4789] | 50 | def setup(self): |
---|
[5345] | 51 | """Setup some hard-wired components. |
---|
| 52 | |
---|
| 53 | Create local datacenter, containers for users, students and |
---|
| 54 | the like. |
---|
| 55 | """ |
---|
[7172] | 56 | self['users'] = UsersContainer() |
---|
[4789] | 57 | self['datacenter'] = DataCenter() |
---|
[6633] | 58 | self['students'] = StudentsContainer() |
---|
[6907] | 59 | self['configuration'] = ConfigurationContainer() |
---|
[6952] | 60 | self['hostels'] = HostelsContainer() |
---|
[5016] | 61 | self._createPlugins() |
---|
| 62 | |
---|
| 63 | def _createPlugins(self): |
---|
| 64 | """Create instances of all plugins defined somewhere. |
---|
| 65 | """ |
---|
[5071] | 66 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 67 | plugin.setup(self, name, self.logger) |
---|
| 68 | return |
---|
[5421] | 69 | |
---|
| 70 | def updatePlugins(self): |
---|
| 71 | """Lookup all plugins and call their `update()` method. |
---|
| 72 | """ |
---|
[6138] | 73 | name = getattr(self, '__name__', '<Unnamed>') |
---|
| 74 | self.logger.info('Fire upgrade event for site %s' % name) |
---|
| 75 | grok.notify(ObjectUpgradeEvent(self)) |
---|
| 76 | self.logger.info('Done.') |
---|
| 77 | self.logger.info('Now upgrading any plugins.') |
---|
[5421] | 78 | for name, plugin in getUtilitiesFor(IWAeUPSIRPPluggable): |
---|
| 79 | plugin.update(self, name, self.logger) |
---|
[6138] | 80 | self.logger.info('Plugin update finished.') |
---|
[5421] | 81 | return |
---|
[6361] | 82 | attrs_to_fields(University) |
---|
[4884] | 83 | |
---|
[6137] | 84 | class ObjectUpgradeEvent(ObjectEvent): |
---|
| 85 | """An event fired, when datacenter storage moves. |
---|
| 86 | """ |
---|
| 87 | grok.implements(IObjectUpgradeEvent) |
---|
[6578] | 88 | |
---|
| 89 | @grok.subscribe(University, grok.IObjectAddedEvent) |
---|
[6839] | 90 | def handle_university_added(app, event): |
---|
[6578] | 91 | app.logger.info('University `%s` added.' % getattr(app, '__name__', None)) |
---|
| 92 | return |
---|