Skip to content

Commit 5cb3109

Browse files
author
SurryaT10
committed
[IMP] Chapter 10: Constraints
Added SQL constraints for expected_price, selling_price, property_tag_name and property_type_name Added python constraint for selling_price to be >= 90% of expected_price unless it is 0.0
1 parent 073a19d commit 5cb3109

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

estate/models/estate.py

Lines changed: 19 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.float_utils import float_compare, float_is_zero
34

45

56
class Estate(models.Model):
@@ -74,3 +75,20 @@ def action_mark_cancel(self):
7475
if record.status == 'sold':
7576
raise UserError("Sold properties cannot be canceled.")
7677
record.status = 'canceled'
78+
79+
_check_expected_price = models.Constraint(
80+
'CHECK(expected_price > 0)',
81+
'The expected price must be strictly positive'
82+
)
83+
84+
_check_selling_price = models.Constraint(
85+
'CHECK(selling_price >= 0)',
86+
'The selling price must be positive'
87+
)
88+
89+
@api.constrains('selling_price')
90+
def _check_selling_price(self):
91+
for record in self:
92+
if not float_is_zero(record.selling_price, precision_digits=2):
93+
if float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) < 0:
94+
raise ValidationError('The selling price must be atleast 90% of the expected price! You must reduce the expected price to accept the offer')

estate/models/offer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ def action_reject_offer(self):
4545
record.property_id.buyer_id = False
4646
record.property_id.status = 'offer_received'
4747

48+
_check_price = models.Constraint(
49+
'CHECK(price > 0)',
50+
'The offer price must be strictly positive'
51+
)

estate/models/property_tag.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ class PropertyTag(models.Model):
66
_description = 'Estate Property Tag'
77

88
name = fields.Char("Tag Name", required=True)
9+
10+
_unique_name = models.Constraint(
11+
'UNIQUE(name)',
12+
'The property tag name must be unique'
13+
)

estate/models/property_type.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ class PropertyType(models.Model):
66
_description = 'Estate Property Type'
77

88
name = fields.Char("Property Type", required=True)
9+
10+
_unique_name = models.Constraint(
11+
'UNIQUE(name)',
12+
'The property type name must be unique'
13+
)

0 commit comments

Comments
 (0)