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