Skip to content

Commit 5e1f68d

Browse files
[IMP] maintenance_location: Add inverse relation and equipment count to locations
1 parent 6467c6b commit 5e1f68d

File tree

3 files changed

+91
-8
lines changed

3 files changed

+91
-8
lines changed

maintenance_location/models/maintenance_location.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,55 @@ class MaintenanceLocation(models.Model):
3535
sequence = fields.Integer(default=10)
3636
active = fields.Boolean(default=True)
3737

38+
equipment_ids = fields.One2many(
39+
"maintenance.equipment", "location_id", string="Equipments"
40+
)
41+
42+
child_equipment_ids = fields.Many2many(
43+
comodel_name="maintenance.equipment",
44+
compute="_compute_child_equipment_ids",
45+
string="Child Equipments",
46+
)
47+
48+
equipment_count = fields.Integer(compute="_compute_equipment_count")
49+
50+
def _compute_child_equipment_ids(self):
51+
all_locations = self.env["maintenance.location"].search(
52+
[("id", "child_of", self.ids)]
53+
)
54+
all_equipments = self.env["maintenance.equipment"].search(
55+
[("location_id", "in", all_locations.ids)]
56+
)
57+
for location in self:
58+
descendant_locs = all_locations.filtered(
59+
lambda sub_loc, loc=location: sub_loc.parent_path
60+
and sub_loc.parent_path.startswith(loc.parent_path)
61+
and sub_loc.id != loc.id
62+
)
63+
location.child_equipment_ids = all_equipments.filtered(
64+
lambda eq, d_locs=descendant_locs: eq.location_id in d_locs
65+
)
66+
67+
def _compute_equipment_count(self):
68+
all_locations = self.env["maintenance.location"].search(
69+
[("id", "child_of", self.ids)]
70+
)
71+
equip_data = self.env["maintenance.equipment"].read_group(
72+
domain=[("location_id", "in", all_locations.ids)],
73+
fields=["location_id"],
74+
groupby=["location_id"],
75+
)
76+
count_dict = {x["location_id"][0]: x["location_id_count"] for x in equip_data}
77+
for location in self:
78+
descendant_ids = all_locations.filtered(
79+
lambda sub_loc, loc=location: sub_loc.parent_path
80+
and sub_loc.parent_path.startswith(loc.parent_path)
81+
).ids
82+
83+
location.equipment_count = sum(
84+
count_dict.get(d_id, 0) for d_id in descendant_ids
85+
)
86+
3887
@api.depends("name", "parent_id.complete_name")
3988
def _compute_complete_name(self):
4089
for location in self:

maintenance_location/tests/test_maintenance_location.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ def setUpClass(cls):
2222
{"name": "Request", "maintenance_team_id": self.team.id}
2323
)
2424

25-
self.equipment = self.env["maintenance.equipment"].create(
25+
self.env["maintenance.equipment"].create(
2626
{"name": "Laptop", "location_id": self.location_1.id}
2727
)
2828

29+
self.env["maintenance.equipment"].create(
30+
{"name": "Printer", "location_id": self.location_1.id}
31+
)
32+
33+
self.env["maintenance.equipment"].create(
34+
{"name": "Scanner", "location_id": self.location_2.id}
35+
)
36+
2937
self.plan = self.env["maintenance.plan"].create(
3038
{
3139
"equipment_id": self.equipment.id,
@@ -38,6 +46,8 @@ def setUpClass(cls):
3846
}
3947
)
4048

49+
self.env.invalidate_all()
50+
4151
def test_maintenance_location(self):
4252
self.assertEqual(self.location_2.complete_name, "L1 / L2")
4353
with self.assertRaises(UserError):
@@ -51,3 +61,11 @@ def test_request_creation(self):
5161
self.assertTrue(request)
5262
for r in request:
5363
self.assertEqual(r.location_id.id, self.location_1.id)
64+
65+
def test_count_equipment(self):
66+
self.assertEqual(self.location_1.equipment_count, 4)
67+
self.assertEqual(self.location_2.equipment_count, 1)
68+
69+
def test_child_equipment_ids(self):
70+
self.assertEqual(len(self.location_1.child_equipment_ids), 1)
71+
self.assertEqual(len(self.location_2.child_equipment_ids), 0)

maintenance_location/views/maintenance_location.xml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,28 @@
1515
</h1>
1616
</div>
1717
<group name="first" col="2">
18-
<field name="description" />
19-
<field name="parent_id" class="oe_inline" />
20-
<label for="latitude" string="Location" />
21-
<span class="oe_inline">
22-
Latitude: <field name="latitude" nolabel="1" /><br />
23-
Longitude: <field name="longitude" nolabel="1" />
24-
</span>
18+
<group>
19+
<field name="description" />
20+
<field name="parent_id" class="oe_inline" />
21+
<label for="latitude" string="Location" />
22+
<span class="oe_inline">
23+
Latitude: <field name="latitude" nolabel="1" /><br />
24+
Longitude: <field name="longitude" nolabel="1" />
25+
</span>
26+
</group>
27+
<group>
28+
<field
29+
name="equipment_ids"
30+
widget="many2many_tags"
31+
string="Equipments"
32+
/>
33+
<field
34+
name="child_equipment_ids"
35+
widget="many2many_tags"
36+
string="Child Equipments"
37+
/>
38+
</group>
39+
2540
</group>
2641
</sheet>
2742
</form>
@@ -42,6 +57,7 @@
4257
<field name="arch" type="xml">
4358
<tree>
4459
<field name="complete_name" />
60+
<field name="equipment_count" />
4561
</tree>
4662
</field>
4763
</record>

0 commit comments

Comments
 (0)