Skip to content

Commit 4353811

Browse files
committed
[IMP] estate: Chapter 10 - SQL and Python Constraints
- Added SQL constraints to ensure positive prices and unique names - Added Python constraints to enforce offers at least 90% of the expected price
1 parent 1d073e0 commit 4353811

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

estate/models/estate_property.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from odoo import fields, models, api
2-
from odoo.exceptions import UserError
2+
from odoo.exceptions import UserError, ValidationError
3+
from odoo.tools import float_is_zero, float_compare
34

45

56
class EstateProperty(models.Model):
@@ -73,3 +74,15 @@ def action_property_cancel(self):
7374
raise UserError("Sold properties cannot be cancelled.")
7475
record.state = "cancelled"
7576
return True
77+
78+
_positive_expected_price = models.Constraint(
79+
"CHECK(expected_price > 0)",
80+
"The expected price must be strictly positive.",
81+
)
82+
83+
@api.constrains("selling_price", "expected_price")
84+
@api.onchange("selling_price", "expected_price")
85+
def _check_ninety_percent(self):
86+
for record in self:
87+
if not float_is_zero(record.selling_price, 2) and float_compare(record.expected_price * 0.9, record.selling_price, 2) == 1:
88+
raise ValidationError("The selling price must be at least 90% of the expected price! You must reduce the expected price if you want to accept this offer.")

estate/models/estate_property_offer.py

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

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

99
price = fields.Float()
1010
status = fields.Selection(
@@ -38,9 +38,14 @@ def action_accept_offer(self):
3838

3939
def action_refuse_offer(self):
4040
for record in self:
41-
if record.property_id.state == "accepted":
41+
if record.status == "accepted":
4242
record.property_id.state = "new"
4343
record.status = "refused"
4444
record.property_id.selling_price = 0.0
4545
record.property_id.buyer_id = ""
4646
return True
47+
48+
_positive_offer_price = models.Constraint(
49+
"CHECK(price > 0)",
50+
"The offer price must be strictly positive.",
51+
)

estate/models/estate_property_tag.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

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

88
name = fields.Char(required=True)
9+
10+
_unique_property_tag_name = models.Constraint(
11+
"UNIQUE(name)",
12+
"The tag name must be unique.",
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 Property Type"
77

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

estate/views/estate_property_offer_views.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<field name="partner_id"/>
2828
<field name="validity"/>
2929
<field name="date_deadline"/>
30-
<button name="action_accept_offer" type="object" icon="fa-check"/>
31-
<button name="action_refuse_offer" type="object" icon="fa-times"/>
30+
<button name="action_accept_offer" type="object" icon="fa-check" title="Accept offer"/>
31+
<button name="action_refuse_offer" type="object" icon="fa-times" title="Refuse offer"/>
3232
<field name="status"/>
3333
</list>
3434
</field>

estate/views/estate_property_type_views.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<record id="estate_property_type_action" model="ir.actions.act_window">
44
<field name="name">Property Types</field>
55
<field name="res_model">estate.property.type</field>
6-
<field name="view_mode">list,form</field>
6+
<field name="view_mode">form</field>
77
</record>
88

99
<record id="estate_property_type_form" model="ir.ui.view">

0 commit comments

Comments
 (0)