Skip to content

Commit d31233f

Browse files
committed
[IMP] estate: Added sql and python contraints for estate model
1 parent fe0e8f3 commit d31233f

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

estate/models/estate_property.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from odoo import models, fields, api
2-
from odoo.exceptions import UserError
2+
from odoo.exceptions import UserError, ValidationError
3+
from odoo.tools import float_utils
34

45

56
class EstateProperty(models.Model):
67
_name = "estate.property"
78
_description = "Real Estate module"
89

9-
10+
1011
name = fields.Char(required=True)
1112
tag_ids = fields.Many2many("estate.property.tag", string="Tags")
1213
property_type_id = fields.Many2one("estate.property.type", string="Property Type")
@@ -55,6 +56,13 @@ class EstateProperty(models.Model):
5556
total_area = fields.Integer(compute="_compute_total_area")
5657
best_price = fields.Float(compute="_compute_best_price")
5758

59+
_sql_constraints = [
60+
('check_expected_price', 'CHECK(expected_price > 0)',
61+
'The expected price should be strictly positive'),
62+
('check_selling_price', 'CHECK(selling_price >= 0)',
63+
'The selling price should be positive')
64+
]
65+
5866
@api.depends("living_area", "garden_area")
5967
def _compute_total_area(self):
6068
for record in self:
@@ -90,3 +98,9 @@ def cancel_action(self):
9098
else:
9199
record.state = "canceled"
92100
return True
101+
102+
@api.constrains('selling_price', 'expected_price')
103+
def _check_selling_price(self):
104+
for record in self:
105+
if not float_utils.float_is_zero(record.selling_price, precision_digits=5) and float_utils.float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=5) < 0:
106+
raise ValidationError("The selling price cannot be lower than 90% of the expected price")

estate/models/estate_property_offer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class EstatePropertyOffer(models.Model):
2222
validity = fields.Integer(default=7)
2323
date_deadline = fields.Date(compute="_compute_date_deadline", inverse="_inverse_date_deadline")
2424

25+
_sql_constraints = [
26+
('check_offer_price', 'CHECK(price > 0)',
27+
'The offer price should be strictly positive')
28+
]
29+
2530
@api.depends("create_date", "validity")
2631
def _compute_date_deadline(self):
2732
for record in self:

estate/models/estate_property_tag.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ class EstatePropertyTag(models.Model):
77

88

99
name = fields.Char(required=True)
10+
11+
_sql_constraints = [
12+
('unique_name', 'UNIQUE(name)',
13+
'Tag name should be unique')
14+
]

estate/models/estate_property_type.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ class EstatePropertyType(models.Model):
77

88

99
name = fields.Char(required=True)
10+
11+
_sql_constraints = [
12+
('unique_name', 'UNIQUE(name)',
13+
'Type name should be unique')
14+
]

0 commit comments

Comments
 (0)