8000 [IMP] estate: Chapter 10 · odoo/tutorials@6ee2a07 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ee2a07

Browse files
committed
[IMP] estate: Chapter 10
Constraints
1 parent b517f01 commit 6ee2a07

File tree

6 files changed

+65
-22
lines changed

6 files changed

+65
-22
lines changed

estate/models/estate_property.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from odoo import fields, models, api, _
2-
from odoo.exceptions import UserError
1+
from odoo import fields, models, api, _, tools
2+
from odoo.exceptions import UserError, ValidationError
33

44

5-
class EstateModel(models.Model):
5+
class EstateProperty(models.Model):
66
_name = 'estate.property'
77
_description = 'Estate properties'
88

@@ -68,13 +68,29 @@ def action_btn_sold(self):
6868
if record.state != 'cancelled':
6969
record.state = "sold"
7070
else:
71-
raise UserError("This property is already cancelled and thus cannot be sold anymore.")
71+
raise UserError(_("This property is already cancelled and thus cannot be sold anymore."))
7272
return True
7373

7474
def action_btn_cancel(self):
7575
for record in self:
7676
if record.state != 'sold':
7777
record.state = "cancelled"
7878
else:
79-
raise UserError("This property is already sold and thus cannot be cancelled anymore.")
79+
raise UserError(_("This property is already sold and thus cannot be cancelled anymore."))
8080
return True
81+
82+
# Add a constraint so that the selling price cannot be lower than 90% of the expected price.
83+
#
84+
# Tip: the selling price is zero until an offer is validated. You will need to fine tune your check to take this into account.
85+
@api.constrains('expected_price', 'selling_price')
86+
@api.onchange("expected_price", "selling_price")
87+
def _check_offer_price(self):
88+
for record in self:
89+
if tools.float_utils.float_compare((record.expected_price * 0.9), record.selling_price, precision_rounding=0.01) >= 0 and not tools.float_utils.float_is_zero(record.selling_price, precision_rounding=0.001):
90+
raise ValidationError(_("Offer price cannot be lower than 90% of the expected price."))
91+
92+
_check_strictly_positive_expected_price = (models.Constraint("""CHECK (expected_price > 0)""",
93+
"The property expected price must be strictly positive."))
94+
95+
_check_positive_selling_price = (models.Constraint("""CHECK (selling_price >= 0)""",
96+
"The property selling price must be positive."))
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from odoo import fields, models, api
22

33

4-
class EstatePropertyOfferModel(models.Model):
4+
class EstatePropertyOffer(models.Model):
55
_name = 'estate.property.offer'
66
_description = 'Estate property offer'
77

@@ -12,27 +12,28 @@ class EstatePropertyOfferModel(models.Model):
1212
partner_id = fields.Many2one("res.partner", string="Buyer", required=True)
1313
property_id = fields.Many2one("estate.property", string="Offer", required=True)
1414
validity = fields.Integer(default=7)
15-
date_deadline = fields.Date(compute="_compute_deadline", inverse="_inverse_deadline")
15+
date_deadline = fields.Date(compute="_compute_deadline", inverse="_inverse_deadline", default=fields.Date.add(fields.Date.today(), days=7))
1616

1717
@api.depends("create_date", "validity")
1818
def _compute_deadline(self):
19-
for record in self:
20-
if record.create_date:
21-
record.date_deadline = fields.Date.add(record.create_date, days=record.validity)
19+
for record in self.filtered("create_date"):
20+
record.date_deadline = fields.Date.add(record.create_date, days=record.validity)
2221

2322
def _inverse_deadline(self):
24-
for record in self:
25-
if record.create_date:
26-
record.validity = (record.date_deadline - fields.Date.to_date(record.create_date)).days
23+
for record in self.filtered("create_date"):
24+
record.validity = (record.date_deadline - record.create_date.date()).days
2725

2826
def action_btn_accept(self):
29-
for record in self:
30-
record.status = "accepted"
31-
record.property_id.buyer_id = self.partner_id.id
32-
record.property_id.selling_price = self.price
33-
return True
27+
self.ensure_one()
28+
self.status = "accepted"
29+
self.property_id.buyer_id = self.partner_id.id
30+
self.property_id.selling_price = self.price
31+
self.property_id.state = 'offer_accepted'
3432

3533
def action_btn_refuse(self):
3634
for record in self:
3735
record.status = "refused"
3836
return True
37+
38+
_check_positive_offer_price = (models.Constraint("""CHECK (price >= 0)""",
39+
"The property offer price must be positive."))

estate/models/estate_property_tag.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from odoo import fields, models
22

33

4-
class EstatePropertyTagModel(models.Model):
4+
class EstatePropertyTag(models.Model):
55
_name = 'estate.property.tag'
66
_description = 'Estate property tag'
77

88
name = fields.Char('Title', required=True, translate=True)
9+
10+
_tag_name_uniq = (models.Constraint("""UNIQUE (name)""",
11+
"The tag name must be unique."))

estate/models/estate_property_type.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from odoo import fields, models
22

33

4-
class EstatePropertyTypeModel(models.Model):
4+
class EstatePropertyType(models.Model):
55
_name = 'estate.property.type'
66
_description = 'Estate property type'
77

88
name = fields.Char('Title', required=True, translate=True)
9+
10+
_type_name_uniq = (models.Constraint("""UNIQUE (name)""",
11+
"The type name must be unique."))

estate/views/estate_property_tag_views.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
<record id="estate_property_tag_model_action" model="ir.actions.act_window">
44
<field name="name">Property Tags</field>
55
<field name="res_model">estate.property.tag</field>
6-
<field name="view_mode">form</field>
6+
<field name="view_mode">form, list</field>
7+
</record>
8+
9+
<record id="estate_property_tag_list_view" model="ir.ui.view">
10+
<field name="name">estate.property.tag.list</field>
11+
<field name="model">estate.property.tag</field>
12+
<field name="arch" type="xml">
13+
<list string="Channel" editable="bottom">
14+
<field name="name"/>
15+
</list>
16+
</field>
717
</record>
818

919
<record id="estate_property_tag_form_view" model="ir.ui.view">

estate/views/estate_property_type_views.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@
33
<record id="estate_property_type_model_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">form</field>
6+
<field name="view_mode">form, list</field>
7+
</record>
8+
9+
<record id="estate_property_tag_list_view" model="ir.ui.view">
10+
<field name="name">estate.property.tag.list</field>
11+
<field name="model">estate.property.tag</field>
12+
<field name="arch" type="xml">
13+
<list string="Channel" editable="bottom">
14+
<field name="name"/>
15+
</list>
16+
</field>
717
</record>
818

919
<record id="estate_property_type_form_view" model="ir.ui.view">

0 commit comments

Comments
 (0)
0