Skip to content

Commit d54574c

Browse files
committed
[IMP] estate: chapter 9 (actions)
1 parent 8efcb4c commit d54574c

File tree

7 files changed

+68
-7
lines changed

7 files changed

+68
-7
lines changed

estate/__manifest__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
""",
1010
'data': [
1111
'security/ir.model.access.csv',
12-
1312
'views/estate_property_views.xml',
1413
'views/estate_property_type_views.xml',
1514
'views/estate_property_tag_views.xml',

estate/models/estate_property.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Part of Odoo. See LICENSE file for full copyright and licensing details.
22

3-
from odoo import fields, models, api
4-
from datetime import timedelta
3+
from odoo import fields, models, api, exceptions
54

65

76
class EstateProperty(models.Model):
@@ -12,7 +11,7 @@ class EstateProperty(models.Model):
1211
active = fields.Boolean('Active', default=True)
1312
description = fields.Text('Description')
1413
postcode = fields.Char('Postcode')
15-
date_availability = fields.Date('Available From', copy=False, default=fields.Datetime.now() + timedelta(days=90))
14+
date_availability = fields.Date('Available From', copy=False, default=fields.Date.add(fields.Date.today(), months=3))
1615
expected_price = fields.Float('Expected Price', required=True)
1716
selling_price = fields.Float('Selling Price', readonly=True, copy=False)
1817
bedrooms = fields.Integer('Bedrooms', default=2)
@@ -44,6 +43,16 @@ class EstateProperty(models.Model):
4443
total_area = fields.Integer('Total Area (sqm)', compute='_compute_total_area')
4544
best_offer = fields.Float('Best Offer', compute='_compute_best_offer')
4645

46+
_check_expected_price = models.Constraint(
47+
'CHECK(expected_price > 0)',
48+
'The expected price must be strictly positive',
49+
)
50+
51+
_check_selling_price = models.Constraint(
52+
'CHECK(selling_price >= 0)',
53+
'The selling price must be positive',
54+
)
55+
4756
@api.depends('garden_area', 'living_area')
4857
def _compute_total_area(self):
4958
for record in self:
@@ -65,3 +74,19 @@ def _onchange_garden(self):
6574
else:
6675
self.garden_area = 0
6776
self.garden_orientation = ''
77+
78+
def property_sold(self):
79+
for record in self:
80+
if record.state != 'cancelled':
81+
record.state = 'sold'
82+
else:
83+
raise exceptions.UserError("Sold properties cannot be cancelled.")
84+
return True
85+
86+
def property_cancelled(self):
87+
for record in self:
88+
if record.state != 'sold':
89+
record.state = 'cancelled'
90+
else:
91+
raise exceptions.UserError("Cancelled properties cannot be sold.")
92+
return True

estate/models/estate_property_offer.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class EstatePropertyOffer(models.Model):
1616
validity = fields.Integer(string='Validity (days)', default=7)
1717
date_deadline = fields.Date(string='Deadline', compute='_compute_deadline', inverse='_inverse_deadline')
1818

19+
_check_price = models.Constraint(
20+
'CHECK(price > 0)',
21+
'The offer price must be strictly positive',
22+
)
23+
1924
@api.depends('validity')
2025
def _compute_deadline(self):
2126
for record in self:
@@ -24,3 +29,18 @@ def _compute_deadline(self):
2429
def _inverse_deadline(self):
2530
for record in self:
2631
record.validity = (record.date_deadline - (record.create_date.date() if record.create_date else date.today())).days
32+
33+
def action_accept(self):
34+
for record in self:
35+
if record.property_id.state in ('new', 'offer_received'):
36+
record.property_id.state = 'offer_accepted'
37+
record.status = 'accepted'
38+
record.property_id.selling_price = record.price
39+
record.property_id.buyer_id = record.partner_id
40+
return True
41+
42+
def action_refuse(self):
43+
for record in self:
44+
if not record.status:
45+
record.status = 'refused'
46+
return True

estate/models/estate_property_tag.py

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

1010
name = fields.Char(string='Property Tag', required=True)
11+
12+
_tag_name_uniq = models.Constraint(
13+
'unique(name)',
14+
"The property tag name must be unique",
15+
)

estate/models/estate_property_type.py

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

1010
name = fields.Char(string='Property Type', required=True)
11+
12+
_type_name_uniq = models.Constraint(
13+
'unique(name)',
14+
"The property type name must be unique",
15+
)

estate/views/estate_menus.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
id="properties_menu"
88
name="Properties"
99
parent="estate_menu_root"
10-
action="estate.estate_property_action"
10+
action="estate_property_action"
1111
sequence="1"/>
1212
<menuitem
1313
id="settings_menu"
@@ -18,12 +18,12 @@
1818
id="properties_type_menu"
1919
name="Property Types"
2020
parent="settings_menu"
21-
action="estate.estate_property_type_action"
21+
action="estate_property_type_action"
2222
sequence="1"/>
2323
<menuitem
2424
id="properties_tag_menu"
2525
name="Property Tags"
2626
parent="settings_menu"
27-
action="estate.estate_property_tag_action"
27+
action="estate_property_tag_action"
2828
sequence="2"/>
2929
</odoo>

estate/views/estate_property_views.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<field name="model">estate.property</field>
3232
<field name="arch" type="xml">
3333
<form string="Estate Property">
34+
<header>
35+
<button name="property_sold" type="object" string="Sold"/>
36+
<button name="property_cancelled" type="object" string="Cancel"/>
37+
</header>
3438
<sheet>
3539
<div class="oe_title">
3640
<h1>
@@ -40,6 +44,7 @@
4044
</div>
4145
<group>
4246
<group>
47+
<field name="state"/>
4348
<field name="property_type_id"/>
4449
<field name="postcode"/>
4550
<field name="date_availability"/>
@@ -72,6 +77,8 @@
7277
<field name="validity"/>
7378
<field name="date_deadline"/>
7479
<field name="status"/>
80+
<button name="action_accept" type="object" icon="fa-check"/>
81+
<button name="action_refuse" type="object" icon="fa-close"/>
7582
</list>
7683
</field>
7784
</page>

0 commit comments

Comments
 (0)