| 1 | ## $Id: app.py 11956 2014-11-14 07:26:14Z 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 | ## |
|---|
| 18 | import grok |
|---|
| 19 | from zope.authentication.interfaces import IAuthentication |
|---|
| 20 | from zope.component import getUtility, getUtilitiesFor |
|---|
| 21 | from zope.component.interfaces import ObjectEvent |
|---|
| 22 | from zope.pluggableauth import PluggableAuthentication |
|---|
| 23 | from waeup.ikoba.authentication import setup_authentication |
|---|
| 24 | from waeup.ikoba.datacenter import DataCenter |
|---|
| 25 | from waeup.ikoba.mandates.container import MandatesContainer |
|---|
| 26 | from waeup.ikoba.interfaces import ( |
|---|
| 27 | ICompany, IIkobaPluggable, IObjectUpgradeEvent, IJobManager, |
|---|
| 28 | VIRT_JOBS_CONTAINER_NAME) |
|---|
| 29 | from waeup.ikoba.userscontainer import UsersContainer |
|---|
| 30 | from waeup.ikoba.utils.logger import Logger |
|---|
| 31 | from waeup.ikoba.utils.helpers import attrs_to_fields |
|---|
| 32 | from waeup.ikoba.configuration import ConfigurationContainer |
|---|
| 33 | |
|---|
| 34 | from waeup.ikoba.customers.container import CustomersContainer |
|---|
| 35 | |
|---|
| 36 | class Company(grok.Application, grok.Container, Logger): |
|---|
| 37 | """A company. |
|---|
| 38 | """ |
|---|
| 39 | grok.implements(ICompany) |
|---|
| 40 | |
|---|
| 41 | # Setup authentication for this app. Note: this is only |
|---|
| 42 | # initialized, when a new instance of this app is created. |
|---|
| 43 | grok.local_utility( |
|---|
| 44 | PluggableAuthentication, provides = IAuthentication, |
|---|
| 45 | setup = setup_authentication,) |
|---|
| 46 | |
|---|
| 47 | def __init__(self, *args, **kw): |
|---|
| 48 | super(Company, self).__init__(*args, **kw) |
|---|
| 49 | self.setup() |
|---|
| 50 | return |
|---|
| 51 | |
|---|
| 52 | def setup(self): |
|---|
| 53 | """Setup some hard-wired components. |
|---|
| 54 | |
|---|
| 55 | Create local datacenter, containers for users and |
|---|
| 56 | the like. |
|---|
| 57 | """ |
|---|
| 58 | |
|---|
| 59 | self['users'] = UsersContainer() |
|---|
| 60 | self['datacenter'] = DataCenter() |
|---|
| 61 | self['configuration'] = ConfigurationContainer() |
|---|
| 62 | self['mandates'] = MandatesContainer() |
|---|
| 63 | self['customers'] = CustomersContainer() |
|---|
| 64 | self._createPlugins() |
|---|
| 65 | |
|---|
| 66 | def _createPlugins(self): |
|---|
| 67 | """Create instances of all plugins defined somewhere. |
|---|
| 68 | """ |
|---|
| 69 | for name, plugin in getUtilitiesFor(IIkobaPluggable): |
|---|
| 70 | plugin.setup(self, name, self.logger) |
|---|
| 71 | return |
|---|
| 72 | |
|---|
| 73 | def traverse(self, name): |
|---|
| 74 | if name == VIRT_JOBS_CONTAINER_NAME: |
|---|
| 75 | return getUtility(IJobManager) |
|---|
| 76 | return None |
|---|
| 77 | |
|---|
| 78 | def updatePlugins(self): |
|---|
| 79 | """Lookup all plugins and call their `update()` method. |
|---|
| 80 | """ |
|---|
| 81 | name = getattr(self, '__name__', '<Unnamed>') |
|---|
| 82 | self.logger.info('Fire upgrade event for site %s' % name) |
|---|
| 83 | grok.notify(ObjectUpgradeEvent(self)) |
|---|
| 84 | self.logger.info('Done.') |
|---|
| 85 | self.logger.info('Now upgrading any plugins.') |
|---|
| 86 | for name, plugin in getUtilitiesFor(IIkobaPluggable): |
|---|
| 87 | plugin.update(self, name, self.logger) |
|---|
| 88 | self.logger.info('Plugin update finished.') |
|---|
| 89 | return |
|---|
| 90 | attrs_to_fields(Company) |
|---|
| 91 | |
|---|
| 92 | class ObjectUpgradeEvent(ObjectEvent): |
|---|
| 93 | """An event fired, when datacenter storage moves. |
|---|
| 94 | """ |
|---|
| 95 | grok.implements(IObjectUpgradeEvent) |
|---|
| 96 | |
|---|
| 97 | @grok.subscribe(Company, grok.IObjectAddedEvent) |
|---|
| 98 | def handle_company_added(app, event): |
|---|
| 99 | """If a company is added, a message is logged. |
|---|
| 100 | """ |
|---|
| 101 | app.logger.info('Company `%s` added.' % getattr(app, '__name__', None)) |
|---|
| 102 | return |
|---|