Skip to content

Commit 6a68bec

Browse files
committed
[IMP] estate, estate_account: correct typo and refactor according to the review
Add a lambda fonction to the default field computing a date based on fields.Date.today method. Add a line at the end of file for files where it was missing. Adapted the create method of the EstatePropertyOffer to match the api.model_create_multi decorator. Add a call to the self.env._ method at each UserError callto export them for translation. Updated class name according to the guideline to match their model name in CamelCase format. Updated 'offer received' and 'offer accepted' to snake_case formatting. Updated misleading _sql_constraints name and error from estate_property_type. Switch to default return value instead of a workaround for max method in estate/models/estate_property.py . Updated action_confirm to: 1) right algorithm 2) better readability using write and filtered method Corrected some string field in xml. Uncommented the core of a stat_button that was only displaying 'test'.
1 parent fb559d6 commit 6a68bec

12 files changed

+56
-57
lines changed

estate/models/estate_property.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from odoo.tools.float_utils import float_compare
66

77

8-
class Properties(models.Model):
8+
class EstateProperty(models.Model):
99
_name = "estate.property"
1010
_description = "Properties of the estate model"
1111
_order = "id desc"
@@ -17,7 +17,7 @@ class Properties(models.Model):
1717
date_availability = fields.Date(
1818
"Date availability",
1919
copy=False,
20-
default=fields.Date.today() + relativedelta(months=3))
20+
default=lambda S: fields.Date.today() + relativedelta(months=3))
2121
expected_price = fields.Float("Expected price", required=True)
2222
selling_price = fields.Float("Selling price", readonly=True, copy=False)
2323
bedrooms = fields.Integer("# Bedrooms", default=2)
@@ -32,8 +32,8 @@ class Properties(models.Model):
3232
state = fields.Selection(
3333
selection=[
3434
("new", "New"),
35-
("offer received", "Offer Received"),
36-
("offer accepted", "Offer Accepted"),
35+
("offer_received", "Offer Received"),
36+
("offer_accepted", "Offer Accepted"),
3737
("sold", "Sold"),
3838
("cancelled", "Cancelled")],
3939
required=True,
@@ -60,7 +60,7 @@ def _compute_area(self):
6060
@api.depends("offer_ids")
6161
def _find_best_price(self):
6262
for record in self:
63-
record.best_price = max(record.offer_ids.mapped("price") + [0])
63+
record.best_price = max(record.offer_ids.mapped("price"), default=0)
6464

6565
@api.onchange("garden")
6666
def _onchange_garden(self):
@@ -76,15 +76,15 @@ def action_sold(self):
7676
if a_property.state not in ["cancelled"]:
7777
a_property.state = "sold"
7878
else:
79-
raise UserError("Property is already cancelled")
79+
raise UserError(self.env._("Property is already cancelled"))
8080
return True
8181

8282
def action_cancel(self):
8383
for a_property in self:
8484
if a_property.state not in ["sold"]:
8585
a_property.state = "cancelled"
8686
else:
87-
raise UserError("Property is already sold")
87+
raise UserError(self.env._("Property is already sold"))
8888
return True
8989

9090
@api.constrains("selling_price")
@@ -96,4 +96,4 @@ def _check_minimum_sell_price(self):
9696
@api.ondelete(at_uninstall=False)
9797
def _unlink_if_new_or_cancelled(self):
9898
if any(record.state not in ["new", "cancelled"] for record in self):
99-
raise UserError("Property should be New or Cancelled in order to be deleted")
99+
raise UserError(self.env._("Property should be New or Cancelled in order to be deleted"))

estate/models/estate_property_offer.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PropertyOffer(models.Model):
1212
status = fields.Selection(copy=False, selection=[('accepted', 'Accepted'), ('refused', 'Refused')])
1313
partner_id = fields.Many2one("res.partner", required=True)
1414
property_id = fields.Many2one("estate.property", required=True)
15-
date_deadline = fields.Date(default=fields.Date.today() + relativedelta(days=7), compute="_compute_deadline", inverse="_update_validity")
15+
date_deadline = fields.Date(default=lambda s:fields.Date.today() + relativedelta(days=7), compute="_compute_deadline", inverse="_update_validity")
1616
validity = fields.Integer(default=7)
1717
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)
1818

@@ -23,45 +23,43 @@ class PropertyOffer(models.Model):
2323
@api.depends("validity")
2424
def _compute_deadline(self):
2525
for record in self:
26-
if record.create_date:
27-
now = record.create_date
28-
record.date_deadline = now + relativedelta(days=record.validity)
26+
now = record.create_date
27+
record.date_deadline = now + relativedelta(days=record.validity)
2928

3029
def _update_validity(self):
3130
for offer in self:
32-
if offer.create_date:
33-
offer.validity = (offer.date_deadline - offer.create_date.date()).days
31+
offer.validity = (offer.date_deadline - offer.create_date.date()).days
3432

3533
def action_confirm(self):
3634
for offer in self:
37-
38-
for other_offer in offer.property_id.offer_ids:
39-
if other_offer.status == "accepted":
40-
offer.status = "refused"
41-
42-
if offer.status == "refused":
43-
raise UserError("Offer already refused")
44-
45-
offer.status = "accepted"
46-
offer.property_id.buyer = offer.partner_id
47-
offer.property_id.selling_price = offer.price
48-
for other_offer in offer.property_id.mapped("offer_ids"):
49-
if other_offer.id != offer.id:
50-
other_offer.status = "refused"
35+
if offer.status == 'refused':
36+
raise UserError("You cannot accept an offer that is already refused.")
37+
other_offers = offer.property_id.offer_ids.filtered(lambda o: o.id != offer.id and o.status != 'refused')
38+
other_offers.write({'status': 'refused'})
39+
offer.write({'status': 'accepted'})
40+
offer.property_id.write({
41+
'buyer': offer.partner_id,
42+
'selling_price': offer.price,
43+
})
5144
return True
5245

5346
def action_cancel(self):
5447
for offer in self:
5548
if offer.status == "accepted":
56-
raise UserError("Offer already accepted")
49+
raise UserError(self.env._("Offer already accepted"))
5750
offer.status = "refused"
5851
return True
5952

60-
@api.model
61-
def create_multi(self, vals):
62-
property_id = self.env["estate.property"].browse(vals["property_id"])
63-
if property_id.state == "new":
64-
property_id.state = "offer received"
65-
if property_id.best_price > vals["price"]:
66-
raise UserError("The price of a new offer can't be below the price of an already existing offer.")
67-
return super().create_multi(vals)
53+
@api.model_create_multi
54+
def create(self, vals_list):
55+
properties = self.env['estate.property'].browse([vals['property_id'] for vals in vals_list])
56+
property_map = {prop.id: prop for prop in properties}
57+
for vals in vals_list:
58+
prop = property_map.get(vals['property_id'])
59+
if not prop:
60+
raise UserError("Invalid property_id in offer.")
61+
if prop.best_price and vals['price'] < prop.best_price:
62+
raise UserError("The price of a new offer can't be below the price of an already existing offer.")
63+
new_properties = properties.filtered(lambda p: p.state == 'new')
64+
new_properties.write({'state': 'offer_received'})
65+
return super().create(vals_list)

estate/models/estate_property_tag.py

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

33

4-
class PropertyTags(models.Model):
4+
class PropertyTag(models.Model):
55
_name = "estate.property.tag"
66
_description = "Tags used to describe a property"
77
_order = "name desc"

estate/models/estate_property_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PropertyType(models.Model):
1212
offer_ids = fields.One2many("estate.property.offer", "property_type_id")
1313
offer_count = fields.Integer(compute="_count_offer")
1414

15-
_sql_constraints = [("unique_property_tag", "UNIQUE(name)", "Tag name must be unique !")]
15+
_sql_constraints = [("unique_property_type", "UNIQUE(name)", "Type name must be unique !")]
1616

1717
@api.depends("offer_ids")
1818
def _count_offer(self):

estate/security/ir.model.access.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
22
estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1
33
estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1
44
estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1
5-
estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1
5+
estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1

estate/views/estate_menus.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
<menuitem id="estate_tag_menu_action" action="estate_property_tag_action"/>
1010
</menuitem>
1111
</menuitem>
12-
</odoo>
12+
</odoo>

estate/views/estate_property_offer_views.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<field name="name">estate.property.offer.list</field>
2020
<field name="model">estate.property.offer</field>
2121
<field name="arch" type="xml">
22-
<list string="Channel">
22+
<list>
2323
<field name="price"/>
2424
<field name="partner_id"/>
2525
<field name="property_id"/>
@@ -33,7 +33,7 @@
3333
<field name="name">estate.property.offer.form</field>
3434
<field name="model">estate.property.offer</field>
3535
<field name="arch" type="xml">
36-
<form string="Property">
36+
<form string="Property Offer">
3737
<sheet>
3838
<group>
3939
<field name="status"/>

estate/views/estate_property_tag_views.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<field name="name">estate.property.tag.list</field>
2323
<field name="model">estate.property.tag</field>
2424
<field name="arch" type="xml">
25-
<list string="Channel" editable="bottom">
25+
<list editable="bottom">
2626
<field name="name"/>
2727
</list>
2828
</field>
@@ -33,7 +33,7 @@
3333
<field name="name">estate.property.tag.form</field>
3434
<field name="model">estate.property.tag</field>
3535
<field name="arch" type="xml">
36-
<form string="Property">
36+
<form string="Property Tag">
3737
<sheet>
3838
<h1><field name="name"/></h1>
3939
</sheet>

estate/views/estate_property_type_views.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<field name="name">estate.property.type.list</field>
2323
<field name="model">estate.property.type</field>
2424
<field name="arch" type="xml">
25-
<list string="Channel">
25+
<list>
2626
<field name="sequence" widget="handle"/>
2727
<field name="name"/>
2828
<field name="offer_count"/>
@@ -40,15 +40,15 @@
4040
<div name="button_box">
4141
<button class="oe_stat_button"
4242
name="estate.estate_property_offer_action_filter"
43-
type="action" string="test">
44-
<!--
43+
type="action">
44+
4545
<div class="o_field_widget o_stat_info">
4646
<span class="o_stat_value d-flex gap-1">
4747
<field name="offer_count" widget="statinfo" nolabel="1" class="oe_inline"/>
4848
<field name="name" class="oe_inline"/>
4949
</span>
5050
</div>
51-
-->
51+
5252
</button>
5353
</div>
5454
<h1><field name="name"/></h1>

estate/views/estate_property_views.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<field string="Bedrooms" name="bedrooms"/>
1313
<field string="Living Area (sqm)" name="living_area" filter_domain="[('living_area', '&gt;', self)]"/>
1414
<field string="Facades" name="facades"/>
15-
<filter string="Available" name="state_availability_filter" domain="['|',('state','=', 'new'), ('state','=','offer received')]"/>
15+
<filter string="Available" name="state_availability_filter" domain="['|',('state','=', 'new'), ('state','=','offer_received')]"/>
1616
<filter string="Postcode" name="groupby_postcode" context="{'group_by':'postcode'}"/>
1717
</search>
1818
</field>
@@ -30,7 +30,7 @@
3030
<field name="name">estate.property.list</field>
3131
<field name="model">estate.property</field>
3232
<field name="arch" type="xml">
33-
<list string="Channel" decoration-success="state in ['offer received','offer accepted']" decoration-bf="state == 'offer accepted'" decoration-muted="state == 'sold'">
33+
<list decoration-success="state in ['offer_received','offer_accepted']" decoration-bf="state == 'offer_accepted'" decoration-muted="state == 'sold'">
3434
<field name="name"/>
3535
<field name="postcode"/>
3636
<field name="bedrooms"/>
@@ -58,7 +58,7 @@
5858
<header>
5959
<button name="action_sold" type="object" string="Sold" invisible="state in ['sold', 'cancelled']"/>
6060
<button name="action_cancel" type="object" string="Cancel" invisible="state in ['sold', 'cancelled']"/>
61-
<field name="state" widget="statusbar" statusbar_visible="new,offer received, offer accepted,sold"/>
61+
<field name="state" widget="statusbar" statusbar_visible="new,offer_received, offer_accepted,sold"/>
6262
</header>
6363
<sheet>
6464
<h1><field name="name"/></h1>
@@ -87,10 +87,11 @@
8787
<field name="garden_area" invisible="not garden"/>
8888
<field name="garden_orientation" invisible="not garden"/>
8989
<field name="total_area"/>
90+
<field name="state"/>
9091
</group>
9192
</page>
9293
<page string="Offers">
93-
<field name="offer_ids" readonly="state in ['offer accepted', 'sold', 'cancelled']">
94+
<field name="offer_ids" readonly="state in ['offer_accepted', 'sold', 'cancelled']">
9495
<list string="Offers" editable="bottom" decoration-danger="status == 'refused'" decoration-success="status == 'accepted'">
9596
<field name="price"/>
9697
<field name="partner_id"/>
@@ -127,13 +128,13 @@
127128
Expected Price :
128129
<field name="expected_price" string="Expected price"/>
129130
</group>
130-
<div t-if="['offer received', 'offer accepted', 'sold'].includes(record.state.raw_value)">
131+
<div t-if="['offer_received', 'offer_accepted', 'sold'].includes(record.state.raw_value)">
131132
<group>
132133
Best Offer : <field name="best_price" string="Best Price"/>
133134
</group>
134135
</div>
135136

136-
<div t-if="['offer accepted', 'sold', 'Sold'].includes(record.state.raw_value)">
137+
<div t-if="['offer_accepted', 'sold', 'Sold'].includes(record.state.raw_value)">
137138
Selling Price :
138139
<field name="selling_price"/>
139140
</div>

estate/views/res_user.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
</xpath>
1313
</field>
1414
</record>
15-
</odoo>
15+
</odoo>

estate_account/models/estate_property.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from odoo import models, Command
22

33

4-
class Property(models.Model):
4+
class EstateProperty(models.Model):
55
_inherit = "estate.property"
66

77
def action_sold(self):

0 commit comments

Comments
 (0)