[IMP]pms: added short_name field in rooms and tests

This commit is contained in:
braisab
2022-05-06 17:44:15 +02:00
parent 70017477a2
commit a74a4e37e0
3 changed files with 185 additions and 3 deletions

View File

@@ -101,13 +101,25 @@ class PmsRoom(models.Model):
translate=True,
)
short_name = fields.Char(
string="Short Name",
help="Four character name, if not set, autocompletes with the first two letters of "
"the room name and two incremental numbers",
)
_sql_constraints = [
(
"room_property_unique",
"unique(name, pms_property_id)",
"you cannot have more than one room "
"You cannot have more than one room "
"with the same name in the same property",
)
),
(
"room_short_name_unique",
"unique(short_name, pms_property_id)",
"You cannot have more than one room "
"with the same short name in the same property",
),
]
@api.depends("child_ids")
@@ -174,6 +186,53 @@ class PmsRoom(models.Model):
)
)
@api.constrains("short_name")
def _check_short_name(self):
for record in self:
if len(record.short_name) > 4:
raise ValidationError(
_("The short name can't contain more than 4 characters")
)
@api.model
def create(self, vals):
if vals.get("name") and not vals.get("short_name"):
if len(vals["name"]) > 4:
short_name = self.calculate_short_name(vals)
vals.update({"short_name": short_name})
else:
vals.update({"short_name": vals["name"]})
return super(PmsRoom, self).create(vals)
def write(self, vals):
if vals.get("name") and not vals.get("short_name"):
if len(vals["name"]) > 4:
short_name = self.calculate_short_name(vals)
vals.update({"short_name": short_name})
else:
vals.update({"short_name": vals["name"]})
return super(PmsRoom, self).write(vals)
def calculate_short_name(self, vals):
short_name = vals["name"][:2].upper()
pms_property_id = self.pms_property_id.id
if vals.get("pms_property_id"):
pms_property_id = vals["pms_property_id"]
rooms = self.env["pms.room"].search([("pms_property_id", "=", pms_property_id)])
same_name_rooms = rooms.filtered(
lambda room: room.name[:2].upper() == short_name
)
numbers_name = [0]
for room in same_name_rooms:
if room.short_name and room.short_name[:2] == short_name:
if all(character.isdigit() for character in room.short_name[2:4]):
numbers_name.append(int(room.short_name[2:4]))
max_number = max(numbers_name) + 1
if max_number < 10:
max_number = str(max_number).zfill(2)
short_name += max_number
return short_name
# Business methods
def get_capacity(self, extra_bed=0):

View File

@@ -1,5 +1,6 @@
from psycopg2 import IntegrityError
from odoo.exceptions import ValidationError
from odoo.tools import mute_logger
from .common import TestPms
@@ -218,3 +219,124 @@ class TestPmsRoom(TestPms):
expected_display_name,
"The display name of the room is not as expected",
)
def test_short_name_room_name_gt_4(self):
"""
It checks through subtest that the short names of the
rooms are correctly established when the names of these
exceed 4 characters.
-------------------------------------------------------
First a room_type (Sweet Imperial) is created. Then 6 rooms
are created with the name Sweet Imperial + room number. Finally
in a loop we check that the short name of the rooms was set
correctly: 'SW01, SW02, SW03...'
"""
self.room_type2 = self.env["pms.room.type"].create(
{
"pms_property_ids": [self.pms_property1.id],
"name": "Sweet Imperial",
"default_code": "SWI",
"class_id": self.room_type_class1.id,
"list_price": 100,
}
)
rooms = []
self.room1 = self.env["pms.room"].create(
{
"name": "Sweet Imperial 101",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type2.id,
}
)
rooms.append(self.room1)
self.room2 = self.env["pms.room"].create(
{
"name": "Sweet Imperial 102",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type2.id,
}
)
rooms.append(self.room2)
self.room3 = self.env["pms.room"].create(
{
"name": "Sweet Imperial 103",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type2.id,
}
)
rooms.append(self.room3)
self.room4 = self.env["pms.room"].create(
{
"name": "Sweet Imperial 104",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type2.id,
}
)
rooms.append(self.room4)
self.room5 = self.env["pms.room"].create(
{
"name": "Sweet Imperial 105",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type2.id,
}
)
rooms.append(self.room5)
self.room6 = self.env["pms.room"].create(
{
"name": "Sweet Imperial 106",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type2.id,
}
)
rooms.append(self.room6)
for index, room in enumerate(rooms, start=1):
with self.subTest(room):
self.assertEqual(
room.short_name,
"SW0" + str(index),
"The short name of the room should be SW0" + str(index),
)
def test_short_name_room_name_lt_4(self):
"""
Checks that the short name of a room is equal to the name
when it does not exceed 4 characters.
---------------------------------------------------------
A room is created with a name less than 4 characters (101).
Then it is verified that the short name and the name of the
room are the same.
"""
self.room1 = self.env["pms.room"].create(
{
"name": "101",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type1.id,
}
)
self.assertEqual(
self.room1.short_name,
self.room1.name,
"The short name of the room should be equal to the name of the room",
)
def test_short_name_gt_4_constraint(self):
"""
Check that the short name of a room cannot exceed 4 characters.
--------------------------------------------------------------
A room named 201 is created. Afterwards, it is verified that a
ValidationError is thrown when trying to change the short name
of that room to 'SIN-201'.
"""
self.room1 = self.env["pms.room"].create(
{
"name": "201",
"pms_property_id": self.pms_property1.id,
"room_type_id": self.room_type1.id,
}
)
with self.assertRaises(
ValidationError,
msg="The short_name of the room should not be able to be write.",
):
self.room1.write({"short_name": "SIN-201"})

View File

@@ -45,6 +45,7 @@
/>
<field name="capacity" />
<field name="extra_beds_allowed" />
<field name="short_name" />
<!-- <field name="uom_id" invisible="1" /> -->
</group>
<group>
@@ -185,7 +186,7 @@
<field name="capacity" />
<field name="extra_beds_allowed" />
<field name="room_amenity_ids" widget="many2many_tags" />
<field name="short_name" />
</tree>
</field>
</record>