Skip to content

Commit 88d0079

Browse files
committed
[REF] estate: Refactor based on guidelines and feedbacks
- Model description - Error messages - Views group - Default data type - Simplify conditional check
1 parent 2e499ad commit 88d0079

File tree

7 files changed

+24
-40
lines changed

7 files changed

+24
-40
lines changed

estate/models/estate_property.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class EstateProperty(models.Model):
77
_name = 'estate.property'
8-
_description = 'All properties'
8+
_description = 'Estate Properties'
99
_order = 'id desc'
1010

1111
name = fields.Char(required=True)
@@ -79,16 +79,15 @@ def _compute_total_area(self):
7979
@api.depends('offer_ids')
8080
def _compute_best_price(self):
8181
for record in self:
82-
offers = record.offer_ids or []
83-
record.best_price = max(offers.mapped('price')) if len(offers) else 0
82+
record.best_price = max(record.offer_ids.mapped('price'), default=0.0)
8483

8584
@api.onchange('garden')
8685
def _onchange_garden(self):
8786
if self.garden:
8887
self.garden_area = 10
8988
self.garden_orientation = 'north'
9089
else:
91-
self.garden_area = None
90+
self.garden_area = 0
9291
self.garden_orientation = None
9392

9493
@api.constrains('selling_price', 'expected_price')
@@ -109,13 +108,13 @@ def _unlink_if_new_or_cancelled(self):
109108
def action_mark_as_sold(self):
110109
for record in self:
111110
if record.state == 'cancelled':
112-
raise UserError('Property is already cancelled')
111+
raise UserError('Cannot sell cancelled properties')
113112

114113
record.state = 'sold'
115114

116115
def action_mark_as_cancelled(self):
117116
for record in self:
118117
if record.state == 'sold':
119-
raise UserError('Property is already sold')
118+
raise UserError('Cannot cancel sold properties')
120119

121120
record.state = 'cancelled'

estate/models/estate_property_offer.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class EstatePropertyOffer(models.Model):
66
_name = 'estate.property.offer'
7-
_description = 'All offers'
7+
_description = 'Estate Offers'
88
_order = 'price desc'
99

1010
price = fields.Float(required=True)
@@ -18,7 +18,7 @@ class EstatePropertyOffer(models.Model):
1818
partner_id = fields.Many2one('res.partner', string='Partner')
1919
property_id = fields.Many2one('estate.property', string='Property')
2020
validity = fields.Integer(default=7)
21-
date_deadline = fields.Date(compute='_compute_deadline', inverse="_inverse_deadline")
21+
date_deadline = fields.Date(compute='_compute_date_deadline', inverse="_inverse_date_deadline")
2222
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)
2323

2424
_check_price = models.Constraint(
@@ -27,13 +27,18 @@ class EstatePropertyOffer(models.Model):
2727
)
2828

2929
@api.depends('create_date', 'validity')
30-
def _compute_deadline(self):
30+
def _compute_date_deadline(self):
3131
for record in self:
3232
created_date = record.create_date or fields.Date.today()
3333
record.date_deadline = fields.Date.add(
3434
created_date, days=record.validity,
3535
)
3636

37+
def _inverse_date_deadline(self):
38+
for record in self:
39+
created_date = record.create_date.date() or fields.Date.today()
40+
record.validity = (record.date_deadline - created_date).days
41+
3742
@api.model
3843
def create(self, val_lists):
3944
for vals in val_lists:
@@ -44,16 +49,10 @@ def create(self, val_lists):
4449
linked_property.state = 'offer_received'
4550
return super().create(val_lists)
4651

47-
def _inverse_deadline(self):
48-
for record in self:
49-
created_date = record.create_date.date() or fields.Date.today()
50-
record.validity = (record.date_deadline - created_date).days
51-
5252
def action_mark_as_accepted(self):
5353
for record in self:
5454
if (
55-
record.property_id.state != 'new'
56-
and record.property_id.state != 'offer_received'
55+
record.property_id.state not in ('new', 'offer_received')
5756
):
5857
raise UserError('Cannot accept offer in this state')
5958

@@ -66,6 +65,6 @@ def action_mark_as_refused(self):
6665
for record in self:
6766
if record.status == 'accepted':
6867
record.property_id.state = 'offer_received'
69-
record.property_id.selling_price = None
68+
record.property_id.selling_price = 0.0
7069
record.property_id.buyer_id = None
7170
record.status = 'refused'

estate/models/estate_property_tag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class EstatePropertyTag(models.Model):
55
_name = 'estate.property.tag'
6-
_description = 'All property tags'
6+
_description = 'Estate Property Tags'
77
_order = 'name asc'
88

99
name = fields.Char(required=True)

estate/models/estate_property_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class EstatePropertyType(models.Model):
55
_name = 'estate.property.type'
6-
_description = 'All property types'
6+
_description = 'Estate Property Types'
77
_order = 'name asc'
88

99
name = fields.Char(required=True)

estate/views/estate_property_offer_views.xml

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,12 @@
4545
<form string="Property Offer">
4646
<sheet>
4747
<group>
48-
<group>
49-
<field name="property_id"/>
50-
</group>
51-
<group>
52-
<field name="partner_id"/>
53-
</group>
54-
<group>
55-
<field name="price"/>
56-
</group>
57-
<group>
58-
<field name="status"/>
59-
</group>
60-
<group>
61-
<field name="validity" string="Validity (days)"/>
62-
</group>
63-
<group>
64-
<field name="date_deadline" string="Deadline"/>
65-
</group>
48+
<field name="property_id"/>
49+
<field name="partner_id"/>
50+
<field name="price"/>
51+
<field name="status"/>
52+
<field name="validity" string="Validity (days)"/>
53+
<field name="date_deadline" string="Deadline"/>
6654
</group>
6755
</sheet>
6856
</form>

estate/views/estate_property_tag_views.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
<sheet>
2525
<group>
2626
<field name="name"/>
27-
</group>
28-
<group>
2927
<field name="color" widget="color_picker"/>
3028
</group>
3129
</sheet>

estate/views/estate_property_views.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135

136136
<separator/>
137137

138-
<filter string="Available" name="available" domain="['|', ('state', '=', 'new'), ('state', '=', 'offer_received')]"/>
138+
<filter string="Available" name="available" domain="[('state', 'in', ('new', 'offer_recevied'))]"/>
139139
<filter string="Postcode" name="postcode" context="{'group_by':'postcode'}"/>
140140
</search>
141141
</field>

0 commit comments

Comments
 (0)