summaryrefslogtreecommitdiffstats
path: root/hyperkitty/lib/store.py
blob: 607475cc9bd77d172a21a76f72d9e769339233df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
WSGI and Django middlewares for KittyStore

Inspired by http://pypi.python.org/pypi/middlestorm
"""

from threading import local

from django.conf import settings
import kittystore


class KittyStoreWSGIMiddleware(object):
    """WSGI middleware.
    Add KittyStore object in environ['kittystore.store']. Each thread contains
    own store.
    """

    def __init__(self, app):
        """Create WSGI middleware.
        :param app: top level application or middleware.
        :param database: instance of Database returned create_database.
        """
        self._app = app
        self._local = local()

    def __call__(self, environ, start_response):
        try:
            environ['kittystore.store']  = self._local.store
        except AttributeError:
            environ['kittystore.store']  = \
                    self._local.__dict__.setdefault('store',
                        kittystore.get_store(settings.KITTYSTORE_URL,
                                             settings.KITTYSTORE_DEBUG))
        try:
            return self._app(environ, start_response)
        finally:
            environ['kittystore.store'].rollback()
            #environ['kittystore.store'].close()


class KittyStoreDjangoMiddleware(object):
    """Django middleware.
    Add KittyStore object in environ['kittystore.store']. Each thread contains
    own store.
    """

    def __init__(self):
        """Create Django middleware."""
        self._local = local()

    def process_request(self, request):
        try:
            request.environ['kittystore.store']  = self._local.store
        except AttributeError:
            request.environ['kittystore.store']  = \
                    self._local.__dict__.setdefault('store',
                        kittystore.get_store(settings.KITTYSTORE_URL,
                                             settings.KITTYSTORE_DEBUG))

    #def process_response(self, request, response):
    #    request.environ['kittystore.store'].close()
    #    return response

    def process_exception(self, request, exception):
        request.environ['kittystore.store'].rollback()