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 | ## |
---|
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 | from waeup.ikoba.products.container import ProductsContainer |
---|
36 | from waeup.ikoba.documents.container import DocumentsContainer |
---|
37 | |
---|
38 | |
---|
39 | class Company(grok.Application, grok.Container, Logger): |
---|
40 | """A company. |
---|
41 | """ |
---|
42 | grok.implements(ICompany) |
---|
43 | |
---|
44 | _curr_doc_id = 101 |
---|
45 | |
---|
46 | _curr_con_id = 101 |
---|
47 | |
---|
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, |
---|
52 | setup = setup_authentication,) |
---|
53 | |
---|
54 | def __init__(self, *args, **kw): |
---|
55 | super(Company, self).__init__(*args, **kw) |
---|
56 | self.setup() |
---|
57 | return |
---|
58 | |
---|
59 | @property |
---|
60 | def unique_document_id(self): |
---|
61 | """A unique document id for all documents in company. |
---|
62 | |
---|
63 | The document id returned is guaranteed to be unique. |
---|
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 | |
---|
78 | @property |
---|
79 | def unique_contract_id(self): |
---|
80 | """A unique contract id for all contract objects in customers. |
---|
81 | |
---|
82 | The contract id returned is guaranteed to be unique. |
---|
83 | |
---|
84 | Once a contract id was issued, it won't be issued again. |
---|
85 | |
---|
86 | Obtaining an contract id is currently not thread-safe but can be |
---|
87 | made easily by enabling commented lines. |
---|
88 | """ |
---|
89 | # lock.acquire() # lock data |
---|
90 | new_id = u'c%s' % (self._curr_con_id) |
---|
91 | self._curr_con_id += 1 |
---|
92 | # self._p_changed = True |
---|
93 | # commit() |
---|
94 | # lock.release() # end of lock |
---|
95 | return new_id |
---|
96 | |
---|
97 | def setup(self): |
---|
98 | """Setup some hard-wired components. |
---|
99 | |
---|
100 | Create local datacenter, containers for users and |
---|
101 | the like. |
---|
102 | """ |
---|
103 | |
---|
104 | self['users'] = UsersContainer() |
---|
105 | self['datacenter'] = DataCenter() |
---|
106 | self['configuration'] = ConfigurationContainer() |
---|
107 | self['mandates'] = MandatesContainer() |
---|
108 | self['customers'] = CustomersContainer() |
---|
109 | self['products'] = ProductsContainer() |
---|
110 | self['documents'] = DocumentsContainer() |
---|
111 | self._createPlugins() |
---|
112 | |
---|
113 | def _createPlugins(self): |
---|
114 | """Create instances of all plugins defined somewhere. |
---|
115 | """ |
---|
116 | for name, plugin in getUtilitiesFor(IIkobaPluggable): |
---|
117 | plugin.setup(self, name, self.logger) |
---|
118 | return |
---|
119 | |
---|
120 | def traverse(self, name): |
---|
121 | if name == VIRT_JOBS_CONTAINER_NAME: |
---|
122 | return getUtility(IJobManager) |
---|
123 | return None |
---|
124 | |
---|
125 | def updatePlugins(self): |
---|
126 | """Lookup all plugins and call their `update()` method. |
---|
127 | """ |
---|
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.') |
---|
133 | for name, plugin in getUtilitiesFor(IIkobaPluggable): |
---|
134 | plugin.update(self, name, self.logger) |
---|
135 | self.logger.info('Plugin update finished.') |
---|
136 | return |
---|
137 | |
---|
138 | attrs_to_fields(Company) |
---|
139 | |
---|
140 | |
---|
141 | class ObjectUpgradeEvent(ObjectEvent): |
---|
142 | """An event fired, when datacenter storage moves. |
---|
143 | """ |
---|
144 | grok.implements(IObjectUpgradeEvent) |
---|
145 | |
---|
146 | @grok.subscribe(Company, grok.IObjectAddedEvent) |
---|
147 | def handle_company_added(app, event): |
---|
148 | """If a company is added, a message is logged. |
---|
149 | """ |
---|
150 | app.logger.info('Company `%s` added.' % getattr(app, '__name__', None)) |
---|
151 | return |
---|