Skip to content

Commit 5782473

Browse files
committed
[ADD] estate : add data constraints from prices and uniqueness
- Added SQL constraints for positive expected and selling prices, positive offer price, and unique tag/type names. - Implemented Python constraint to ensure selling price is at least 90% of expected price. - Used float_compare() and float_is_zero() for accurate float handling. - Triggered constraint on changes to selling_price and expected_price fields.
1 parent d02d21f commit 5782473

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

estate/models/estate_property.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import timedelta
22
from odoo import api, fields, models
3-
from odoo.exceptions import UserError
3+
from odoo.exceptions import UserError, ValidationError
4+
from odoo.tools.float_utils import float_compare, float_is_zero
45

56

67
class EstateModel(models.Model):
@@ -53,6 +54,36 @@ class EstateModel(models.Model):
5354
total_area = fields.Float(compute="_compute_total_area")
5455
best_price = fields.Float(compute="_compute_best_price")
5556

57+
_sql_constraints = [
58+
(
59+
"check_property_expected_price",
60+
"CHECK(expected_price > 0)",
61+
"Property Expected Price must be a valid value",
62+
),
63+
(
64+
"check_property_selling_price",
65+
"CHECK(selling_price >= 0)",
66+
"Property Selling Price must be positive",
67+
),
68+
]
69+
70+
@api.constrains("selling_price", "expected_price")
71+
def _check_selling_price_percentage(self):
72+
for record in self:
73+
if float_is_zero(record.selling_price, precision_rounding=0.01):
74+
continue
75+
if (
76+
float_compare(
77+
record.selling_price,
78+
(record.expected_price * 0.9),
79+
precision_rounding=0.01,
80+
)
81+
< 0
82+
):
83+
raise ValidationError(
84+
"Selling Price of the property can not be less than 90 percent of the expected Price"
85+
)
86+
5687
@api.depends("living_area", "garden_area")
5788
def _compute_total_area(self):
5889
for record in self:

estate/models/estate_property_offer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class EstateOfferModel(models.Model):
1616
validity = fields.Integer(default=7)
1717
date_deadline = fields.Date(default=fields.Date.today() + timedelta(days=7))
1818

19+
_sql_constraints = [
20+
(
21+
"check_property_offer_price",
22+
"CHECK(price > 0)",
23+
"Property Offer Price must be a valid value",
24+
)
25+
]
26+
1927
@api.depends("validity", "create_date")
2028
def _compute_date_deadline(self):
2129
for record in self:

estate/models/estate_property_tag.py

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

88
name = fields.Char(required=True)
9+
10+
_sql_constraints = [
11+
("check_property_tag_name", "UNIQUE(name)", "Property Tag Name must be unique")
12+
]

estate/models/estate_property_type.py

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

88
name = fields.Char(required=True)
9+
10+
_sql_constraints = [
11+
(
12+
"check_property_type_name",
13+
"UNIQUE(name)",
14+
"Property Type Name must be unique",
15+
)
16+
]

0 commit comments

Comments
 (0)