forked from shopinvader/odoo-shopinvader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopinvader_response.py
72 lines (57 loc) · 1.73 KB
/
shopinvader_response.py
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
67
68
69
70
71
72
# Copyright 2019 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import threading
from odoo.http import request
_test_mode = False
threadLocal = threading.local()
class ShopinvaderResponse(object):
"""
A response object used to enrich the response returned by a shopinvader
service with cache and session informations
"""
def __init__(self):
self._store_cache = {}
self._session = {}
def set_store_cache(self, key, value):
self._store_cache[key] = value
def set_session(self, key, value):
self._session[key] = value
@property
def store_cache(self):
"""
Read only store cache values
:return: dict
"""
return self._store_cache.copy()
@property
def session(self):
"""
Read only session values
:return: dict
"""
return self._session.copy()
def reset(self):
"""
Reset the content off the response
:return:
"""
self._session = {}
self._store_cache = {}
def set_testmode(mode):
global _test_mode
_test_mode = mode
if _test_mode:
get().reset()
def get():
"""
Returns an instance of `ShopinvaderResponse``
"""
current_local = request
if _test_mode:
# in test mode (unittest) request is not available as container to hold
# our context local data. (in a process/thread safe way). Since we are
# in test mode we can rely on the threadLocal context
current_local = threadLocal
if not hasattr(current_local, "_shopinvader_response"):
current_local._shopinvader_response = ShopinvaderResponse()
return current_local._shopinvader_response