Skip to content

Commit 6a34048

Browse files
committed
[IMP] estate: add computed fields to estate.property model
In this commit, I added a couple of computed fields, like best_offer_price to the estate.property model to easily visualize the best offer and modified the model class names to follow the naming convention.
1 parent 0a1066e commit 6a34048

5 files changed

+43
-7
lines changed

estate/models/estate_property.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import datetime
22

3-
from odoo import fields, models
3+
from odoo import api, fields, models
44

55

6-
class Estate_Property(models.Model):
6+
class EstateProperty(models.Model):
77
_name = "estate.property"
88
_description = "Estate Property"
99
name = fields.Char(string="Title")
@@ -17,23 +17,55 @@ class Estate_Property(models.Model):
1717
+ datetime.timedelta(days=90), # Setting the date availability 3 months from now.
1818
copy=False,
1919
)
20+
validity_date = fields.Integer(default=7)
21+
deadline_date = fields.Date(compute="_compute_deadline_date", inverse="_compute_validity_date")
2022
expected_price = fields.Float()
2123
selling_price = fields.Float(readonly=True, copyright=False, copy=False)
2224
bedrooms = fields.Integer(default=4)
2325
living_area = fields.Integer()
26+
garden_area = fields.Integer()
27+
total_area = fields.Integer(compute="_compute_total_area")
2428
facades = fields.Integer()
2529
has_garage = fields.Boolean()
2630
has_garden = fields.Boolean()
2731
active = fields.Boolean(default=True)
28-
garden_area = fields.Integer()
2932
garden_orientation = fields.Selection(
3033
selection=[("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")]
3134
)
3235
status = fields.Selection(
33-
selection=[("new", "New"), ("offer_receieved", "Offer Recieved"), ("sold", "Sold")],
36+
selection=[("new", "New"), ("offer_receieved", "Offer Recieved"), ("sold", "Sold"), ("cancelled", "Cancelled")],
3437
readonly=True,
3538
default="new",
3639
)
3740
property_type_id = fields.Many2one("estate.property.type", string="Property type")
3841
property_tag_id = fields.Many2many("estate.property.tag", string="Property tag")
3942
offer_ids = fields.One2many("estate.property.offer", "property_id")
43+
best_offer_price = fields.Float(compute="_compute_best_offer_price")
44+
45+
@api.depends("garden_area", "living_area")
46+
def _compute_total_area(self):
47+
for record in self:
48+
record.total_area = record.living_area + record.garden_area
49+
50+
@api.depends("offer_ids.price")
51+
def _compute_best_offer_price(self):
52+
for record in self:
53+
record.best_offer_price = max(record.offer_ids.mapped("price")) if record.offer_ids else 0.0
54+
55+
@api.depends("validity_date")
56+
def _compute_deadline_date(self):
57+
for record in self:
58+
record.deadline_date = datetime.date.today() + datetime.timedelta(days=record.validity_date)
59+
60+
@api.depends("deadline_date")
61+
def _compute_validity_date(self):
62+
for record in self:
63+
record.validity_date = (record.deadline.date - datetime.date.today()).days
64+
65+
@api.onchange("has_garden")
66+
def _onchange_has_garden(self):
67+
for record in self:
68+
if record.has_garden:
69+
record.garden_orientation, record.garden_area = "north", 10
70+
else:
71+
record.garden_orientation, record.garden_area = None, None

estate/models/estate_property_offer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from odoo import fields, models
22

33

4-
class Estate_Property_Offer(models.Model):
4+
class EstatePropertyOffer(models.Model):
55
_name = "estate.property.offer"
66
_description = "Estate Property Offer"
77

estate/models/estate_property_tag.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from odoo import fields, models
22

33

4-
class Estate_Property_Tag(models.Model):
4+
class EstatePropertyTag(models.Model):
55
_name = "estate.property.tag"
66

77
_description = "Estate Property Type"

estate/models/estate_property_type.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from odoo import fields, models
22

33

4-
class Estate_Property_Type(models.Model):
4+
class EstatePropertyType(models.Model):
55
_name = "estate.property.type"
66

77
_description = "Estate Property Type"

estate/views/estate_property_actions.xml

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<field name="postcode"/>
4141
<field name="property_type_id"/>
4242
<field name="date_availability"/>
43+
<field name="deadline"/>
44+
<field name="validity"/>
4345
</group>
4446
<group>
4547
<field name="expected_price"/>
@@ -52,6 +54,8 @@
5254
<field name="description"/>
5355
<field name="bedrooms"/>
5456
<field name="living_area" string="Living Area(sqm)"/>
57+
<field name="garden_area" string="Garden Area(sqm)"/>
58+
<field name="total_area" string="Total Area(sqm)"/>
5559
<field name="facades"/>
5660
<field name="has_garage" string="Garage"/>
5761
<field name="has_garden" string="Graden"/>

0 commit comments

Comments
 (0)