Skip to content

Commit 6080f12

Browse files
author
Ana Zurabashvili [anzu]
committed
[IMP] estate: Chapter 10: Constraints
- Add "Cancel" and "Sold" buttons to estate.property with state change logic and constraints. - Add "Accept" and "Refuse" buttons to estate.property.offer. - Accepting an offer sets the property's buyer and selling price. - Added constraint that only one offer can be accepted per property. [email protected]
1 parent a2a0010 commit 6080f12

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

estate/models/estate_property.py

+30
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ class EstateProperty(models.Model):
5757
best_price = fields.Float(string="Best price",
5858
compute='_compute_best_price', store=True)
5959

60+
_sql_constraints = [
61+
(
62+
"check_expected_price_positive",
63+
"CHECK(expected_price >= 0)",
64+
"Expected price must be strictly positive.",
65+
),
66+
(
67+
"check_selling_price_positive",
68+
"CHECK(selling_price >= 0)",
69+
"Selling price must be strictly positive.",
70+
),
71+
]
72+
6073
@api.depends('garden_area', 'living_area')
6174
def _compute_total_area(self):
6275
for val in self:
@@ -86,3 +99,20 @@ def _onchange_garden(self):
8699
else:
87100
self.garden_area = False
88101
self.garden_orientation = False
102+
103+
@api.constrains("expected_price", "selling_price")
104+
def _check_selling_price_vs_expected(self):
105+
for rec in self:
106+
if float_is_zero(rec.selling_price or 0.0, precision_digits=2):
107+
continue
108+
if float_compare(
109+
rec.selling_price, rec.expected_price * 0.90, precision_digits=2
110+
) < 0:
111+
raise ValidationError(
112+
"Here Selling price must be at least 90% of the expected price."
113+
)
114+
# if rec.selling_price and rec.selling_price <= rec.expected_price * 0.90:
115+
# raise ValidationError(
116+
# "Selling price should not be possible to accept an offer lower "
117+
# "than 90% of the expected price",
118+
# )

estate/models/estate_property_offer.py

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class EstatePropertyOffer(models.Model):
2222
inverse="_inverse_date_deadline")
2323
validity = fields.Integer(string="Validity Days", default=7)
2424

25+
_sql_constraints = [
26+
(
27+
"check_price_positive",
28+
"CHECK(price >= 0)",
29+
"Offer price must be strictly positive.",
30+
),
31+
]
32+
2533
@api.depends("validity")
2634
def _compute_date_deadline(self):
2735
for val in self:

estate/models/estate_property_tag.py

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

88
name = fields.Char(string="Property Tag", required=True)
9+
10+
_sql_constraints = [
11+
('unique_name', 'unique(name)',
12+
'Tags should have unique names.'),
13+
]

estate/models/estate_property_type.py

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

88
name = fields.Char(string="Property Type", required=True)
9+
10+
_sql_constraints = [
11+
('unique_name', 'unique(name)',
12+
'Types should have unique names.'),
13+
]

0 commit comments

Comments
 (0)