Skip to content

Commit 9d776f7

Browse files
committed
[IMP] estate: Add constraints
1 parent 2949458 commit 9d776f7

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

estate/models/estate_property.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from odoo import fields, models, api
22
from dateutil.relativedelta import relativedelta
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 EstateProperty(models.Model):
78

89
_name = "estate.property"
910
_description = "estate properties"
1011
_order = "id"
12+
_sql_constraints = [
13+
('check_expected_price', 'CHECK(expected_price > 0 )', 'expected price must be strictly positive'),
14+
('check_selling_price', 'CHECK(selling_price >= 0 )', 'selling price must be positive'),
15+
]
1116

1217
name = fields.Char("Title", required=True)
1318
description = fields.Text("Description")
@@ -84,3 +89,9 @@ def action_cancel(self):
8489
raise UserError("Sold properties cannot be canceled.")
8590
else:
8691
record.state = 'canceled'
92+
93+
@api.constrains('expected_price', 'selling_price')
94+
def _check_selling_price(self):
95+
for record in self:
96+
if not float_is_zero(record.selling_price, precision_rounding=0.01) and float_compare(record.selling_price, record.expected_price * 0.9, precision_rounding=0.01) < 0:
97+
raise ValidationError("the selling price cannot be lower than 90% of the expected price")

estate/models/estate_property_offer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class EstatePropertyOffer(models.Model):
88
_name = "estate.property.offer"
99
_description = "estate property offer"
1010
_order = "id"
11+
_sql_constraints = [
12+
('check_offer_price', 'CHECK(price > 0)', 'An offer price must be strictly positive')
13+
]
1114

1215
price = fields.Float(string="Price")
1316
status = fields.Selection(

estate/models/estate_property_tag.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ class EstatePropertyTag(models.Model):
66
_name = "estate.property.tag"
77
_description = "estate property tag"
88
_order = "id"
9+
_sql_constraints = [
10+
('unique_name', 'UNIQUE(name)', 'the tag name must be unique')
11+
]
912

1013
name = fields.Char("Name", required=True)

estate/models/estate_property_type.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ class EstatePropertyType(models.Model):
66
_name = "estate.property.type"
77
_description = "estate property type"
88
_order = "id"
9+
_sql_constraints = [
10+
('unique_name', 'UNIQUE(name)', 'the type name must be unique')
11+
]
912

1013
name = fields.Char("Name", required=True)

0 commit comments

Comments
 (0)