Skip to content

Commit f18326f

Browse files
committed
[IMP] estate: chapter 10: done
1 parent 0d55383 commit f18326f

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

estate/models/estate.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Part of Odoo. See LICENSE file for full copyright and licensing details.
22

33
from odoo import api, fields, models
4+
from odoo.exceptions import ValidationError
5+
from odoo.tools.float_utils import float_compare
46
from dateutil.relativedelta import relativedelta
57

68

@@ -54,6 +56,19 @@ class Estate(models.Model):
5456
total_area = fields.Float(compute="_compute_total_area")
5557
best_price = fields.Float(compute="_compute_best_price")
5658

59+
_sql_constraints = [
60+
(
61+
"check_expected_price",
62+
"CHECK(expected_price > 0)",
63+
"Expected price must be positive.",
64+
),
65+
(
66+
"check_selling_price",
67+
"CHECK(selling_price > -1)",
68+
"Selling price must be positive.",
69+
),
70+
]
71+
5772
@api.depends("garden_area", "living_area")
5873
def _compute_total_area(self):
5974
for record in self:
@@ -62,6 +77,9 @@ def _compute_total_area(self):
6277
@api.depends("property_offer_ids")
6378
def _compute_best_price(self):
6479
for record in self:
80+
if not record.property_offer_ids:
81+
record.best_price = 0
82+
continue
6583
record.best_price = max(record.mapped("property_offer_ids.price"))
6684

6785
@api.onchange("garden")
@@ -70,8 +88,8 @@ def _onchange_garden(self):
7088
self.garden_area = 10
7189
self.garden_orientation = "north"
7290
else:
73-
self.garden_area = 0 # Or None if you prefer
74-
self.garden_orientation = False # Or None if you prefer
91+
self.garden_area = 0
92+
self.garden_orientation = False
7593

7694
def action_sell_property(self):
7795
for record in self:
@@ -80,3 +98,14 @@ def action_sell_property(self):
8098
def action_cancel_property(self):
8199
for record in self:
82100
record.state = "cancelled"
101+
102+
@api.constrains("selling_price")
103+
def _check_selling_price(self):
104+
for record in self:
105+
if not record.selling_price:
106+
continue
107+
108+
if float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits = 2) == -1:
109+
raise ValidationError(
110+
"The selling price can not be less than 90 percent of the expected price"
111+
)

estate/models/property_offer.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
from odoo import api, fields, models
33
from dateutil.relativedelta import relativedelta
44

5-
65
class PropertyOffer(models.Model):
76
_name = "property_offer"
87
_description = "Property Offer Model"
9-
name = fields.Char(required=True)
108
price = fields.Float()
119
validity = fields.Integer(default=7)
1210
date_deadline = fields.Date(
@@ -22,6 +20,10 @@ class PropertyOffer(models.Model):
2220
partner_id = fields.Many2one("res.partner", required=True)
2321
property_id = fields.Many2one("estate_property", required=True)
2422

23+
_sql_constraints = [
24+
("check_price", "CHECK(price > 0)", "The offer price must be positive.")
25+
]
26+
2527
@api.depends("validity", "create_date")
2628
def _compute_date_deadine(self):
2729
for record in self:
@@ -49,4 +51,4 @@ def action_refuse_offer(self):
4951
record.status = "refused"
5052
if record.property_id.selling_price == record.price:
5153
record.property_id.selling_price = 0
52-
54+

estate/models/property_tag.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ class PropertyTag(models.Model):
66
_name = "property_tag"
77
_description = "Property Tag Model"
88
name = fields.Char(required=True)
9+
_sql_constraints = [
10+
(
11+
"unique_tag_name",
12+
"UNIQUE(name)",
13+
"Property tag must be unique.",
14+
),
15+
]

estate/models/propery_type.py

+8
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ class PropertyType(models.Model):
77
_name = "property_type"
88
_description = "Property Types"
99
name = fields.Char(required=True)
10+
11+
_sql_constraints = [
12+
(
13+
"unique_type_name",
14+
"UNIQUE(name)",
15+
"Property type must be unique.",
16+
),
17+
]

estate/views/property_offer_views.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<field name="status" />
1313
<field name="date_deadline"/>
1414
<field name="validity"/>
15-
<button name="action_accept_offer" type="object" icon="fa-check"/>
16-
<button name="action_refuse_offer" type="object" icon="fa-times"/>
15+
<button name="action_accept_offer" type="object" icon="fa-check" string ="accept"/>
16+
<button name="action_refuse_offer" type="object" icon="fa-times" string = "refuse"/>
1717
</list>
1818
</field>
1919
</record>

0 commit comments

Comments
 (0)