Skip to content

Commit 64e758f

Browse files
committed
[IMP] estate: Chapter 10 - Constraints
1 parent aef6a96 commit 64e758f

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

estate/models/estate_property.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from odoo import api, fields, models
22
from dateutil.relativedelta import relativedelta
33
from datetime import date
4-
from odoo.exceptions import UserError
4+
from odoo.exceptions import UserError, ValidationError
5+
from odoo.tools import float_compare, float_is_zero
56

67

78
class EstatePropertyModel(models.Model):
@@ -56,6 +57,9 @@ class EstatePropertyModel(models.Model):
5657
total_area = fields.Integer(compute='_compute_total_area', string="Total Area (sqm)")
5758
best_price = fields.Float(compute='_compute_best_price', string="Best Offer")
5859

60+
_expected_price_check = models.Constraint('CHECK(expected_price > 0)', "The expected price must be strictly positive.")
61+
_selling_price_check = models.Constraint('CHECK(selling_price >= 0)', "The selling price must be positive.")
62+
5963
@api.depends('living_area', 'garden_area')
6064
def _compute_total_area(self):
6165
for record in self:
@@ -92,3 +96,10 @@ def action_cancel(self):
9296
raise UserError("Sold properties cannot be canceled.")
9397
record.state = "cancel"
9498
return True
99+
100+
@api.constrains('selling_price', 'expected_price')
101+
def _check_selling_price(self):
102+
for record in self:
103+
if not float_is_zero(record.selling_price, precision_digits=2):
104+
if float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) == -1:
105+
raise ValidationError("The selling price cannot be lower than 90% of the expected price!")

estate/models/estate_property_offer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class EstatePropertyOffer(models.Model):
1717
validity = fields.Integer(string="Validity (days)", default=7)
1818
date_deadline = fields.Date(string="Deadline", compute='_compute_date_deadline', inverse='_inverse_date_deadline')
1919

20+
_price_check = models.Constraint('CHECK(price > 0)', "The offer price must be strictly positive.")
21+
2022
@api.depends('create_date', 'validity')
2123
def _compute_date_deadline(self):
2224
for record in self:

estate/models/estate_property_tag.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33

44
class EstatePropertyTag(models.Model):
5-
_name = "estate.property.tag"
5+
_name = 'estate.property.tag'
66
_description = "Property Tag"
77

88
name = fields.Char(required=True)
9+
10+
_name_check = models.Constraint('UNIQUE(name)', "The name must be unique.")

estate/models/estate_property_type.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33

44
class EstatePropertyTypeModel(models.Model):
5-
_name = "estate.property.type"
5+
_name = 'estate.property.type'
66
_description = "Real Estate property types database"
7+
78
name = fields.Char(required=True)
9+
10+
_name_check = models.Constraint('UNIQUE(name)', "The name must be unique.")

0 commit comments

Comments
 (0)