Skip to content

Commit 422a131

Browse files
author
karygauss03
committed
[IMP] Estate: Adding SQL and Python constraints to estate related models.
In this commit, I added the sql and python constraints for the prices of the estate and the offers. This commit is related to the 10th chapter of the Server Framework 101 Odoo's documentation.
1 parent 531b039 commit 422a131

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

estate/models/estate_property.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from dateutil.relativedelta import relativedelta
22
from odoo import api, fields, models
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
_name = "estate.property"
89
_description = "Real Estate"
10+
_sql_constraints = [
11+
('check_expected_price', 'CHECK(expected_price > 0)', 'Expected price must be strictly positve'),
12+
('check_selling_price', 'CHECK(selling_price >= 0)', 'Selling price must be positive')
13+
]
914

1015
name = fields.Char('Name', required=True)
1116
description = fields.Char('Description')
@@ -41,7 +46,8 @@ class EstateProperty(models.Model):
4146
tag_ids = fields.Many2many('estate.property.tag', string='Tags')
4247
offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers')
4348

44-
@api.depends('living_area', 'garden_area')
49+
50+
@api.depends('living_area','garden_area')
4551
def _compute_total_area(self):
4652
for record in self:
4753
record.total_area = record.living_area + record.garden_area
@@ -57,12 +63,20 @@ def _onchange_garden_checkbox(self):
5763
self.garden_area = 10 if self.garden else 0
5864
self.garden_orientation = 'north' if self.garden else None
5965

66+
@api.constrains('selling_price','expected_price')
67+
def _check_selling_price(self):
68+
for record in self:
69+
if float_is_zero(record.selling_price, precision_digits=2):
70+
continue
71+
72+
if float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) < 0:
73+
raise ValidationError("Selling price cannot be lower than 90% of the expected price.")
74+
6075
def action_set_sold(self):
6176
for record in self:
6277
if (record.state == 'sold'):
6378
raise UserError('Property Already Sold')
6479
if record.state != 'canceled':
65-
record.active = False
6680
record.state = 'sold'
6781
else:
6882
raise UserError("Cannot sell a canceled property")
@@ -74,6 +88,5 @@ def action_set_cancel(self):
7488
raise UserError('Cannot cancel a sold property')
7589
if (record.state == 'canceled'):
7690
raise UserError("Property Already canceled")
77-
record.active = False
7891
record.state = 'canceled'
7992
return True

estate/models/estate_property_offer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
class EstatePropertyOffer(models.Model):
88
_name = "estate.property.offer"
99
_description = "Estate Property Offer"
10+
_sql_constraints = [
11+
('check_offer_price', 'CHECK(price >= 0)', 'Offer price must be strictly positive')
12+
]
1013

1114
price = fields.Float(string="Price")
1215
status = fields.Selection(
@@ -19,6 +22,7 @@ class EstatePropertyOffer(models.Model):
1922
partner_id = fields.Many2one('res.partner', required=True, string="Partner")
2023
property_id = fields.Many2one('estate.property', required=True, string="Property")
2124

25+
2226
@api.depends('create_date', 'validity')
2327
def _compute_date_deadline(self):
2428
for offer in self:

estate/models/estate_property_tag.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
from odoo import models, fields
1+
from odoo import fields, models
22

33

44
class EstatePropertyTag(models.Model):
55
_name = 'estate.property.tag'
66
_description = 'Estate Property Tags'
7+
_sql_constraints = [
8+
('check_name_unique', 'UNIQUE(name)', "Tag name must be unique")
9+
]
710

811
name = fields.Char(string='Name', required=True)

0 commit comments

Comments
 (0)