Skip to content

Commit dfc0ad5

Browse files
committed
[IMP] estate: Added SQL and Python constraints for data validation
Chapter 10 : Added SQL constraints: - expected price must be strictly positive - selling price must be positive - offer price must be strictly positive - property type and tag names must be unique Added a Python constraint to prevent selling prices lower than 90% of the expected price
1 parent f21c9e7 commit dfc0ad5

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

estate/models/estate_property.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from dateutil.relativedelta import relativedelta
22
from datetime import date
33
from odoo import models, fields, api
4-
from odoo.exceptions import UserError
4+
from odoo.exceptions import UserError,ValidationError
5+
from odoo.tools.float_utils import float_compare, float_is_zero
56

67

78
class EstateProperty(models.Model):
@@ -71,6 +72,14 @@ class EstateProperty(models.Model):
7172
best_price = fields.Float(
7273
compute="_compute_best_price",
7374
)
75+
_expected_price = models.Constraint(
76+
'CHECK(expected_price >= 0)',
77+
'The expected price must be positive.',
78+
)
79+
_selling_price = models.Constraint(
80+
'CHECK(selling_price >= 0)',
81+
'The selling price must be positive.',
82+
)
7483

7584
@api.depends('living_area', 'garden_area')
7685
def _compute_total_area(self):
@@ -107,3 +116,13 @@ def action_set_sold(self):
107116
else:
108117
rec.state = "sold"
109118
return True
119+
@api.constrains("selling_price", "expected_price")
120+
def _check_selling_price(self):
121+
for rec in self:
122+
if rec.selling_price == 0:
123+
return False
124+
if float_compare(rec.selling_price, rec.expected_price * 0.9, precision_digits=2) < 0:
125+
raise ValidationError(
126+
"The selling price must be at least 90% of the expected price!\n"
127+
"You must reduce the expected price if you want to accept this offer."
128+
)

estate/models/estate_property_offer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ def action_refuse(self):
5656
for offer in self:
5757
offer.status = 'refused'
5858
return True
59+
60+
_offer_price = models.Constraint(
61+
'CHECK (price > 0)',
62+
'Offer price must be greater than 0',
63+
)

estate/models/estate_property_tag.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ class EstatePropertyTag(models.Model):
66
_description = "Real Estate Property Tag"
77

88
name = fields.Char(required=True)
9+
10+
_unique_name = models.Constraint(
11+
'unique(name)',
12+
'The tag name must be unique.',
13+
)

estate/models/estate_property_type.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ class EstatePropertyType(models.Model):
66
_description = "Real Estate Property Type"
77

88
name = fields.Char(required=True)
9+
10+
_unique_name = models.Constraint(
11+
'unique(name)',
12+
'The property type name must be unique.',
13+
)

0 commit comments

Comments
 (0)