Skip to content

Commit 12b8775

Browse files
committed
[IMP] estate: add constraints in model
add SQL and Python both type constraints in database
1 parent 155c627 commit 12b8775

File tree

6 files changed

+48
-26
lines changed

6 files changed

+48
-26
lines changed

estate/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from . import models
1+
from . import models

estate/__manifest__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
2+
'author': 'Odoo S.A.',
23
'name': 'Estate',
34
'depends': ['base'],
4-
'license': 'AGPL-3',
5+
'license': 'LGPL-3',
56
'data': [
67
'security/ir.model.access.csv',
78
'views/estate_property_views.xml',
8-
'views/estate_menus.xml',
99
'views/estate_property_type_views.xml',
1010
'views/estate_property_tag_views.xml',
11-
'views/estate_property_offer_views.xml'
11+
'views/estate_property_offer_views.xml',
12+
'views/estate_menus.xml'
1213
],
1314
'application': True,
1415
'installable': True

estate/models/estate_property.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from odoo import models, fields, api
22
from dateutil.relativedelta import relativedelta
3-
from odoo.exceptions import UserError
3+
from odoo.exceptions import UserError, ValidationError
4+
from odoo.tools.float_utils import float_compare
45

56

67
class EstateProperty(models.Model):
@@ -22,8 +23,8 @@ class EstateProperty(models.Model):
2223
garden_orientation = fields.Selection(
2324
string="Garden Orientation",
2425
selection=[('north', 'North'),
25-
('south', 'South'),
26-
('east', 'East'),
26+
('south', 'South'),
27+
('east', 'East'),
2728
('west', 'West')
2829
]
2930
)
@@ -42,14 +43,14 @@ class EstateProperty(models.Model):
4243
salesperson = fields.Many2one("res.users", default=lambda self: self.env.user)
4344
tag_ids = fields.Many2many("estate.property.tag")
4445
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers")
45-
4646
total_area = fields.Float(compute="_compute_total_area")
47+
best_offer = fields.Float(compute="_compute_best_offer")
48+
4749
@api.depends('living_area', 'garden_area')
4850
def _compute_total_area(self):
4951
for record in self:
5052
record.total_area = record.living_area + record.garden_area
5153

52-
best_offer = fields.Float(compute="_compute_best_offer")
5354
@api.depends('offer_ids.price')
5455
def _compute_best_offer(self):
5556
for record in self:
@@ -79,5 +80,16 @@ def sold_property(self):
7980
if record.state == 'cancelled':
8081
raise UserError("cancelled property cannot be sold.")
8182
else:
82-
record.state = 'sold'
83-
83+
record.state = 'sold'
84+
85+
_sql_constraints = [
86+
('check_expected_price', 'CHECK(expected_price>=0)', 'expected price must be positive'),
87+
('check_selling_price', 'CHECK(selling_price>=0)', 'selling price must be positive')
88+
]
89+
90+
@api.constrains('selling_price', 'expected_price', 'state')
91+
def _check_selling_price(self):
92+
for record in self:
93+
if float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0 and (record.state == 'sold' or record.state == 'offer_accepted'):
94+
raise ValidationError("the selling price is lower")
95+

estate/models/estate_property_offer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from odoo import models,fields, api
1+
from odoo import models, fields, api
22
from dateutil.relativedelta import relativedelta
3-
from odoo.exceptions import UserError
3+
from odoo.exceptions import UserError, ValidationError
44

55

66
class EstatePropertyOffer(models.Model):
7-
_name='estate.property.offer'
8-
_description="Estate Property Offer"
7+
_name = 'estate.property.offer'
8+
_description = "Estate Property Offer"
99

10-
price=fields.Float()
11-
status=fields.Selection(
10+
price = fields.Float()
11+
status = fields.Selection(
1212
selection=[
1313
('accepted', 'Accepted'),
1414
('refused', 'Refused')
@@ -30,7 +30,7 @@ def _compute_deadline(self):
3030
def _inverse_deadline(self):
3131
for record in self:
3232
if record.create_date:
33-
record.validity = (record.date_deadline - fields.Date.to_date(record.create_date)).days
33+
record.validity = (record.date_deadline - fields.Date.to_date(record.create_date)).days
3434
else:
3535
record.validity = 0
3636

@@ -46,5 +46,7 @@ def action_accept(self):
4646

4747
def action_refuse(self):
4848
for record in self:
49-
record.status = 'refused'
50-
49+
if record.status == 'accepted':
50+
raise UserError("Property already accepted")
51+
else:
52+
record.status = 'refused'

estate/models/estate_property_tag.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33

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

8-
name=fields.Char()
8+
name = fields.Char()
99
property_ids = fields.Many2many('estate.property')
10-
10+
11+
_sql_constraints = [
12+
('unique_name', 'UNIQUE(name)', 'name already exists!')
13+
]

estate/models/estate_property_type.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ class EstatePropertyType(models.Model):
55
_name = 'estate.property.type'
66
_description = "Real Estate Property Type"
77

8-
name = fields.Char(required=True)
9-
property_ids = fields.One2many('estate.property','property_type_id')
8+
name = fields.Char(required=True)
9+
property_ids = fields.One2many('estate.property', 'property_type_id')
10+
11+
_sql_constraints = [
12+
('unique_name', 'UNIQUE(name)', 'name already exists!'),
13+
]

0 commit comments

Comments
 (0)