Skip to content

Commit 7aea702

Browse files
[IMP] estate: add constraints to verify amounts and unique names (ch 10)
1 parent 953bf23 commit 7aea702

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

estate/models/estate_property.py

Lines changed: 15 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_compare, float_is_zero
34

45

56
class EstateProperty(models.Model):
@@ -44,6 +45,11 @@ class EstateProperty(models.Model):
4445
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
4546
best_price = fields.Float(string="Best Offer", compute="_compute_best_offer")
4647

48+
_check_expected_price = models.Constraint(
49+
"CHECK(expected_price > 0)", "Expected Price must be strictly positive.")
50+
_check_selling_price_positive = models.Constraint(
51+
"CHECK(selling_price >= 0)", "Selling Price must be positive.")
52+
4753
@api.depends("living_area", "garden_area")
4854
def _compute_total_area(self):
4955
for record in self:
@@ -65,6 +71,14 @@ def _onchange_garden(self):
6571
self.garden_area = 0
6672
self.garden_orientation = None
6773

74+
@api.constrains('selling_price')
75+
def _check_selling_price_90_percent(self):
76+
for record in self:
77+
if float_is_zero(record.selling_price, precision_digits=2):
78+
continue
79+
if float_compare(record.expected_price * 0.9, record.selling_price, precision_digits=2) > 0:
80+
raise ValidationError("The selling price must be at least 90% of the expected price! You must reduce the expected price if you want to accept this offer.")
81+
6882
def action_sold(self):
6983
for record in self:
7084
if record.state == "cancelled":

estate/models/estate_property_offer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class EstatePropertyOffer(models.Model):
1717
compute="_compute_date_deadline",
1818
inverse="_inverse_date_deadline")
1919

20+
_check_price = models.Constraint("CHECK(price > 0)", "Offer price must be strictly positive.")
21+
2022
@api.depends("validity")
2123
def _compute_date_deadline(self):
2224
for record in self:
@@ -31,7 +33,7 @@ def _inverse_date_deadline(self):
3133

3234
def action_accept_offer(self):
3335
for record in self:
34-
if any([offer.status == 'accepted' for offer in record.property_id.offer_ids]):
36+
if any(offer.status == 'accepted' for offer in record.property_id.offer_ids):
3537
raise UserError("An offer for this property has already been accepted.")
3638
else:
3739
record.status = 'accepted'

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 EstatePropertyTag(models.Model):
66
_description = "Estate property tag"
77

88
name = fields.Char('Tag', required=True)
9+
10+
_check_name = models.Constraint("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 EstatePropertyType(models.Model):
66
_description = "Estate property type"
77

88
name = fields.Char('Type', required=True)
9+
10+
_check_name = models.Constraint("UNIQUE(name)", "Property type name must be unique.")

0 commit comments

Comments
 (0)