Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Oct 24, 2024
1 parent 854b78f commit 054f225
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
5 changes: 0 additions & 5 deletions polars_process/models/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 1 addition & 1 deletion polars_process/models/df_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 10 additions & 10 deletions polars_process/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))


Expand Down
52 changes: 29 additions & 23 deletions polars_process/wizards/df_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,26 @@ def _pre_process(self):
self.ensure_one()
attribs = {}
df = self._get_dataframe()
self.sample = f"<pre>\n{self._2html(df)}\n</pre>"
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'<div id="{self._slug_me(key)}"><div>{key}'
f"</div>{self._2html(data)}</div>"
f'<div id="{self._slug_me(key)}">\n\t<div>{key}:</div>\n\t'
f'<div data-value="1">{self._2html(data)}\n\t</div>\n</div>'
for key, data in attribs.items()
]
)
self.comment = f"<pre>{comment}</pre>"
self.comment = comment
self._reload()

def _pre_process_hook(self, df, attribs):
Expand All @@ -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
Expand All @@ -77,22 +70,35 @@ 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):
"""
- 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", "<br />").replace("null", " ")
string = (
str(df.__repr__)[:-1].replace("\n", "<br />").replace("null", " ")
)
return f"\n\t\t<pre>{string}\n\t\t</pre>"
return df

def _reload(self):
Expand Down

0 comments on commit 054f225

Please sign in to comment.