Skip to content

Commit 3d66c40

Browse files
committed
[IMP] estate: add some sql and python constraints
1 parent 1775268 commit 3d66c40

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

estate/models/estate_properties.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
from dateutil.relativedelta import relativedelta
33
from odoo import fields, models, api
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 Properties(models.Model):
@@ -45,6 +46,11 @@ class Properties(models.Model):
4546
total_area = fields.Float("Total Area", compute="_compute_area")
4647
best_price = fields.Float("Best Price", compute="_find_best_price")
4748

49+
_sql_constraints = [
50+
('positive_expecting_price', "CHECK(expected_price > 0)", "Property should have a strictly positive expected price !"),
51+
("positive_sell_price", "CHECK(selling_price > 0)", "Property should have a strictly positive selling price !"),
52+
]
53+
4854
@api.depends("garden_area", "living_area")
4955
def _compute_area(self):
5056
for record in self:
@@ -79,3 +85,9 @@ def action_cancel(self):
7985
else:
8086
raise UserError("Property is already sold")
8187
return True
88+
89+
@api.constrains("selling_price")
90+
def _check_minimum_sell_price(self):
91+
for a_property in self:
92+
if float_compare(a_property.selling_price, a_property.expected_price * 0.9, 2) == -1:
93+
raise ValidationError("The seling prce can not be lower than 90% of the expected price")

estate/models/estate_property_offer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class PropertyOffer(models.Model):
1414
date_deadline = fields.Date(default=fields.Date.today() + relativedelta(days=7), compute="_compute_deadline", inverse="_update_validity")
1515
validity = fields.Integer(default=7)
1616

17+
_sql_constraints = [
18+
("positive_offer_price", "CHECK(price > 0)", "Offer should have a strictly positive price !")
19+
]
20+
1721
@api.depends("validity")
1822
def _compute_deadline(self):
1923
for record in self:

estate/models/estate_property_tag.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ class PropertyTags(models.Model):
66
_description = "Tags used to describe a property"
77

88
name = fields.Char("name", required=True)
9+
10+
_sql_constraints = [("unique_property_tag", "UNIQUE(name)", "Tag name must be unique !")]

estate/models/estate_property_type.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ class PropertyType(models.Model):
66
_description = "the type of an estate"
77

88
name = fields.Char("Type", required=True)
9+
10+
_sql_constraints = [("unique_property_tag", "UNIQUE(name)", "Tag name must be unique !")]

0 commit comments

Comments
 (0)