Skip to content

Commit 77d0bd8

Browse files
committed
[IMP] estate: chapter15 - corrected code after review
- Removed un necessary assert (for the unused imports in the __init__ files) - Renamed looping variables' names for better readability (property instead of record, and offer instead of record) - Added auto_install property to True in the estate_account link module (used to auto install dependencies when we install estate_account)
1 parent d270336 commit 77d0bd8

8 files changed

+73
-64
lines changed

estate/__manifest__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
'name': 'Estate',
3+
'summary': "Module to manage properties",
4+
'description': """
5+
This module is used to manage any type of properties and also to manage the selling pipeline for each property
6+
""",
7+
'author': 'Odoo',
8+
'version': '1.0',
39
'depends': ['base'],
410
'application': True,
511
'license': 'LGPL-3',

estate/models/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,3 @@
33
from . import estate_property_tag
44
from . import estate_property_offer
55
from . import res_users
6-
7-
assert estate_property
8-
assert estate_property_type
9-
assert estate_property_tag
10-
assert estate_property_offer
11-
assert res_users

estate/models/estate_property.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from odoo import api, models, fields
2-
from odoo.exceptions import UserError, ValidationError
31
from dateutil.relativedelta import relativedelta
42

3+
from odoo import _, api, models, fields
4+
from odoo.exceptions import UserError, ValidationError
55
from odoo.tools.float_utils import float_compare, float_is_zero
66

77

8-
def _default_date_availability():
8+
def _default_date_availability(_):
99
return fields.Date.today() + relativedelta(months=3)
1010

1111

@@ -22,7 +22,7 @@ class EstateProperty(models.Model):
2222
postcode = fields.Char()
2323
date_availability = fields.Date(
2424
copy=False,
25-
default=_default_date_availability()
25+
default=_default_date_availability
2626
)
2727
state = fields.Selection(
2828
string='Status',
@@ -70,46 +70,46 @@ def group_by_empty(self, types, domain):
7070
return types.search([]) # this is equivalent to return self.env['estate.property.type'].search([])
7171

7272
def action_set_sold(self):
73-
for record in self:
74-
if record.state == 'cancelled':
75-
raise UserError("Canceled properties cannot be set as sold.")
76-
record.state = 'sold'
73+
for property in self:
74+
if property.state == 'cancelled':
75+
raise UserError(_("Canceled properties cannot be set as sold."))
76+
property.state = 'sold'
7777
return True
7878

7979
def action_cancel(self):
80-
for record in self:
81-
if record.state == 'sold':
82-
raise UserError("Sold properties cannot be canceled.")
83-
record.state = 'cancelled'
80+
for property in self:
81+
if property.state == 'sold':
82+
raise UserError(_("Sold properties cannot be canceled."))
83+
property.state = 'cancelled'
8484
return True
8585

8686
@api.ondelete(at_uninstall=False)
8787
def _check_unlink_state(self):
88-
for record in self:
89-
if record.state not in ('new', 'cancelled'):
90-
raise UserError("You cannot delete a property that is not in 'New' or 'Cancelled' state.")
88+
for property in self:
89+
if property.state not in ('new', 'cancelled'):
90+
raise UserError(_("You cannot delete a property that is not in 'New' or 'Cancelled' state."))
9191

9292
@api.constrains('expected_price', 'selling_price')
9393
def _check_selling_price(self):
94-
for record in self:
95-
if not float_is_zero(record.expected_price, precision_digits=2):
96-
accepted_offers = record.offers_ids.filtered(lambda o: o.status == 'accepted')
94+
for property in self:
95+
if not float_is_zero(property.expected_price, precision_digits=2):
96+
accepted_offers = property.offers_ids.filtered(lambda o: o.status == 'accepted')
9797
if accepted_offers:
98-
if float_compare(record.selling_price, record.expected_price * 0.90, precision_digits=2) == -1:
98+
if float_compare(property.selling_price, property.expected_price * 0.90, precision_digits=2) == -1:
9999
raise ValidationError("Selling price cannot be lower than 90 percent of expected price")
100100

101101
@api.depends('living_area', 'garden_area')
102102
def _compute_total_area(self):
103-
for record in self:
104-
record.total_area = record.living_area + record.garden_area
103+
for property in self:
104+
property.total_area = property.living_area + property.garden_area
105105

106106
@api.depends('offers_ids.price')
107107
def _compute_best_price(self):
108-
for record in self:
109-
if record.offers_ids:
110-
record.best_price = max(record.offers_ids.mapped('price'))
108+
for property in self:
109+
if property.offers_ids:
110+
property.best_price = max(property.offers_ids.mapped('price'))
111111
else:
112-
record.best_price = 0.0
112+
property.best_price = 0.0
113113

114114
@api.onchange('garden')
115115
def _onchange_garden(self):

estate/models/estate_property_offer.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from odoo import api, models, fields
1+
from odoo import _, api, models, fields
22
from dateutil.relativedelta import relativedelta
33

44
from odoo.exceptions import UserError
@@ -30,54 +30,56 @@ class EstatePropertyOffer(models.Model):
3030
]
3131

3232
@api.model_create_multi
33-
def create(self, vals):
34-
for val in vals:
33+
def create(self, vals_list):
34+
for val in vals_list:
3535
property_id = val.get('property_id')
36-
price = val.get('price')
36+
if not property_id:
37+
continue
38+
price = val.get('price', 0)
3739

3840
prop = self.env['estate.property'].browse(property_id)
3941

4042
existing_offer_prices = prop.offers_ids.mapped('price')
4143
if existing_offer_prices:
4244
max_existing_offer = max(existing_offer_prices)
4345
if float_compare(price, max_existing_offer, precision_digits=2) < 0:
44-
raise UserError("Cannot create an offer with a price lower than an existing offer")
46+
raise UserError(_("Cannot create an offer with a price lower than an existing offer"))
4547

4648
if prop.state == 'new':
4749
prop.write({'state': 'offer_received'})
4850

49-
new_offer = super().create(vals)
51+
new_offer = super().create(vals_list)
5052
return new_offer
5153

5254
def action_accept(self):
53-
for record in self:
54-
accepted_offers = record.property_id.offers_ids.filtered(lambda o: o.status == 'accepted')
55+
for offer in self:
56+
accepted_offers = offer.property_id.offers_ids.filtered(lambda o: o.status == 'accepted')
5557
if accepted_offers:
56-
raise UserError("Another offer has already been accepted for this property.")
57-
record.status = 'accepted'
58-
record.property_id.state = 'offer_accepted'
59-
record.property_id.selling_price = record.price
60-
record.property_id.buyer_id = record.partner_id
58+
raise UserError(_("Another offer has already been accepted for this property."))
59+
offer.status = 'accepted'
60+
offer.property_id.state = 'offer_accepted'
61+
offer.property_id.selling_price = offer.price
62+
offer.property_id.buyer_id = offer.partner_id
6163
return True
6264

6365
def action_refuse(self):
64-
for record in self:
65-
record.status = 'refused'
66+
for offer in self:
67+
offer.status = 'refused'
6668
return True
6769

6870
@api.depends('create_date', 'validity')
6971
def _compute_date_deadline(self):
70-
for record in self:
71-
if record.create_date:
72-
base_date = record.create_date.date()
72+
for offer in self:
73+
if offer.create_date:
74+
base_date = offer.create_date.date()
7375
else:
7476
base_date = fields.Date.today()
75-
record.date_deadline = base_date + relativedelta(days=record.validity)
77+
offer.date_deadline = base_date + relativedelta(days=offer.validity)
7678

7779
def _inverse_date_deadline(self):
78-
for record in self:
79-
if record.create_date:
80-
base_date = record.create_date.date()
80+
for offer in self:
81+
if offer.create_date:
82+
base_date = offer.create_date.date()
8183
else:
8284
base_date = fields.Date.today()
83-
record.validity = (record.date_deadline - base_date).days
85+
offer.validity = (offer.date_deadline - base_date).days

estate/views/estate_property_views.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
<field name="facades"/>
137137
<filter name="available" string="Available" domain="[('state', 'in', ['new', 'offer_received'])]"/>
138138
<group expand="1" string="Group By">
139-
<filter string="Postcode" name="postcode" context="{'group_by':'postcode'}"/>
139+
<filter string="Postcode" name="postcode" context="{'group_by': 'postcode'}"/>
140140
</group>
141141
</search>
142142
</field>

estate/views/res_users_views.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
</field>
1414
</record>
1515
</data>
16-
</odoo>
16+
</odoo>

estate_account/__manifest__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
{
22
'name': "Real Estate Accounting Link",
3+
'summary': "Link estate and account modules for creating invoice",
4+
'description': """
5+
This module is used as a bridge between estate and account modules in order to create an invoice automatically when marking a property as sold
6+
""",
7+
'author': 'Odoo',
8+
'version': '1.0',
39
'depends': [
410
'estate',
511
'account',
612
],
713
'data': [
814
],
915
'installable': True,
16+
'auto_install': True,
1017
'application': False,
1118
'license': 'LGPL-3',
1219
}

estate_account/models/estate_property.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from odoo import models, Command
1+
from odoo import _, models, Command
22
from odoo.exceptions import UserError
33

44

@@ -11,14 +11,14 @@ def action_set_sold(self):
1111
if not self:
1212
return res
1313

14-
for record in self:
15-
if not record.buyer_id:
16-
raise UserError("Cannot create invoice: Buyer is not set for property")
14+
for property in self:
15+
if not property.buyer_id:
16+
raise UserError(_("Cannot create invoice: Buyer is not set for property"))
1717

18-
if record.selling_price <= 0:
19-
raise UserError("Cannot create invoice: Selling price must be positive for property")
18+
if property.selling_price <= 0:
19+
raise UserError(_("Cannot create invoice: Selling price must be positive for property"))
2020

21-
commission = record.selling_price * 0.06
21+
commission = property.selling_price * 0.06
2222
admin_fees = 100.00
2323

2424
invoice_line_commands = [
@@ -35,7 +35,7 @@ def action_set_sold(self):
3535
]
3636

3737
invoice_vals = {
38-
'partner_id': record.buyer_id.id,
38+
'partner_id': property.buyer_id.id,
3939
'move_type': 'out_invoice',
4040
'invoice_line_ids': invoice_line_commands,
4141
}

0 commit comments

Comments
 (0)