Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Oct 21, 2024
1 parent fd02a15 commit 42827d5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
12 changes: 10 additions & 2 deletions sheet_dataframe_process/models/file_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ class FileConfig(models.Model):
_rec_name = "model_id"

model_id = fields.Many2one(
comodel_name="ir.model", required=True, ondelete="cascade"
comodel_name="ir.model",
required=True,
copy=False,
ondelete="cascade",
tracking=True,
)
code = fields.Char(help="Allow to browse between several identical models")
action = fields.Selection(
Expand All @@ -20,18 +24,22 @@ class FileConfig(models.Model):
("dataframe", "Dataframe"),
],
default="display",
tracking=True,
help="Some other behaviors can be implemented",
)
on_fail = fields.Selection(
selection=[("stop", "Stop"), ("skip", "Skip record (TODO)")],
default="stop",
tracking=True,
help="What should be the behavior in case of failure regarding constraint "
"fields (required, format, etc)\n\n"
" - 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))]"
comodel_name="res.partner",
domain="[('active', 'in', (True, False))]",
tracking=True,
)
field_ids = fields.One2many(
comodel_name="file.field", inverse_name="config_id", copy=True
Expand Down
2 changes: 1 addition & 1 deletion sheet_dataframe_process/models/file_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class FileField(models.Model):
_name = "file.field"
_inherit = ["mail.thread"]
_description = "Configuration de l'import de champ"
_order = "field_id ASC"

Expand All @@ -21,6 +22,5 @@ class FileField(models.Model):
readonly=True,
)
required = fields.Boolean(
tracking=True,
help="Prevent to import missing data if field is missing in some records",
)
3 changes: 2 additions & 1 deletion sheet_dataframe_process/views/file_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
context="{'config_id': parent}"
/>
<field name="name" string="Partner" readonly="1" />
<field name="city" />
<field name="city" optional="hide" />
<field name="id" optional="hide" />
</list>
</field>
</group>
Expand Down
2 changes: 1 addition & 1 deletion sheet_dataframe_process/views/file_partner_field.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
optional="hide"
/>
<field name="field_id" context="{'technical_name': 1}" readonly="1" />
<field name="partner_id" readonly="1" />
<field name="matching_column" placeholder="Name in the spreadsheet" />
<field name="partner_id" readonly="1" />
</list>
</field>
</record>
Expand Down
20 changes: 12 additions & 8 deletions sheet_dataframe_process/wizards/sheet_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def _pre_process(self):
self.ensure_one()
comment = ""
df = pl.read_excel(source=base64.b64decode(self.file))
match = self.config_id.field_match_ids.filtered(
spe_records = self.config_id.field_match_ids.filtered(
lambda s, df_cols=df.columns: s.matching_column in df_cols
)
partner, specific_names = self._guess_partner(df, match)
partner, specific_names = self._guess_partner(df, spe_records)
if partner:
self.partner_id = partner.id
self.missing_cols = self._get_missing_cols(df, match, partner)
self.missing_cols = self._check_missing_cols(df, spe_records, partner)
if self.missing_cols:
comment += _(f"\nMissing columns: {self.missing_cols}")
self._dataframe2html(df)
Expand All @@ -48,10 +48,13 @@ def process(self):
- apply skip to record when required not respected
"""

def _get_missing_cols(self, df, match, partner):
def _check_missing_value(self, df):
df.rename({"foo": "apple"})

def _check_missing_cols(self, df, spe_records, partner):
map_cols = {
x.matching_column: x.field_id.name
for x in match.filtered(lambda s, part=partner: s.partner_id == part)
for x in spe_records.filtered(lambda s, part=partner: s.partner_id == part)
}
file_techn_name_cols = [map_cols.get(x, x) for x in df.columns]
required = self.config_id.field_ids.filtered(lambda s: s.required).mapped(
Expand All @@ -60,16 +63,17 @@ def _get_missing_cols(self, df, match, partner):
missing = [x for x in required if x not in file_techn_name_cols]
return missing or ""

def _guess_partner(self, df, match):
def _guess_partner(self, df, spe_records):
cols_by_part = defaultdict(list)
for line in match:
for line in spe_records:
cols_by_part[line.partner_id].append(line.matching_column)
if len(cols_by_part) > 1:
self._manage_several_partners(cols_by_part.keys())
if cols_by_part:
# only csutom _columns of first partner
# only custom columns of first partner
partner = list(cols_by_part.keys())[0]
return partner, cols_by_part[partner]
# Not supported case
return False, []

def _dataframe2html(self, df):
Expand Down
14 changes: 7 additions & 7 deletions sheet_dataframe_process/wizards/sheet_dataframe.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
<group col="2">
<group>
<field name="config_id" readonly="config_id" />
<field
name="file"
widget="binary"
filename="filename"
readonly="file"
/>
</group>
<group>
<field name="partner_id" readonly="partner_id" />
<field name="filename" invisible="1" />
</group>
<field
name="file"
widget="binary"
filename="filename"
readonly="file"
options="{'accepted_file_extensions': '.xlsx'}"
/>
</group>
<group colspan="1">
<field name="missing_cols" nolabel="1" />
<field name="sample" nolabel="1" />
<separator invisible="not comment" />
<field
Expand Down

0 comments on commit 42827d5

Please sign in to comment.