Skip to content

Commit b2a440b

Browse files
committed
[IMP] estate: add SQL and Python constraints
- Added SQL constraints: * Expected price must be strictly positive. * Selling price must be positive. * Offer price must be strictly positive. * Property tag name must be unique. * Property type name must be unique. - Added Python constraint: * Ensure the selling price cannot be lower than 90% of the expected price (except when selling price is zero before offer validation).
1 parent 0bb058f commit b2a440b

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

estate/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
'license': 'LGPL-3',
88
'description': 'Real estate purchase & sales',
99
'data': [
10+
'security/ir.model.access.csv',
1011
'views/estate_property_views.xml',
1112
'views/estate_property_type_views.xml',
1213
'views/estate_property_tag_views.xml',
1314
'views/estate_property_offer_views.xml',
14-
'views/estate_property_menu.xml',
15-
'security/ir.model.access.csv'
15+
'views/estate_property_menu.xml'
1616
],
1717
'application': True,
1818
}

estate/models/estate_property.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
from dateutil.relativedelta import relativedelta
22
from odoo import models, api, fields, exceptions
3+
from odoo.exceptions import ValidationError
34

45

56
class EstateProperty(models.Model):
67
_name = "estate.property"
78
_description = "Real Estate Property"
8-
9+
_check_expected_price = models.Constraint(
10+
"CHECK(expected_price > 0)", "Expected price must be strictly positive."
11+
)
12+
_check_selling_price = models.Constraint(
13+
"CHECK(selling_price >= 0)", "Selling price must be positive."
14+
)
915
name = fields.Char(required=True)
1016
description = fields.Text()
1117
postcode = fields.Char()
@@ -86,3 +92,14 @@ def action_cancel_offer(self):
8692
else:
8793
record.state = "cancelled"
8894
return True
95+
96+
@api.constrains("selling_price", "expected_price")
97+
def _check_selling_price_percentage_criteria(self):
98+
for record in self:
99+
selling_price_percentage = (record.selling_price / record.expected_price) * 100
100+
if selling_price_percentage >= 90 or selling_price_percentage == 0:
101+
pass
102+
else:
103+
raise ValidationError(
104+
"The selling price cannot be lower then 90% of the expected price."
105+
)

estate/models/estate_property_offer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
class EstatePropertyOffer(models.Model):
66
_name = "estate.property.offer"
77
_description = "Estate Property Offer"
8-
8+
_check_price = models.Constraint(
9+
"CHECK(price > 0)", "Price of an offer must be positive"
10+
)
911
price = fields.Float()
1012
status = fields.Selection(
1113
selection=[("accepted", "Accepted"), ("refused", "Refused")], copy=False

estate/models/estate_property_tag.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class EstatePropertyTags(models.Model):
55
_name = "estate.property.tag"
66
_description = "Estate Property Tag"
7-
_order = "name"
8-
7+
_check_tag_name = models.Constraint(
8+
"UNIQUE(name)", "Property tag should be unique."
9+
)
910
name = fields.Char(required=True)

estate/models/estate_property_type.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
class EstatePropertyType(models.Model):
55
_name = "estate.property.type"
66
_description = "Estate Property Type"
7-
7+
_check_type_name = models.Constraint(
8+
"UNIQUE(name)", "Property type should be unique."
9+
)
810
name = fields.Char(required=True)

0 commit comments

Comments
 (0)