Skip to content

Commit 5f480f5

Browse files
committed
[IMP] estate: add SQL and Python constraints - Chapter 10
SQL constraints: expected_price, selling_price, offer price, property tag and type Python constraints: selling price >= 0.9 of the expected price
1 parent 79ff504 commit 5f480f5

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-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 odoo import api, fields, models
22
from dateutil.relativedelta import relativedelta
33
from datetime import datetime
4-
from odoo.exceptions import UserError
4+
from odoo.exceptions import UserError, ValidationError
5+
from odoo.tools.float_utils import float_compare
56

67

78
class EstateProperty(models.Model):
@@ -55,6 +56,13 @@ class EstateProperty(models.Model):
5556
total_area = fields.Integer("Total Area (sqm)", compute="_compute_area")
5657
best_price = fields.Float("Best Price", compute="_compute_best_price")
5758

59+
_check_expected_price = models.Constraint(
60+
"CHECK(expected_price > 0)", "The expected price must be positive"
61+
)
62+
_check_selling_price = models.Constraint(
63+
"CHECK(selling_price > 0)", "The selling price must be positive"
64+
)
65+
5866
@api.depends("living_area", "garden_area")
5967
def _compute_area(self):
6068
for record in self:
@@ -91,3 +99,14 @@ def action_sell_property(self):
9199

92100
for record in self:
93101
record.state = "sold"
102+
103+
@api.constrains("expected_price", "selling_price")
104+
def _check_selling_price(self):
105+
for record in self:
106+
if len(record.offer_ids) > 0 and (
107+
float_compare(record.selling_price, record.expected_price * 0.9, 5)
108+
== -1
109+
):
110+
raise ValidationError(
111+
"The selling price cannot be lower than the 90% of the expected price"
112+
)

estate/models/estate_property_offer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class EstatePropertyOffer(models.Model):
1414
property_id = fields.Many2one("estate_property", required=True)
1515
validity = fields.Integer("Validity", default=7)
1616
date_deadline = fields.Date("Deadline", compute="_compute_date_deadline")
17+
_check_price = models.Constraint(
18+
"CHECK(price > 0)", "Offer's price must be positive"
19+
)
1720

1821
@api.depends("validity", "create_date")
1922
def _compute_date_deadline(self):

estate/models/estate_property_tag.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ class EstatePropertyTag(models.Model):
55
_name = "estate_property_tag"
66
_description = "Estate Property Tag"
77
name = fields.Char("Name", required=True)
8+
_name_unique = models.Constraint("unique(name)", "Tag name must be unique")

estate/models/estate_property_type.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ class EstatePropertyType(models.Model):
55
_name = "estate_property_type"
66
_description = "Estate Property Type"
77
name = fields.Char(required=True)
8+
_name_unique = models.Constraint("unique(name)", "Type must be unique")

0 commit comments

Comments
 (0)