Skip to content

Commit b081baf

Browse files
committed
[IMP] estate: Add validation conditions
1 parent 9deb164 commit b081baf

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

estate/models/estate_property.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from odoo import api, fields, models
2-
from odoo.exceptions import UserError
2+
from odoo.exceptions import UserError, ValidationError
3+
from odoo.tools.float_utils import float_is_zero, float_compare
34

45

56
class EstateProperty(models.Model):
@@ -58,6 +59,15 @@ class EstateProperty(models.Model):
5859
tag_ids = fields.Many2many('estate.property.tag', string='Tags')
5960
offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers')
6061

62+
_check_expected_price = models.Constraint(
63+
'CHECK(expected_price > 0)',
64+
'The expected price of a property should be strictly positive.',
65+
)
66+
_check_selling_price = models.Constraint(
67+
'CHECK(selling_price >= 0)',
68+
'The selling price of a property should be positive.',
69+
)
70+
6171
@api.depends('living_area', 'garden_area')
6272
def _compute_total_area(self):
6373
for record in self:
@@ -79,6 +89,16 @@ def _onchange_garden(self):
7989
self.garden_area = None
8090
self.garden_orientation = None
8191

92+
@api.constrains('selling_price', 'expected_price')
93+
def _check_selling_price(self):
94+
for record in self:
95+
if (
96+
not float_is_zero(record.selling_price, 2)
97+
and float_compare(record.selling_price, record.expected_price * 0.90, 2) < 0
98+
):
99+
raise ValidationError("Selling price cannot be lower than 90% of expected price")
100+
101+
82102
def action_mark_as_sold(self):
83103
for record in self:
84104
if record.state == 'cancelled':

estate/models/estate_property_offer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class EstatePropertyOffer(models.Model):
1919
validity = fields.Integer(default=7)
2020
date_deadline = fields.Date(compute='_compute_deadline', inverse="_inverse_deadline")
2121

22+
_check_price = models.Constraint(
23+
'CHECK(price > 0)',
24+
'The price of an offer should be strictly positive.',
25+
)
26+
2227
@api.depends('create_date', 'validity')
2328
def _compute_deadline(self):
2429
for record in self:

estate/models/estate_property_tag.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ class EstatePropertyTag(models.Model):
66
_description = 'All property tags'
77

88
name = fields.Char(required=True)
9+
10+
_check_unique_name = models.Constraint(
11+
'unique (name)',
12+
'The name of a tag should be unique.',
13+
)

0 commit comments

Comments
 (0)