Skip to content

Commit 2afca8f

Browse files
committed
[IMP] estate: add constraints for positive prices and unique names in property offers, tags, and types
1 parent b0836be commit 2afca8f

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

estate/models/estate_property.py

Lines changed: 23 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 import float_compare
34

45

56
class EstateProperty(models.Model):
@@ -58,6 +59,10 @@ class EstateProperty(models.Model):
5859
total_area = fields.Integer('Total Area (sqm)', compute='_compute_total_area')
5960
best_price = fields.Float('Best Offer', compute='_compute_best_price')
6061

62+
_check_expected_price = models.Constraint(
63+
'CHECK(expected_price > 0)', 'The expected price must be strictly positive.'
64+
)
65+
6166
@api.depends('garden_area', 'living_area')
6267
def _compute_total_area(self):
6368
for property in self:
@@ -80,6 +85,23 @@ def _onchange_garden(self):
8085
self.garden_area = 0
8186
self.garden_orientation = False
8287

88+
@api.constrains('selling_price', 'expected_price')
89+
def _check_selling_price(self):
90+
for property in self:
91+
if (
92+
property.state == 'offer_accepted'
93+
and float_compare(property.selling_price, 0, 2) == -1
94+
):
95+
raise ValidationError('The selling price must be positive.')
96+
97+
if (
98+
float_compare(property.selling_price, property.expected_price * 0.9, 2)
99+
== -1
100+
):
101+
raise ValidationError(
102+
'Selling cannot be less than 90% of the expected price.'
103+
)
104+
83105
def action_set_property_as_sold(self):
84106
for property in self:
85107
if property.state == 'canceled':

estate/models/estate_property_offer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ class EstatePropertyOffer(models.Model):
1717
copy=False,
1818
)
1919
partner_id = fields.Many2one('res.partner', string='Partner', required=True)
20-
property_id = fields.Many2one('estate.property', string='Property', required=True)
20+
property_id = fields.Many2one(
21+
'estate.property', string='Property', required=True, ondelete='cascade'
22+
)
2123
validity = fields.Integer('Validity (days)', default=7)
2224
date_deadline = fields.Date(
2325
'Deadline', compute='_compute_date_deadline', inverse='_inverse_date_deadline'
2426
)
2527

28+
_check_price = models.Constraint(
29+
'CHECK(price > 0)', 'The price must be strictly positive.'
30+
)
31+
2632
@api.depends('validity')
2733
def _compute_date_deadline(self):
2834
for offer in self:

estate/models/estate_property_tag.py

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

88
name = fields.Char('Tag', required=True)
9+
10+
_name_uniq = models.Constraint(
11+
'unique(name)', 'A tag with the same name already exists.'
12+
)

estate/models/estate_property_type.py

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

88
name = fields.Char('Type', required=True)
9+
10+
_name_uniq = models.Constraint(
11+
'unique(name)', 'A type with the same name already exists.'
12+
)

0 commit comments

Comments
 (0)