Skip to content

Commit 22244b9

Browse files
committed
[IMP] estate: Apply constrain to form fields
Added a constraint on the expected price and the selling price so that they can't be negative Applied a constraint on the offer's price so that it can't be negative Added a unique name check for tags and types model Chapter 10
1 parent d46ed71 commit 22244b9

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

estate/models/estate_property.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from odoo import models, fields, api, exceptions
2+
from odoo.tools.float_utils import float_compare
23

34

45
class EstateProperty(models.Model):
@@ -74,3 +75,21 @@ def set_property_sold(self):
7475
else:
7576
record.state = 'sold'
7677
return True
78+
79+
_check_expected_price = models.Constraint(
80+
'CHECK(expected_price > 0)',
81+
'The Expected price cannot be 0 or less then 0'
82+
)
83+
84+
_check_selling_price = models.Constraint(
85+
'CHECK(selling_price >= 0)',
86+
'The Selling price cannot be less then 0'
87+
)
88+
89+
@api.constrains('selling_price', 'buyer', 'expected_price')
90+
def _check_selling_price_90p(self):
91+
for record in self:
92+
if record.selling_price == 0:
93+
return False
94+
if float_compare((record.selling_price/record.expected_price) * 100, 90, precision_digits=2) < 0:
95+
raise exceptions.ValidationError('Selling Price should be 90% or more of expected price.')

estate/models/estate_property_offer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class EstatePropertyOffer(models.Model):
1616
validity = fields.Integer(default=7)
1717
date_deadline = fields.Date(compute='_compute_offer_date_deadline', inverse='_inverse_offer_date_deadline')
1818

19-
@api.depends('create_date', 'validity')
19+
@api.depends('validity')
2020
def _compute_offer_date_deadline(self):
2121
for record in self:
2222
if record.create_date:
@@ -30,8 +30,6 @@ def _inverse_offer_date_deadline(self):
3030

3131
def action_offer_accepted(self):
3232
for record in self:
33-
if record.status == 'accepted':
34-
return False
3533
if record.property_id.buyer:
3634
raise exceptions.UserError('An another offer is already accepted')
3735
record.property_id.selling_price = record.price
@@ -43,3 +41,8 @@ def action_offer_refused(self):
4341
for record in self:
4442
record.status = 'refused'
4543
return True
44+
45+
_check_offer_price = models.Constraint(
46+
'CHECK(price > 0)',
47+
'Offer Price cannot be 0 or below 0'
48+
)

estate/models/estate_property_tag.py

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

33

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

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

estate/models/estate_property_type.py

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

33

44
class EstatePropertyType(models.Model):
5-
_name = "estate.property.type"
6-
_description = "Defines the type of Real Estate Property"
5+
_name = 'estate.property.type'
6+
_description = 'Defines the type of Real Estate Property'
77

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

estate/views/estate_property_views.xml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
<field name="model">estate_property</field>
55
<field name="arch" type="xml">
66
<form string="Description">
7-
<haeder>
8-
<group col="2">
9-
<button type="object" name="cancel_property_sale" string="Cancel" />
10-
<button type="object" name="set_property_sold" string="Sold" />
11-
</group>
12-
</haeder>
7+
<header>
8+
<button type="object" name="cancel_property_sale" string="Cancel" />
9+
<button type="object" name="set_property_sold" string="Sold" />
10+
</header>
1311
<sheet>
1412
<h1>
1513
<field name="name" nolabel="True" />
@@ -100,4 +98,4 @@
10098
<field name="res_model">estate_property</field>
10199
<field name="view_mode">list,form</field>
102100
</record>
103-
</odoo>
101+
</odoo>

0 commit comments

Comments
 (0)