-
-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy path__init__.py
More file actions
74 lines (63 loc) · 2.64 KB
/
__init__.py
File metadata and controls
74 lines (63 loc) · 2.64 KB
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
# Copyright 2025 - TODAY Akretion - Raphael Valyi <raphael.valyi@akretion.com.br>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from . import models
from .hooks import post_init_hook
from . import wizards
import csv
from io import StringIO
import logging
import os
from odoo import SUPERUSER_ID, api
from odoo.tools import convert
from .demo import DEMO_NCM, DEMO_NBM, DEMO_NBS, DEMO_CEST
_logger = logging.getLogger(__name__)
def convert_csv_import(
env, module, fname, csvcontent, idref=None, mode="init", noupdate=False
):
"""
This is a monkey patch allowing filtering for faster demo/test loading
and allowing to force noupdate=True for some key fiscal data.
"""
filename, _ext = os.path.splitext(os.path.basename(fname))
model = filename.split("-")[0]
# is is one of the very large fiscal CSV data files?
if model in (
"l10n_br_fiscal.ncm",
"l10n_br_fiscal.nbm",
"l10n_br_fiscal.nbs",
"l10n_br_fiscal.cest",
):
noupdate = True
# now we will figure out if we are in demo/test mode
# at this early stage of data module loading, the demo flag of the fiscal module
# can still be False, that's why we will consider it's the demo/test mode
# if the demo flag is set for the l10n_br_base module.
modules = env["ir.module.module"].search(
[("name", "in", ("l10n_br_base", "l10n_br_fiscal"))]
)
if any(module.demo for module in modules):
# Then we will filter and load only the demo records:
input_stream = StringIO(csvcontent.decode())
output_stream = StringIO()
csv_reader = csv.reader(input_stream)
csv_writer = csv.writer(output_stream)
header = next(csv_reader)
csv_writer.writerow(header)
# Filter and write rows matching the IDs
id_column_values = globals().get(f"DEMO_{model.split('.')[-1].upper()}")
line_count = 0
for row in csv_reader:
if row[0].replace('"', "") in id_column_values:
csv_writer.writerow(row)
line_count += 1
_logger.info(
f"(faster demo/test mode: loading only {line_count} demo lines)"
)
# Get the filtered content as a string
output_stream.seek(0)
csvcontent = output_stream.getvalue().encode()
return convert.convert_csv_import._original_method(
env, module, fname, csvcontent, idref, mode, noupdate
)
convert_csv_import._original_method = convert.convert_csv_import
convert.convert_csv_import = convert_csv_import