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

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

Implement document_id generator. Ensure that document_ids remain unique during import.

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