Skip to content

Commit 06cbe42

Browse files
aj-fuentesKangOl
andcommitted
[IMP] spreadsheet: add iter_commands
The logic to iterate the commands of spreadsheets is reused in many sripts. We collect it here into one util that handles the update if anything changed. closes #147 Related: odoo/upgrade#6588 Signed-off-by: Christophe Simonis (chs) <[email protected]> Co-authored-by: Christophe Simonis (chs) <[email protected]>
1 parent 0833a35 commit 06cbe42

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/util/spreadsheet/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
from .misc import *
12
from .tokenizer import *

src/util/spreadsheet/misc.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json
2+
3+
4+
def iter_commands(cr, like_all=(), like_any=()):
5+
if not (bool(like_all) ^ bool(like_any)):
6+
raise ValueError("Please specify `like_all` or `like_any`, not both")
7+
cr.execute(
8+
"""
9+
SELECT id,
10+
commands
11+
FROM spreadsheet_revision
12+
WHERE commands LIKE {}(%s::text[])
13+
""".format("ALL" if like_all else "ANY"),
14+
[list(like_all or like_any)],
15+
)
16+
for revision_id, data in cr.fetchall():
17+
data_loaded = json.loads(data)
18+
if "commands" not in data_loaded:
19+
continue
20+
data_old = json.dumps(data_loaded, sort_keys=True)
21+
22+
changed = yield data_loaded["commands"]
23+
if changed is None:
24+
changed = data_old != json.dumps(data_loaded, sort_keys=True)
25+
26+
if changed:
27+
cr.execute(
28+
"UPDATE spreadsheet_revision SET commands=%s WHERE id=%s", [json.dumps(data_loaded), revision_id]
29+
)
30+
31+
32+
def process_commands(cr, callback, *args, **kwargs):
33+
gen = iter_commands(cr, *args, **kwargs)
34+
try:
35+
cmd = next(gen)
36+
while True:
37+
changed = callback(cmd)
38+
cmd = gen.send(changed)
39+
40+
except StopIteration:
41+
pass

0 commit comments

Comments
 (0)