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

Last change on this file since 12256 was 12256, checked in by Henrik Bettermann, 11 years ago

Change document_id generation algorithm. Use Universally Unique IDentifiers instead of consecutive numbers.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1## $Id: app.py 12256 2014-12-18 12:58:12Z 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
36from waeup.ikoba.documents.container import DocumentsContainer
37
38
39class Company(grok.Application, grok.Container, Logger):
40    """A company.
41    """
42    grok.implements(ICompany)
43
44    _curr_con_id = 101
45
46    # Setup authentication for this app. Note: this is only
47    # initialized, when a new instance of this app is created.
48    grok.local_utility(
49        PluggableAuthentication, provides = IAuthentication,
50        setup = setup_authentication,)
51
52    def __init__(self, *args, **kw):
53        super(Company, self).__init__(*args, **kw)
54        self.setup()
55        return
56
57    @property
58    def unique_contract_id(self):
59        """A unique contract id for all contract objects in customers.
60
61        The contract id returned is guaranteed to be unique.
62
63        Once a contract id was issued, it won't be issued again.
64
65        Obtaining an contract id is currently not thread-safe but can be
66        made easily by enabling commented lines.
67        """
68        # lock.acquire() # lock data
69        new_id = u'c%s' % (self._curr_con_id)
70        self._curr_con_id += 1
71        # self._p_changed = True
72        # commit()
73        # lock.release() # end of lock
74        return new_id
75
76    def setup(self):
77        """Setup some hard-wired components.
78
79        Create local datacenter, containers for users and
80        the like.
81        """
82
83        self['users'] = UsersContainer()
84        self['datacenter'] = DataCenter()
85        self['configuration'] = ConfigurationContainer()
86        self['mandates'] = MandatesContainer()
87        self['customers'] = CustomersContainer()
88        self['products'] = ProductsContainer()
89        self['documents'] = DocumentsContainer()
90        self._createPlugins()
91
92    def _createPlugins(self):
93        """Create instances of all plugins defined somewhere.
94        """
95        for name, plugin in getUtilitiesFor(IIkobaPluggable):
96            plugin.setup(self, name, self.logger)
97        return
98
99    def traverse(self, name):
100        if name == VIRT_JOBS_CONTAINER_NAME:
101            return getUtility(IJobManager)
102        return None
103
104    def updatePlugins(self):
105        """Lookup all plugins and call their `update()` method.
106        """
107        name = getattr(self, '__name__', '<Unnamed>')
108        self.logger.info('Fire upgrade event for site %s' % name)
109        grok.notify(ObjectUpgradeEvent(self))
110        self.logger.info('Done.')
111        self.logger.info('Now upgrading any plugins.')
112        for name, plugin in getUtilitiesFor(IIkobaPluggable):
113            plugin.update(self, name, self.logger)
114        self.logger.info('Plugin update finished.')
115        return
116
117attrs_to_fields(Company)
118
119
120class ObjectUpgradeEvent(ObjectEvent):
121    """An event fired, when datacenter storage moves.
122    """
123    grok.implements(IObjectUpgradeEvent)
124
125@grok.subscribe(Company, grok.IObjectAddedEvent)
126def handle_company_added(app, event):
127    """If a company is added, a message is logged.
128    """
129    app.logger.info('Company `%s` added.' % getattr(app, '__name__', None))
130    return
Note: See TracBrowser for help on using the repository browser.