source: main/waeup.ikoba/trunk/src/waeup/ikoba/app.py @ 12141

Last change on this file since 12141 was 12098, checked in by Henrik Bettermann, 10 years ago

Renaming batch 2

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