Skip to content

Commit 8c4baf2

Browse files
committed
[IMP] estate: model constraints
1 parent 6bfe13f commit 8c4baf2

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

estate/models/estate_property.py

Lines changed: 13 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
34

45

56
class Property(models.Model):
@@ -41,6 +42,7 @@ class Property(models.Model):
4142
offer_ids = fields.One2many('estate.property.offer', 'property_id', string="Offers")
4243
best_price = fields.Float(compute="_compute_best_price")
4344

45+
# Functions
4446
@api.depends('living_area', 'garden_area')
4547
def _compute_total_area(self):
4648
for record in self:
@@ -76,3 +78,13 @@ def action_cancel_property(self):
7678
raise UserError("Sold properties cannot be cancelled.")
7779
record.state = 'cancelled'
7880
return True
81+
82+
# Constraints
83+
_check_expected_price = models.Constraint('CHECK (expected_price > 0)', "A property expected price must be strictly positive")
84+
_check_selling_price = models.Constraint('CHECK (selling_price >= 0)', "A property selling price must be positive")
85+
86+
@api.constrains('selling_price', 'expected_price')
87+
def _check_selling_price_percentage(self):
88+
for record in self:
89+
if record.state == 'offer accepted' and float_compare(record.selling_price, 0.9*record.expected_price, precision_digits=9) == -1:
90+
raise ValidationError("The selling price cannot be lower than 90% of the expected price")

estate/models/estate_property_offer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class PropertyOffer(models.Model):
1616
validity = fields.Integer(string="Validity (days)", default=7)
1717
date_deadline = fields.Date(string="Deadline", compute="_compute_date_deadline", inverse="_inverse_date_deadline")
1818

19+
# Functions
1920
@api.depends('create_date', 'validity')
2021
def _compute_date_deadline(self):
2122
for record in self:
@@ -34,13 +35,14 @@ def action_accept(self):
3435
raise UserError("An offer has already been accepted for this property!")
3536
record.status = 'accepted'
3637
record.property_id.state = 'offer accepted'
37-
self.property_id.buyer_id = self.partner_id
38-
self.property_id.selling_price = self.price
38+
record.property_id.buyer_id = self.partner_id
39+
record.property_id.selling_price = self.price
3940
return True
4041

4142
def action_refuse(self):
4243
for record in self:
4344
record.status = 'refused'
44-
self.property_id.buyer_id = ''
45-
self.property_id.selling_price = 0
4645
return True
46+
47+
# Constraints
48+
_check_offer_price = models.Constraint('CHECK (price > 0)', "An offer price must be strictly positive")

estate/models/estate_property_tag.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ class PropertyTag(models.Model):
66
_description = "Real Estate Property Tag"
77

88
name = fields.Char(name="Name", required=True)
9+
10+
# Constraints
11+
_check_unique_name = models.Constraint("UNIQUE(name)", "A property tag name must be unique")

estate/models/estate_property_type.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ class PropertyType(models.Model):
66
_description = "Real Estate Property Type"
77

88
name = fields.Char(name="Name", required=True)
9+
10+
# Constraints
11+
_check_unique_name = models.Constraint("UNIQUE(name)", "A property type name must be unique")

0 commit comments

Comments
 (0)