[7193] | 1 | ## $Id: app.py 12005 2014-11-20 05:40:52Z 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 |
---|
[9217] | 20 | from zope.component import getUtility, getUtilitiesFor |
---|
[6137] | 21 | from zope.component.interfaces import ObjectEvent |
---|
[6362] | 22 | from zope.pluggableauth import PluggableAuthentication |
---|
[11949] | 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 ( |
---|
[11954] | 27 | ICompany, IIkobaPluggable, IObjectUpgradeEvent, IJobManager, |
---|
[9217] | 28 | VIRT_JOBS_CONTAINER_NAME) |
---|
[11949] | 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 |
---|
[4789] | 33 | |
---|
[11956] | 34 | from waeup.ikoba.customers.container import CustomersContainer |
---|
| 35 | |
---|
[11954] | 36 | class Company(grok.Application, grok.Container, Logger): |
---|
| 37 | """A company. |
---|
[4789] | 38 | """ |
---|
[11954] | 39 | grok.implements(ICompany) |
---|
[6129] | 40 | |
---|
[12005] | 41 | _curr_doc_id = 101 |
---|
| 42 | |
---|
[4789] | 43 | # Setup authentication for this app. Note: this is only |
---|
| 44 | # initialized, when a new instance of this app is created. |
---|
| 45 | grok.local_utility( |
---|
| 46 | PluggableAuthentication, provides = IAuthentication, |
---|
[5054] | 47 | setup = setup_authentication,) |
---|
[5345] | 48 | |
---|
[6592] | 49 | def __init__(self, *args, **kw): |
---|
[11954] | 50 | super(Company, self).__init__(*args, **kw) |
---|
[4789] | 51 | self.setup() |
---|
[6592] | 52 | return |
---|
[3521] | 53 | |
---|
[12005] | 54 | @property |
---|
| 55 | def unique_document_id(self): |
---|
| 56 | """A unique document id for all documents in company. |
---|
| 57 | |
---|
| 58 | The document id returned is guaranteed to be unique. It |
---|
| 59 | consists of some prefix (normally a single letter) followed by |
---|
| 60 | a number with at least 7 digits. |
---|
| 61 | |
---|
| 62 | Once a document id was issued, it won't be issued again. |
---|
| 63 | |
---|
| 64 | Obtaining a document id is currently not thread-safe but can be |
---|
| 65 | made easily by enabling commented lines. |
---|
| 66 | """ |
---|
| 67 | # lock.acquire() # lock data |
---|
| 68 | new_id = u'd%s' % (self._curr_doc_id) |
---|
| 69 | self._curr_doc_id += 1 |
---|
| 70 | # self._p_changed = True |
---|
| 71 | # commit() |
---|
| 72 | # lock.release() # end of lock |
---|
| 73 | return new_id |
---|
| 74 | |
---|
[4789] | 75 | def setup(self): |
---|
[5345] | 76 | """Setup some hard-wired components. |
---|
| 77 | |
---|
[11947] | 78 | Create local datacenter, containers for users and |
---|
[5345] | 79 | the like. |
---|
| 80 | """ |
---|
[9217] | 81 | |
---|
[7172] | 82 | self['users'] = UsersContainer() |
---|
[4789] | 83 | self['datacenter'] = DataCenter() |
---|
[6907] | 84 | self['configuration'] = ConfigurationContainer() |
---|
[8846] | 85 | self['mandates'] = MandatesContainer() |
---|
[11956] | 86 | self['customers'] = CustomersContainer() |
---|
[5016] | 87 | self._createPlugins() |
---|
| 88 | |
---|
| 89 | def _createPlugins(self): |
---|
| 90 | """Create instances of all plugins defined somewhere. |
---|
| 91 | """ |
---|
[11949] | 92 | for name, plugin in getUtilitiesFor(IIkobaPluggable): |
---|
[5071] | 93 | plugin.setup(self, name, self.logger) |
---|
| 94 | return |
---|
[5421] | 95 | |
---|
[9217] | 96 | def traverse(self, name): |
---|
| 97 | if name == VIRT_JOBS_CONTAINER_NAME: |
---|
| 98 | return getUtility(IJobManager) |
---|
| 99 | return None |
---|
| 100 | |
---|
[5421] | 101 | def updatePlugins(self): |
---|
| 102 | """Lookup all plugins and call their `update()` method. |
---|
| 103 | """ |
---|
[6138] | 104 | name = getattr(self, '__name__', '<Unnamed>') |
---|
| 105 | self.logger.info('Fire upgrade event for site %s' % name) |
---|
| 106 | grok.notify(ObjectUpgradeEvent(self)) |
---|
| 107 | self.logger.info('Done.') |
---|
| 108 | self.logger.info('Now upgrading any plugins.') |
---|
[11949] | 109 | for name, plugin in getUtilitiesFor(IIkobaPluggable): |
---|
[5421] | 110 | plugin.update(self, name, self.logger) |
---|
[6138] | 111 | self.logger.info('Plugin update finished.') |
---|
[5421] | 112 | return |
---|
[11954] | 113 | attrs_to_fields(Company) |
---|
[4884] | 114 | |
---|
[6137] | 115 | class ObjectUpgradeEvent(ObjectEvent): |
---|
| 116 | """An event fired, when datacenter storage moves. |
---|
| 117 | """ |
---|
| 118 | grok.implements(IObjectUpgradeEvent) |
---|
[6578] | 119 | |
---|
[11954] | 120 | @grok.subscribe(Company, grok.IObjectAddedEvent) |
---|
| 121 | def handle_company_added(app, event): |
---|
| 122 | """If a company is added, a message is logged. |
---|
[11477] | 123 | """ |
---|
[11954] | 124 | app.logger.info('Company `%s` added.' % getattr(app, '__name__', None)) |
---|
[6578] | 125 | return |
---|