Skip to content

Commit

Permalink
[FIX] l10n_it_asset_history_import Apply pre-commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzoallegrucci committed Oct 11, 2024
1 parent da6cc46 commit 057e7c2
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion l10n_it_asset_history_import/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
},
"depends": [
"assets_management",
"l10n_it_asset_management",
],
"data": [
"security/ir.model.access.csv",
Expand Down
3 changes: 1 addition & 2 deletions l10n_it_asset_history_import/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Asset(models.Model):
copy=False,
help="Used to import data from xls(x) files. Must be unique.",
readonly=True,
string="Import Code",
)

@api.model_create_multi
Expand Down Expand Up @@ -100,7 +99,7 @@ def make_template_file_data(self, file_headers):
template_data_list = [
line.get_template_file_data(file_headers)
for line in self.mapped("depreciation_ids.line_ids").sorted(
key=lambda l: (l.asset_id, l.depreciation_id, l.date)
key=lambda ln: (ln.asset_id, ln.depreciation_id, ln.date)
)
]
for template_data in template_data_list:
Expand Down
1 change: 0 additions & 1 deletion l10n_it_asset_history_import/models/asset_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class AssetCategory(models.Model):
copy=False,
help="Used to import data from xls(x) files. Must be unique.",
readonly=True,
string="Import Code",
)

@api.model_create_multi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class DepreciationMode(models.Model):
copy=False,
help="Used to import data from xls(x) files. Must be unique.",
readonly=True,
string="Import Code",
)

@api.model_create_multi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class DepreciationType(models.Model):
copy=False,
help="Used to import data from xls(x) files. Must be unique.",
readonly=True,
string="Import Code",
)

@api.model_create_multi
Expand Down
6 changes: 3 additions & 3 deletions l10n_it_asset_history_import/views/asset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<record model="ir.ui.view" id="asset_search_view_inherit">
<field name="name">asset.search.view</field>
<field name="model">asset.asset</field>
<field name="inherit_id" ref="assets_management.asset_search_view" />
<field name="inherit_id" ref="l10n_it_asset_management.asset_search_view" />
<field name="arch" type="xml">
<xpath expr="/search/field[@name='code']" position="after">
<field name="import_code" />
Expand All @@ -15,7 +15,7 @@
<record model="ir.ui.view" id="asset_form_view_inherit">
<field name="name">asset.form.view</field>
<field name="model">asset.asset</field>
<field name="inherit_id" ref="assets_management.asset_form_view" />
<field name="inherit_id" ref="l10n_it_asset_management.asset_form_view" />
<field name="arch" type="xml">
<xpath expr="//group/group/field[@name='code']" position="after">
<field name="import_code" />
Expand All @@ -26,7 +26,7 @@
<record model="ir.ui.view" id="asset_tree_view_inherit">
<field name="name">asset.tree.view</field>
<field name="model">asset.asset</field>
<field name="inherit_id" ref="assets_management.asset_tree_view" />
<field name="inherit_id" ref="l10n_it_asset_management.asset_tree_view" />
<field name="arch" type="xml">
<xpath expr="//field[@name='code']" position="after">
<field name="import_code" />
Expand Down
5 changes: 4 additions & 1 deletion l10n_it_asset_history_import/views/asset_category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
<record model="ir.ui.view" id="asset_category_form_view_inherit">
<field name="name">asset.category.form.view</field>
<field name="model">asset.category</field>
<field name="inherit_id" ref="assets_management.asset_category_form_view" />
<field
name="inherit_id"
ref="l10n_it_asset_management.asset_category_form_view"
/>
<field name="arch" type="xml">
<xpath expr="//group/group/field[@name='tag_ids']" position="before">
<field name="import_code" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="model">asset.depreciation.mode</field>
<field
name="inherit_id"
ref="assets_management.asset_depreciation_mode_form_view"
ref="l10n_it_asset_management.asset_depreciation_mode_form_view"
/>
<field name="arch" type="xml">
<xpath expr="//group/group/field[@name='name']" position="after">
Expand All @@ -20,7 +20,7 @@
<field name="model">asset.depreciation.mode</field>
<field
name="inherit_id"
ref="assets_management.asset_depreciation_mode_tree_view"
ref="l10n_it_asset_management.asset_depreciation_mode_tree_view"
/>
<field name="arch" type="xml">
<xpath expr="//field[@name='name']" position="after">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="model">asset.depreciation.type</field>
<field
name="inherit_id"
ref="assets_management.asset_depreciation_type_tree_view"
ref="l10n_it_asset_management.asset_depreciation_type_tree_view"
/>
<field name="arch" type="xml">
<xpath expr="//field[@name='name']" position="after">
Expand All @@ -20,7 +20,7 @@
<field name="model">asset.depreciation.type</field>
<field
name="inherit_id"
ref="assets_management.asset_depreciation_type_form_view"
ref="l10n_it_asset_management.asset_depreciation_type_form_view"
/>
<field name="arch" type="xml">
<xpath expr="//field[@name='name']" position="after">
Expand Down
47 changes: 29 additions & 18 deletions l10n_it_asset_history_import/wizards/asset_history_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,11 @@ def get_header_by_model_and_field(model, field):
if h.model == model and h.field == field:
return h
raise ValueError(

Check warning on line 264 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L263-L264

Added lines #L263 - L264 were not covered by tests
_("No file column for model `{}` and field `{}`").format(model, field)
_(
"No file column for model `%(model)s` and field `%(field)s`",
model=model,
field=field,
)
)


Expand All @@ -278,20 +282,20 @@ def to_date(v, w, s):
try:

Check warning on line 282 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L282

Added line #L282 was not covered by tests
if not v:
return fields.Date.to_date(v)

Check warning on line 284 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L284

Added line #L284 was not covered by tests
if isinstance(v, (int, float)):
if isinstance(v, (int | float)):
return date(*xlrd.xldate_as_tuple(v, w.datemode)[:3])
return datetime.strptime(v, "%d/%m/%Y").date()
except Exception:
raise ValidationError(_(f"Invalid date {v}"))
except Exception as exc:
raise ValidationError(_(f"Invalid date {v}")) from exc

Check warning on line 289 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L286-L289

Added lines #L286 - L289 were not covered by tests


def to_float(v, w, s):
try:

Check warning on line 293 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L293

Added line #L293 was not covered by tests
if not v:
return 0
return float(v)
except Exception:
raise ValidationError(_(f"Invalid float number {v}"))
except Exception as exc:
raise ValidationError(_(f"Invalid float number {v}")) from exc

Check warning on line 298 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L295-L298

Added lines #L295 - L298 were not covered by tests


def to_selection(v, w, s):
Expand Down Expand Up @@ -345,11 +349,11 @@ def get_default_company_id(self):
string="Company",
)

file = fields.Binary(string="File")
file = fields.Binary()

filename = fields.Char(string="File Name")

template_file = fields.Binary(string="Template File")
template_file = fields.Binary()

def download_template_file(self):
"""
Expand Down Expand Up @@ -392,7 +396,7 @@ def import_file(self):

def launch_view(self, asset_ids):
"""Opens tree view upon assets"""
act = self.env.ref("assets_management.action_asset").read(load="")[0]
act = self.env.ref("l10n_it_asset_management.action_asset").read(load="")[0]
act.update({"domain": [("id", "in", asset_ids)]})
return act

Check warning on line 401 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L399-L401

Added lines #L399 - L401 were not covered by tests

Expand Down Expand Up @@ -458,8 +462,10 @@ def convert_to_dep_vals(self, dep_dict, workbook, sheet):
raise ValidationError(

Check warning on line 462 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L462

Added line #L462 was not covered by tests
_(
"Could not retrieve depreciation mode and type by codes"
" '{}' and '{}'."
).format(mode_code, type_code)
" '%(mode_code)s' and '%(type_code)s'.",
mode_code=mode_code,
type_code=type_code,
)
)

vals.update(

Check warning on line 471 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L471

Added line #L471 was not covered by tests
Expand Down Expand Up @@ -500,9 +506,11 @@ def import_assets_from_data(self, file_data, workbook, sheet):
if missing_h:
raise ValidationError(

Check warning on line 507 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L507

Added line #L507 was not covered by tests
_(
"Line {} misses required info on column(s) {}."
" Aborting import."
).format(str(n), ", ".join(missing_h))
"Line %(nr)s misses required info on column(s) %(missing)s."
" Aborting import.",
nr=str(n),
missing=", ".join(missing_h),
)
)

key = tuple(row[h.col] for h in HEADERS if h.field == "import_code")
Expand All @@ -529,8 +537,9 @@ def import_assets_from_data(self, file_data, workbook, sheet):
}
)

ctx = dict(self._context or [], skip_depreciation_creation=True)
asset_obj = self.env["asset.asset"].with_context(ctx)
asset_obj = self.env["asset.asset"].with_context(

Check warning on line 540 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L540

Added line #L540 was not covered by tests
skip_depreciation_creation=True
)
dep_obj = self.env["asset.depreciation"]
dep_line_obj = self.env["asset.depreciation.line"]
asset_ids = set()
Expand Down Expand Up @@ -586,8 +595,10 @@ def parse_file(self):
try:
workbook = xlrd.open_workbook(file_contents=base64.decodebytes(self.file))
sheet = workbook.sheet_by_index(0)
except xlrd.XLRDError:
raise ValidationError(_("Attention! Invalid xls(x) file. Aborting import."))
except xlrd.XLRDError as exc:
raise ValidationError(

Check warning on line 599 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L595-L599

Added lines #L595 - L599 were not covered by tests
_("Attention! Invalid xls(x) file. Aborting import.")
) from exc

file_headers = sheet.row_values(0)

Check warning on line 603 in l10n_it_asset_history_import/wizards/asset_history_import.py

View check run for this annotation

Codecov / codecov/patch

l10n_it_asset_history_import/wizards/asset_history_import.py#L603

Added line #L603 was not covered by tests
if not file_headers:
Expand Down
10 changes: 3 additions & 7 deletions l10n_it_asset_history_import/wizards/asset_history_import.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
<field name="model">wizard.asset.history.import</field>
<field name="arch" type="xml">
<form>
<group name="file" string="File">
<sheet>
<field name="file" filename="filename" nolabel="1" />
<field name="filename" invisible="1" />
</group>
<group name="template_file">
<div class="oe_inline">
<p>
Files will be imported following the position of every single column.
Expand All @@ -30,15 +28,13 @@
/>
</p>
</div>
</group>
<group name="settings">
<group name="company" groups="base.group_multi_company">
<field
name="company_id"
options="{'no_open': 1, 'no_create_edit': True}"
/>
</group>
</group>
</sheet>
<footer>
<button
name="import_file"
Expand All @@ -64,7 +60,7 @@
id="menu_wizard_asset_history_import"
name="Assets History Import"
action="action_wizard_asset_history_import"
parent="assets_management.menu_asset_management"
parent="l10n_it_asset_management.menu_asset_management"
sequence="10"
/>

Expand Down

0 comments on commit 057e7c2

Please sign in to comment.