forked from modoolar/scrummer
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi.py
120 lines (92 loc) · 3.3 KB
/
api.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright 2017 - 2018 Modoolar <[email protected]>
# License LGPLv3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html).
from odoo import api
class SyncerContext(object):
def __init__(self, env, data=None):
self._env = env
self._data = data or {"notifications": {}, "values": {}}
def begin(self, records):
if self.is_started():
return
self._data.update(
{
"origin": {records._name: [r.id for r in records]},
# 'values': {},
"level": 0,
}
)
def is_started(self):
return "origin" in self._data
@property
def data(self):
return self._data
@property
def origin(self):
return self._data["origin"]
@property
def values(self):
return self._data["values"]
def update(self, record, values):
self.values.setdefault(record._name, {}).setdefault(
record.id, {}
).update(values)
@property
def records(self):
records = []
for res_model in self.values:
for record_id, data in self.values[res_model].items():
record = self._env[res_model].browse(record_id)
indirect = not (
record._name in self.origin
and record.id in self.origin[record._name]
)
records.append((data, record, indirect))
return records
@property
def level(self):
return self._data["level"]
def level_up(self):
self._data["level"] += 1
def level_down(self):
self._data["level"] -= 1
@property
def is_top_level(self):
return self.level == 0
@property
def notifications(self):
return self._data["notifications"]
@property
def has_notifications(self):
return len(self.notifications) > 0
def push(self, notification):
notif = self.notifications.setdefault(notification[0], {}).setdefault(
notification[1][0], notification
)
if notification[1][1]["method"] in ["create", "unlink"]:
notif[1][1]["method"] = notification[1][1]["method"]
notif[1][1]["data"].update(notification[1][1]["data"])
def send(self):
if self.has_notifications:
notifications = []
for model_header, data in self.notifications.items():
for record_header, notification in data.items():
notifications.append(notification)
self._data = {"notifications": {}, "values": {}}
self._env["bus.bus"].sendmany(notifications)
class SyncerEnvironment(api.Environment):
def __new__(cls, cr, uid, context, syncer_data=None):
new = super(SyncerEnvironment, cls).__new__(cls, cr, uid, context)
new._syncer = SyncerContext(new, syncer_data)
return new
def __call__(self, cr=None, user=None, context=None):
cr = self.cr if cr is None else cr
uid = self.uid if user is None else int(user)
context = self.context if context is None else context
new = SyncerEnvironment(
cr, uid, context, self.syncer and self.syncer.data or None
)
return new
@property
def syncer(self):
return self._syncer
api.Environment = SyncerEnvironment