Skip to content

Commit 9637ec0

Browse files
committed
[IMP] estate: add constraints
1 parent 5d4a3ff commit 9637ec0

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

estate/models/estate_property.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from odoo import models, fields, api, exceptions
1+
from odoo import models, fields, api
2+
from odoo.exceptions import ValidationError, UserError
3+
from odoo.tools.float_utils import float_compare, float_is_zero
24

35

46
class EstateProperty(models.Model):
@@ -35,15 +37,24 @@ class EstateProperty(models.Model):
3537
total_area = fields.Float('Total Area', compute='_compute_total_area')
3638
best_price = fields.Float('Best Offer', compute='_compute_best_price')
3739

40+
_check_positive_selling_price = models.Constraint(
41+
'CHECK(selling_price >= 0)',
42+
'The selling price must be positive.',
43+
)
44+
_check_positive_expected_price = models.Constraint(
45+
'CHECK(expected_price > 0)',
46+
'The expected price must be positive.',
47+
)
48+
3849
@api.depends('garden_area', 'living_area')
3950
def _compute_total_area(self):
4051
for record in self:
4152
record.total_area = record.garden_area + record.living_area
42-
53+
4354
@api.depends('offer_ids.price')
4455
def _compute_best_price(self):
4556
for record in self:
46-
record.best_price = max(record.offer_ids, key=lambda p: p.price).price
57+
record.best_price = max(record.offer_ids, key=lambda p: p.price, default=0).price
4758

4859
@api.onchange('garden')
4960
def _onchange_garden(self):
@@ -53,19 +64,33 @@ def _onchange_garden(self):
5364
else:
5465
self.garden_area = 10
5566
self.garden_orientation = 'north'
56-
67+
5768
def action_cancel_property(self):
5869
for record in self:
5970
if record.state == 'sold':
60-
raise exceptions.UserError('Sold properties cannot be cancelled')
71+
raise UserError('Sold properties cannot be cancelled')
6172
else:
6273
record.state = 'cancelled'
6374
return True
6475

6576
def action_sold_property(self):
6677
for record in self:
6778
if record.state == 'cancelled':
68-
raise exceptions.UserError('Cancelled properties cannot be sold')
79+
raise UserError('Cancelled properties cannot be sold')
6980
else:
7081
record.state = 'sold'
71-
return True
82+
return True
83+
84+
@api.constrains('selling_price', 'expected_price')
85+
def _check_selling_price(self):
86+
for record in self:
87+
if float_is_zero(record.selling_price, precision_rounding=0.0001):
88+
continue
89+
if float_compare(
90+
record.selling_price,
91+
record.expected_price * 0.9,
92+
precision_rounding=0.01,
93+
) == -1:
94+
raise ValidationError(
95+
"The selling price cannot be lower than 90 precent of the expected price."
96+
)

estate/models/estate_property_offer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ class EstatePropertyOffer(models.Model):
1616
validity = fields.Integer('Validity', default=7)
1717
date_deadline = fields.Datetime('Deadline', compute='_compute_date_deadline', inverse='_inverse_date_deadline')
1818

19+
_check_positive_price = models.Constraint(
20+
'CHECK(price >= 0)',
21+
'The price must be positive.',
22+
)
23+
1924
@api.depends('validity', 'create_date')
2025
def _compute_date_deadline(self):
2126
for record in self:
2227
record.date_deadline = fields.Date.add(record.create_date, days=record.validity)
23-
28+
2429
def _inverse_date_deadline(self):
2530
for record in self:
2631
record.validity = (record.date_deadline - record.create_date).days

estate/models/estate_property_tag.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ class EstatePropertyTag(models.Model):
66
_description = "Estate properties Tags"
77

88
name = fields.Char('Name', required=True, translate=True)
9+
10+
_tags_uniq = models.Constraint(
11+
'unique(name)',
12+
f"The tag name already exists",
13+
)

estate/models/estate_property_type.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ class EstatePropertyType(models.Model):
66
_description = "Estate properties Types"
77

88
name = fields.Char('Name', required=True, translate=True)
9+
10+
_types_uniq = models.Constraint(
11+
'unique(name)',
12+
f"The type name already exists",
13+
)

0 commit comments

Comments
 (0)