Skip to content

Commit dc75d7e

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

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

maintenance_location/models/maintenance_location.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ 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+
equipment_count = fields.Integer(compute="_compute_equipment_count")
43+
44+
@api.depends("equipment_ids")
45+
def _compute_equipment_count(self):
46+
res = self.env["maintenance.equipment"].read_group(
47+
domain=[("location_id", "in", self.ids)],
48+
fields=["location_id"],
49+
groupby=["location_id"],
50+
)
51+
52+
res_dict = {x["location_id"][0]: x["location_id_count"] for x in res}
53+
54+
for location in self:
55+
location.equipment_count = res_dict.get(location.id, 0)
56+
3857
@api.depends("name", "parent_id.complete_name")
3958
def _compute_complete_name(self):
4059
for location in self:

maintenance_location/tests/test_maintenance_location.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def setUpClass(cls):
2626
{"name": "Laptop", "location_id": self.location_1.id}
2727
)
2828

29+
self.equipment = self.env["maintenance.equipment"].create(
30+
{"name": "Printer", "location_id": self.location_1.id}
31+
)
32+
2933
self.plan = self.env["maintenance.plan"].create(
3034
{
3135
"equipment_id": self.equipment.id,
@@ -51,3 +55,7 @@ def test_request_creation(self):
5155
self.assertTrue(request)
5256
for r in request:
5357
self.assertEqual(r.location_id.id, self.location_1.id)
58+
59+
def test_count_equipment(self):
60+
self.assertEqual(self.location_1.equipment_count, 3)
61+
self.assertEqual(self.location_2.equipment_count, 0)

maintenance_location/views/maintenance_location.xml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
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+
28+
<group>
29+
<field
30+
name="equipment_ids"
31+
widget="many2many_tags"
32+
string="Equipments"
33+
/>
34+
</group>
2535
</group>
2636
</sheet>
2737
</form>
@@ -42,6 +52,7 @@
4252
<field name="arch" type="xml">
4353
<tree>
4454
<field name="complete_name" />
55+
<field name="equipment_count" />
4556
</tree>
4657
</field>
4758
</record>

0 commit comments

Comments
 (0)