diff --git a/polars_process/models/dataframe.py b/polars_process/models/dataframe.py index 17068131f9..a93171209e 100644 --- a/polars_process/models/dataframe.py +++ b/polars_process/models/dataframe.py @@ -34,11 +34,6 @@ class Dataframe(models.Model): " - Stop: stop the process by raising an exception\n" " - Skip record: current line'll be ignored from the next process", ) - partner_ids = fields.Many2many( - comodel_name="res.partner", - domain="[('active', 'in', (True, False))]", - tracking=True, - ) field_ids = fields.One2many( comodel_name="df.field", inverse_name="dataframe_id", copy=True ) diff --git a/polars_process/models/df_source.py b/polars_process/models/df_source.py index fc5f716c29..df7470d1fd 100644 --- a/polars_process/models/df_source.py +++ b/polars_process/models/df_source.py @@ -13,7 +13,7 @@ class DfSource(models.Model): comodel_name="dataframe", required=True, ondelete="cascade", readonly=True ) name = fields.Char() - rename = fields.Boolean(help="Display renamed Dataframe") + rename = fields.Boolean(help="Display renamed Dataframe in wizard") template = fields.Binary(string="Fichier", attachment=False) def _populate(self): diff --git a/polars_process/tests/test_module.py b/polars_process/tests/test_module.py index 51840b10f1..05e649e43b 100644 --- a/polars_process/tests/test_module.py +++ b/polars_process/tests/test_module.py @@ -8,27 +8,27 @@ class TestModule(TransactionCase): def setUpClass(cls): super().setUpClass() cls.env["df.source"]._populate() - cls.env.ref("polars_process.dataframe_contact").populate_match_lines() + # cls.env.ref("polars_process.dataframe_contact").populate_match_lines() cls.file_records = cls.env["df.source"].search([]) - def test_missing(self): - wiz = self.get_wizard(self.file_records, "missing_required") - self.assertTrue(wiz.partner_id) + def test_missing_required_column(self): + wiz = self.get_wizard(self.file_records, "missing_required_column") comment = sanitize(str(wiz.comment)) + print(comment) root = etree.fromstring(comment) + # breakpoint() # import pdb; pdb.set_trace() self.assertEqual( - root.xpath('//div[@id="missing-name-values"]/div')[0].text, - "Missing 'name' values", + root.xpath('//div[@id="missing-columns"]//div[2]')[0].text, + "['Street']", ) # def test_four_fields(self): # wiz = self.get_wizard(self.file_records, "4_fields") # self.assertFalse(wiz.missing_cols) - def get_wizard(self, file_recs, file_str): - action = file_recs.filtered( - lambda s, file_str=file_str: file_str in s.name - ).start() + def get_wizard(self, source_recs, file_str): + source = source_recs.filtered(lambda s, file_str=file_str: file_str in s.name) + action = source.start() return self.env["df.process.wiz"].browse(action.get("res_id")) diff --git a/polars_process/wizards/df_process.py b/polars_process/wizards/df_process.py index 6e0d30e9b5..629242bc2b 100644 --- a/polars_process/wizards/df_process.py +++ b/polars_process/wizards/df_process.py @@ -28,23 +28,26 @@ def _pre_process(self): self.ensure_one() attribs = {} df = self._get_dataframe() - self.sample = f"
\n{self._2html(df)}\n" + missing_columns = self._check_missing_cols(df) + if missing_columns: + attribs["Missing Columns"] = missing_columns + self.sample = f"{self._2html(df)}" source = self.df_source_id if source and source.rename: renamed_df, rdf = self._rename_df_columns(df) if renamed_df: - attribs["Renamed columns"] = rdf + attribs["Renamed Columns"] = rdf self._pre_process_hook(df, attribs) # TODO improve miss_col # attribs["miss_col"] = self._check_missing_cols(df, map_cols) - comment = " ".join( + comment = "\n".join( [ - f'
{comment}" + self.comment = comment self._reload() def _pre_process_hook(self, df, attribs): @@ -56,16 +59,6 @@ def _get_dataframe(self): name="N°", offset=1 ) - def _rename_df_columns(self, df): - map_cols = { - x.name: x.renamed - for x in self.dataframe_id.field_ids.filtered(lambda s: s.renamed) - } - new_cols = {x: map_cols.get(x) for x in df.columns if x in map_cols} - if new_cols: - return True, df.rename(new_cols) - return False, False - def _check_missing_values(self, df): requireds = self.dataframe_id.field_ids.filtered( lambda s: s.required and s.field_id.name in df.columns @@ -77,12 +70,12 @@ def _check_missing_values(self, df): dico[f"Missing '{req}' values"] = res return dico - def _check_missing_cols(self, df, map_cols): - file_techn_name_cols = [map_cols.get(x, x) for x in df.columns] - required = self.dataframe_id.field_ids.filtered(lambda s: s.required).mapped( - "field_id.name" - ) - missing = [x for x in required if x not in file_techn_name_cols] + def _check_missing_cols(self, df): + requireds = [ + x.renamed or x.name or x.field_id.name + for x in self.dataframe_id.field_ids.filtered(lambda s: s.required) + ] + missing = [x for x in requireds if x not in df.columns] return missing or "" def process(self): @@ -90,9 +83,22 @@ def process(self): - apply skip to record when required not respected """ + def _rename_df_columns(self, df): + map_cols = { + x.name: x.renamed + for x in self.dataframe_id.field_ids.filtered(lambda s: s.renamed) + } + new_cols = {x: map_cols.get(x) for x in df.columns if x in map_cols} + if new_cols: + return True, df.rename(new_cols) + return False, False + def _2html(self, df): if isinstance(df, pl.dataframe.frame.DataFrame): - return str(df.__repr__)[:-1].replace("\n", "
{string}\n\t\t" return df def _reload(self):