Skip to content

Commit 3afa339

Browse files
committed
wip
1 parent 0acb1a0 commit 3afa339

File tree

3 files changed

+54
-44
lines changed

3 files changed

+54
-44
lines changed

src/util/spreadsheet/data_wrappers.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ def create_data_source_from_cmd(cmd):
3434

3535
class Spreadsheet:
3636
def __init__(self, data: Union[str, dict]):
37-
if type(data) == str:
37+
if isinstance(data, str):
3838
data = json.loads(data)
3939
self.data = load(data)
40+
self.pivotContructor = version_gte("saas~17.2") and PivotV17_2 or PivotV16
4041

4142
def __str__(self) -> str:
4243
return self.to_json()
@@ -63,7 +64,7 @@ def odoo_charts(self):
6364

6465
@property
6566
def pivots(self) -> List[str]:
66-
return [PivotV16(d) for d in self.data.get("pivots", {}).values()]
67+
return [self.pivotContructor(d) for d in self.data.get("pivots", {}).values()]
6768

6869
@property
6970
def lists(self) -> List[str]:
@@ -256,7 +257,7 @@ def fields(self, fields: List[str]):
256257

257258
class PivotV16:
258259
"""Wrapper around a pivot data source, hiding
259-
its internal structure"""
260+
its internal structure for v16"""
260261

261262
def __init__(self, definition):
262263
self.definition = definition
@@ -309,11 +310,11 @@ def fields_matching(self):
309310

310311
@property
311312
def measures(self):
312-
return self.definition.get("measures", []) #[m["field"] for m in self.definition.get("measures", [])]
313+
return [m["field"] for m in self.definition.get("measures", [])]
313314

314315
@measures.setter
315316
def measures(self, measures):
316-
self.definition["measures"] = measures #[{"field": m} for m in measures]
317+
self.definition["measures"] = [{"field": m} for m in measures]
317318

318319
@property
319320
def row_group_by(self):
@@ -331,6 +332,14 @@ def col_group_by(self):
331332
def col_group_by(self, group_by):
332333
self.definition["colGroupBys"] = group_by
333334

335+
class PivotV17_2(PivotV16):
336+
@property
337+
def measures(self):
338+
return self.definition.get("measures", [])
339+
340+
@measures.setter
341+
def measures(self, measures):
342+
self.definition["measures"] = measures
334343

335344
class OdooChartV16:
336345
def __init__(self, definition):

src/util/spreadsheet/fields.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def rename_field(cr, model, old, new, data, revisions = ()):
3131
def remove_field_in_all_spreadsheets(cr, model, field):
3232
apply_in_all_spreadsheets(cr, field, (lambda data, revisions_data: remove_field(cr, model, field, data, revisions_data)))
3333

34+
3435
def remove_field(cr, model, field, data, revisions=()):
3536
try:
36-
3737
spreadsheet = Spreadsheet(data)
3838
_remove_field_from_filter_matching(cr, spreadsheet, model, field)
3939
adapters = _remove_field_from_list(spreadsheet, model, field)
@@ -45,6 +45,7 @@ def remove_field(cr, model, field, data, revisions=()):
4545
except:
4646
import ipdb;ipdb.set_trace()
4747

48+
4849
def _rename_function_fields(content, data_source_ids, functions, old, new):
4950
def adapter(fun_call):
5051
for arg in fun_call.args[1:]:
@@ -334,13 +335,10 @@ def adapt_re_insert(cmd):
334335

335336

336337
def _remove_field_from_pivot(spreadsheet: Spreadsheet, model, field):
337-
# try:
338338
pivot_to_delete = [
339339
pivot.id for pivot in spreadsheet.pivots if pivot.model == model and field in pivot_fields(pivot)
340340
]
341-
# except:
342-
# import ipdb;ipdb.set_trace()
343-
# pivot_to_delete = []
341+
344342
adapters = remove_pivots(
345343
spreadsheet,
346344
pivot_to_delete,
@@ -411,7 +409,8 @@ def pivot_fields(pivot):
411409
return fields
412410
# except:
413411
# import ipdb;ipdb.set_trace()
414-
# return set()
412+
# return []
413+
415414

416415

417416

src/util/spreadsheet/misc.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from .o_spreadsheet import load
1515
from .revisions import CommandAdapter, Drop
16-
from odoo.upgrade.util.misc import version_gte
16+
from odoo.addons.base.maintenance.migrations import util
1717

1818
_logger = logging.getLogger(__name__)
1919

@@ -41,34 +41,37 @@ def read_spreadsheet_snapshots(cr, like_pattern=""):
4141

4242

4343
def read_spreadsheet_initial_data(cr, like_pattern=""):
44-
cr.execute(
45-
"""
46-
SELECT doc.id AS document_id, a.id AS attachment_id, a.db_datas
47-
FROM documents_document doc
48-
LEFT JOIN ir_attachment a ON a.id = doc.attachment_id
49-
WHERE doc.handler='spreadsheet'
50-
AND position(%s::bytea in db_datas) > 0
51-
""",
52-
[like_pattern],
53-
)
54-
# TODO there are excel files in there!
55-
for document_id, attachment_id, db_datas in cr.fetchall():
56-
if db_datas:
57-
yield attachment_id, "documents.document", document_id, json.loads(db_datas.tobytes())
58-
data_field = _magic_spreadsheet_field(cr) #"spreadsheet_binary_data" if version_gte("saas~16.3") else "data"
59-
cr.execute(
60-
"""
61-
SELECT id, res_model, res_id, db_datas
62-
FROM ir_attachment
63-
WHERE res_model = 'spreadsheet.dashboard'
64-
AND res_field = %s
65-
AND position(%s::bytea in db_datas) > 0
66-
""",
67-
[data_field, like_pattern],
68-
)
69-
for attachment_id, res_model, res_id, db_datas in cr.fetchall():
70-
if db_datas:
71-
yield attachment_id, res_model, res_id, json.loads(db_datas.tobytes())
44+
if util.table_exists(cr, "documents_document"):
45+
cr.execute(
46+
"""
47+
SELECT doc.id AS document_id, a.id AS attachment_id, a.db_datas
48+
FROM documents_document doc
49+
LEFT JOIN ir_attachment a ON a.id = doc.attachment_id
50+
WHERE doc.handler='spreadsheet'
51+
AND position(%s::bytea in db_datas) > 0
52+
""",
53+
[like_pattern],
54+
)
55+
# TODO there are excel files in there!
56+
for document_id, attachment_id, db_datas in cr.fetchall():
57+
if db_datas:
58+
yield attachment_id, "documents.document", document_id, json.loads(db_datas.tobytes())
59+
60+
if util.table_exists(cr, "spreadsheet_dashboard"):
61+
data_field = _magic_spreadsheet_field(cr) #"spreadsheet_binary_data" if version_gte("saas~16.3") else "data"
62+
cr.execute(
63+
"""
64+
SELECT id, res_model, res_id, db_datas
65+
FROM ir_attachment
66+
WHERE res_model = 'spreadsheet.dashboard'
67+
AND res_field = %s
68+
AND position(%s::bytea in db_datas) > 0
69+
""",
70+
[data_field, like_pattern],
71+
)
72+
for attachment_id, res_model, res_id, db_datas in cr.fetchall():
73+
if db_datas:
74+
yield attachment_id, res_model, res_id, json.loads(db_datas.tobytes())
7275

7376
def _magic_spreadsheet_field(cr):
7477
cr.execute(
@@ -88,11 +91,11 @@ def apply_in_all_spreadsheets(cr, like_pattern, callback):
8891
# upgrade the initial data and all revisions based on it
8992
for attachment_id, res_model, res_id, db_datas in read_spreadsheet_initial_data(cr, like_pattern):
9093
print("attachment : ", attachment_id)
91-
94+
print("datas: ", len(db_datas))
9295
b = True
9396
revisions_data = []
9497
revisions_ids = []
95-
## FIXME batch the calls
98+
## FIXME TODORAR batch the calls
9699
for revision_id, commands in get_revisions(cr, res_model, res_id, like_pattern):
97100
revisions_data.append(json.loads(commands))
98101
revisions_ids.append(revision_id)
@@ -137,7 +140,7 @@ def write_attachment(cr, attachment_id, data):
137140

138141

139142
def get_revisions(cr, res_model, res_id, like_pattern):
140-
if version_gte("16.0"):
143+
if util.version_gte("16.0"):
141144
cr.execute(
142145
"""
143146
SELECT id, commands
@@ -163,7 +166,6 @@ def get_revisions(cr, res_model, res_id, like_pattern):
163166
def upgrade_data(cr, upgrade_callback):
164167
for attachment_id, _res_model, _res_id, data in read_spreadsheet_attachments(cr):
165168
upgraded_data = upgrade_callback(load(data))
166-
# import ipdb;ipdb.set_trace()
167169
cr.execute(
168170
"""
169171
UPDATE ir_attachment

0 commit comments

Comments
 (0)