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