[REF] excel_import_export: hooks are added so that they can be inherited

pull/2742/head
Rodrigo 2023-10-27 02:03:15 +02:00
parent 3a5305184a
commit 5b1cced5a9
2 changed files with 51 additions and 25 deletions

View File

@ -41,6 +41,31 @@ class XLSXExport(models.AbstractModel):
} }
return eval_context return eval_context
def _get_conditions_dict(self):
return {
"field_cond_dict": {},
"field_style_dict": {},
"style_cond_dict": {},
"aggre_func_dict": {},
}
def run_field_cond_dict(self, field):
temp_field, eval_cond = co.get_field_condition(field)
eval_cond = eval_cond or 'value or ""'
return temp_field, eval_cond
def run_field_style_dict(self, field):
return co.get_field_style(field)
def run_style_cond_dict(self, field):
return co.get_field_style_cond(field)
def run_aggre_func_dict(self, field):
return co.get_field_aggregation(field)
def apply_extra_conditions_to_value(self, field, value, conditions_dict):
return value
@api.model @api.model
def _get_line_vals(self, record, line_field, fields): def _get_line_vals(self, record, line_field, fields):
"""Get values of this field from record set and return as dict of vals """Get values of this field from record set and return as dict of vals
@ -56,40 +81,37 @@ class XLSXExport(models.AbstractModel):
raise Exception(_("Records in %s exceed max records allowed") % line_field) raise Exception(_("Records in %s exceed max records allowed") % line_field)
vals = {field: [] for field in fields} # value and do_style vals = {field: [] for field in fields} # value and do_style
# Get field condition & aggre function # Get field condition & aggre function
field_cond_dict = {} conditions_dict = self._get_conditions_dict()
aggre_func_dict = {}
field_style_dict = {}
style_cond_dict = {}
pair_fields = [] # I.e., ('debit${value and . or .}@{sum}', 'debit') pair_fields = [] # I.e., ('debit${value and . or .}@{sum}', 'debit')
for field in fields: for field in fields:
temp_field, eval_cond = co.get_field_condition(field) raw_field = field
eval_cond = eval_cond or 'value or ""' for key, condition_dict in conditions_dict.items():
temp_field, field_style = co.get_field_style(temp_field) run_func_name = "run_" + key
temp_field, style_cond = co.get_field_style_cond(temp_field) raw_field, get_result = getattr(self, run_func_name, None)(raw_field)
raw_field, aggre_func = co.get_field_aggregation(temp_field) condition_dict.update({field: get_result})
# Dict of all special conditions
field_cond_dict.update({field: eval_cond})
aggre_func_dict.update({field: aggre_func})
field_style_dict.update({field: field_style})
style_cond_dict.update({field: style_cond})
# --
pair_fields.append((field, raw_field)) pair_fields.append((field, raw_field))
for line in lines: for line in lines:
for field in pair_fields: # (field, raw_field) for field in pair_fields: # (field, raw_field)
value = self._get_field_data(field[1], line) value = self._get_field_data(field[1], line)
eval_cond = field_cond_dict[field[0]] eval_cond = conditions_dict["field_cond_dict"][field[0]]
eval_context = self.get_eval_context(line._name, line, value) eval_context = self.get_eval_context(line._name, line, value)
if eval_cond: if eval_cond:
value = safe_eval(eval_cond, eval_context) value = safe_eval(eval_cond, eval_context)
value = self.apply_extra_conditions_to_value(
field, value, conditions_dict
)
# style w/Cond takes priority # style w/Cond takes priority
style_cond = style_cond_dict[field[0]] style_cond = conditions_dict["style_cond_dict"][field[0]]
style = self._eval_style_cond(line._name, line, value, style_cond) style = self._eval_style_cond(line._name, line, value, style_cond)
if style is None: if style is None:
style = False # No style style = False # No style
elif style is False: elif style is False:
style = field_style_dict[field[0]] # Use default style style = conditions_dict["field_style_dict"][
field[0]
] # Use default style
vals[field[0]].append((value, style)) vals[field[0]].append((value, style))
return (vals, aggre_func_dict) return (vals, conditions_dict["aggre_func_dict"])
@api.model @api.model
def _eval_style_cond(self, model, record, value, style_cond): def _eval_style_cond(self, model, record, value, style_cond):

View File

@ -362,6 +362,15 @@ self['{}'] = self.env['{}'].search(self.safe_domain(self.domain))
input_dict = literal_eval(rec.input_instruction.strip()) input_dict = literal_eval(rec.input_instruction.strip())
rec.post_import_hook = input_dict.get("__POST_IMPORT__") rec.post_import_hook = input_dict.get("__POST_IMPORT__")
def _compose_field_name(self, line):
field_name = line.field_name or ""
field_name += line.field_cond or ""
field_name += line.style or ""
field_name += line.style_cond or ""
if line.is_sum:
field_name += "@{sum}"
return field_name
def _compute_output_instruction(self): def _compute_output_instruction(self):
"""From database, compute back to dictionary""" """From database, compute back to dictionary"""
for rec in self: for rec in self:
@ -390,12 +399,7 @@ self['{}'] = self.env['{}'].search(self.safe_domain(self.domain))
continue continue
if line.section_type == "data": if line.section_type == "data":
excel_cell = line.excel_cell excel_cell = line.excel_cell
field_name = line.field_name or "" field_name = self._compose_field_name(line)
field_name += line.field_cond or ""
field_name += line.style or ""
field_name += line.style_cond or ""
if line.is_sum:
field_name += "@{sum}"
cell_dict = {excel_cell: field_name} cell_dict = {excel_cell: field_name}
inst_dict[itype][prev_sheet][prev_row].update(cell_dict) inst_dict[itype][prev_sheet][prev_row].update(cell_dict)
continue continue