Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP]add seq to inspection template #160

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FleetVehicleInspection(models.Model):
def _compute_line_data_for_template_change(self, line):
return {
"inspection_item_id": line.inspection_template_item_id.id,
"sequence": line.sequence,
"state": "draft",
}

Expand All @@ -25,7 +26,11 @@ def _onchange_inspection_template_id(self):
self.note = self.inspection_template_id.note

inspection_lines = [(5, 0, 0)]
for line in self.inspection_template_id.inspection_template_line_ids:
# Sort the lines by sequence before appending
for line in sorted(
self.inspection_template_id.inspection_template_line_ids,
key=lambda linei: linei.sequence,
):
Comment on lines +29 to +33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Sort the lines by sequence before appending
for line in sorted(
self.inspection_template_id.inspection_template_line_ids,
key=lambda linei: linei.sequence,
):
for line in self.inspection_template_id.inspection_template_line_ids.sorted("sequence"):

💡 tip: you can just use the model's sorted method

data = self._compute_line_data_for_template_change(line)
inspection_lines.append((0, 0, data))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ class FleetVehicleInspectionTemplateLine(models.Model):
required=True,
copy=True,
)

sequence = fields.Integer(default=10)
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def setUpClass(cls):

cls.item_02 = cls.inspection_item.create({"name": "Mirrors"})

cls.inspection_template = cls.inspection_template.create(
cls.inspection_template_01 = cls.inspection_template.create(
{
"name": "TemplateTest",
"name": "TemplateTest_01",
"inspection_template_line_ids": [
(
0,
Expand All @@ -35,15 +35,69 @@ def setUpClass(cls):
}
)

cls.inspection_template_02 = cls.inspection_template.create(
{
"name": "TemplateTest_02",
"inspection_template_line_ids": [
(
0,
0,
{
"inspection_template_item_id": cls.item_01.id,
"sequence": 11, # Different sequence in the template line
},
),
(
0,
0,
{
"inspection_template_item_id": cls.item_02.id,
"sequence": 10, # Different sequence in the template line
},
),
],
}
)

cls.inspection = cls.inspection.create(
{
"vehicle_id": cls.vehicle,
"inspection_template_id": cls.inspection_template.id,
"inspection_template_id": cls.inspection_template_01.id,
}
)

def test_fleet_vehicle_inspection(self):
# --- Test with an inspection template ---
self.inspection._onchange_inspection_template_id()

self.assertEqual(self.inspection.name, self.inspection_template_01.name)
self.assertTrue(self.inspection.inspection_line_ids)

# --- Change the template ID ---
self.inspection.inspection_template_id = self.inspection_template_02

# Trigger the onchange method again
self.inspection._onchange_inspection_template_id()
self.assertEqual(self.inspection.name, self.inspection_template.name)

self.assertEqual(len(self.inspection.inspection_line_ids), 2)

# Check if the sequence is correctly copied from the template line
line_1 = self.inspection.inspection_line_ids.filtered(
lambda linei: linei.inspection_item_id == self.item_01
)
self.assertEqual(line_1.sequence, 11)

# --- Test without an inspection template ---
self.inspection.inspection_template_id = False # Remove the template

# Trigger the onchange method again
self.inspection._onchange_inspection_template_id()

# Assert that the name and note are not changed
self.assertEqual(self.inspection.name, self.inspection_template_02.name)
# (remains the same as the previous template)
self.assertNotEqual(self.inspection.name, self.inspection_template_01.name)

# Assert that the inspection lines are NOT removed
self.assertTrue(self.inspection.inspection_line_ids)
self.assertEqual(len(self.inspection.inspection_line_ids), 2)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
>
<field name="inspection_template_line_ids">
<tree editable="bottom">
<field name="sequence" widget="handle" />
<field
name="inspection_template_item_id"
string="Inspection Template Line"
Expand Down
Loading