Skip to content

Commit 48d5ad6

Browse files
committed
[CLN] estate: Chapter 15
1 parent a3bdf2f commit 48d5ad6

14 files changed

+66
-63
lines changed

estate/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'views/estate_property_type_views.xml',
1212
'views/estate_property_tag_views.xml',
1313
'views/estate_res_user_views.xml',
14-
'views/estate_menus.xml',
14+
'views/estate_menu_views.xml',
1515
],
1616
'application': True,
1717
'author': 'Odoo S.A.',

estate/models/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from . import estate_properties
2-
from . import estate_property_types
3-
from . import estate_property_tags
4-
from . import estate_property_offers
1+
from . import estate_property
2+
from . import estate_property_type
3+
from . import estate_property_tag
4+
from . import estate_property_offer
55
from . import res_user

estate/models/estate_properties.py renamed to estate/models/estate_property.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from odoo import api, fields, models
1+
from odoo import api, fields, models, _
22
from odoo.exceptions import UserError, ValidationError
33
from odoo.tools import float_compare
44

@@ -10,21 +10,13 @@ class PropertyModel(models.Model):
1010
_name = "estate.property"
1111
_description = "Estate Property model"
1212
_order = "id desc"
13-
_check_positive_expected_price = models.Constraint(
14-
"CHECK(expected_price >= 0)",
15-
"The expected price must be positive."
16-
)
17-
_check_positive_selling_price = models.Constraint(
18-
"CHECK(selling_price >= 0)",
19-
"The selling price must be positive"
20-
)
2113

2214
name = fields.Char("Title", required=True)
2315
description = fields.Text()
2416
postcode = fields.Char()
2517
date_availability = fields.Date(default=fields.Date.add(fields.Date.today(), months=3), copy=False)
2618
expected_price = fields.Float(required=True)
27-
best_offer = fields.Float(compute="_get_highest_price")
19+
best_offer = fields.Float(compute="_compute_highest_price")
2820
selling_price = fields.Float(readonly=True, copy=False)
2921
bedrooms = fields.Integer(default=2)
3022
living_area = fields.Integer("Living Area (sqm)")
@@ -57,13 +49,22 @@ class PropertyModel(models.Model):
5749
tag_ids = fields.Many2many("estate.property.tag")
5850
offer_ids = fields.One2many("estate.property.offer", "property_id")
5951

52+
_check_positive_expected_price = models.Constraint(
53+
"CHECK(expected_price >= 0)",
54+
"The expected price must be positive."
55+
)
56+
_check_positive_selling_price = models.Constraint(
57+
"CHECK(selling_price >= 0)",
58+
"The selling price must be positive"
59+
)
60+
6061
@api.depends("living_area", "garden_area")
6162
def _compute_total_area(self):
6263
for record in self:
6364
record.total_living_area = record.living_area + record.garden_area
6465

6566
@api.depends("offer_ids")
66-
def _get_highest_price(self):
67+
def _compute_highest_price(self):
6768
for record in self:
6869
record.best_offer = max(record.offer_ids.mapped("price")) if record.offer_ids else 0
6970

@@ -76,27 +77,27 @@ def _update_garden_area_and_orientation(self):
7677
self.garden_area = 0
7778
self.garden_orientation = None
7879

79-
def mark_as_sold(self):
80+
@api.constrains("selling_price", "expected_price")
81+
def _check_selling_price(self):
82+
for record in self:
83+
if record.selling_price and float_compare(record.selling_price, record.expected_price * .9, 0) == -1:
84+
raise ValidationError(r"The selling price cannot be lower than 90% of the expected price.")
85+
86+
@api.ondelete(at_uninstall=False)
87+
def _unlink_if_new_or_cancelled(self):
88+
if any(record.state not in ('new', 'cancelled') for record in self):
89+
raise UserError(_("Only 'New' and 'Cancelled' properties can be deleted."))
90+
91+
def action_mark_as_sold(self):
8092
self.ensure_one()
8193
if self.state == "cancelled":
8294
raise UserError("A cancelled property cannot be set as sold.")
8395
self.state = "sold"
8496
return True
8597

86-
def mark_as_cancelled(self):
98+
def action_mark_as_cancelled(self):
8799
self.ensure_one()
88100
if self.state == "sold":
89-
raise UserError("A sold property cannot be set as cancelled.")
101+
raise UserError(_("A sold property cannot be set as cancelled."))
90102
self.state = "cancelled"
91103
return True
92-
93-
@api.constrains("selling_price", "expected_price")
94-
def _check_selling_price(self):
95-
for record in self:
96-
if record.selling_price and float_compare(record.selling_price, record.expected_price * .9, 0) == -1:
97-
raise ValidationError(r"The selling price cannot be lower than 90% of the expected price.")
98-
99-
@api.ondelete(at_uninstall=False)
100-
def _unlink_if_new_or_cancelled(self):
101-
if any(record.state not in ('new', 'cancelled') for record in self):
102-
raise UserError("Only 'New' and 'Cancelled' properties can be deleted.")

estate/models/estate_property_offers.py renamed to estate/models/estate_property_offer.py

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

44

@@ -20,6 +20,7 @@ class PropertyOfferModel(models.Model):
2020
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)
2121
validity = fields.Integer(default=7)
2222
date_deadline = fields.Date(compute="_compute_deadline", inverse="_inverse_deadline")
23+
2324
_check_price = models.Constraint(
2425
"CHECK(price >= 0)",
2526
"The price of the offer must be positive."
@@ -34,6 +35,16 @@ def _inverse_deadline(self):
3435
for record in self:
3536
record.validity = (record.date_deadline - fields.Date.to_date(record.create_date)).days if record.date_deadline else record.validity
3637

38+
@api.model
39+
def create(self, vals_list: list[dict]):
40+
for val in vals_list:
41+
estate_property = self.env["estate.property"].browse(val["property_id"])
42+
if any(offer.price > val["price"] for offer in estate_property.offer_ids):
43+
raise UserError(_("Cannot create a new offer with a lower price than an existing offer."))
44+
if estate_property.state == 'new':
45+
estate_property.state = 'received'
46+
return super().create(vals_list)
47+
3748
def accept_offer(self):
3849
self.ensure_one()
3950
self.status = "accepted"
@@ -50,13 +61,3 @@ def refuse_all_other_offers(self):
5061
def refuse_offer(self):
5162
self.ensure_one()
5263
self.status = "refused"
53-
54-
@api.model
55-
def create(self, vals_list: list[dict]):
56-
for val in vals_list:
57-
estate_property = self.env["estate.property"].browse(val["property_id"])
58-
if any(offer.price > val["price"] for offer in estate_property.offer_ids):
59-
raise UserError("Cannot create a new offer with a lower price than an existing offer.")
60-
if estate_property.state == 'new':
61-
estate_property.state = 'received'
62-
return super().create(vals_list)

estate/models/estate_property_tags.py renamed to estate/models/estate_property_tag.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ class PropertyTagModel(models.Model):
55
_name = "estate.property.tag"
66
_description = "Estate Property Tag model"
77
_order = "name"
8+
9+
name = fields.Char(required=True)
10+
color = fields.Integer()
11+
812
_check_tag_uniqueness = models.Constraint(
913
"UNIQUE(name)",
1014
"Each tag should have a unique name."
1115
)
12-
13-
name = fields.Char(required=True)
14-
color = fields.Integer()

estate/models/estate_property_types.py renamed to estate/models/estate_property_type.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ class PropertyTypeModel(models.Model):
55
_name = "estate.property.type"
66
_description = "Estate Property Type model"
77
_order = "sequence"
8-
_check_type_uniqueness = models.Constraint(
9-
"UNIQUE(name)",
10-
"Each type should have a unique name."
11-
)
128

139
name = fields.Char(required=True)
1410
property_ids = fields.One2many("estate.property", "property_type_id")
1511
offer_ids = fields.One2many("estate.property.offer", "property_type_id")
1612
offer_count = fields.Integer("Offer Count", compute="_compute_offer_count")
1713
sequence = fields.Integer("Sequence")
1814

15+
_check_type_uniqueness = models.Constraint(
16+
"UNIQUE(name)",
17+
"Each type should have a unique name."
18+
)
19+
1920
@api.depends("offer_ids")
2021
def _compute_offer_count(self):
2122
for record in self:

estate/models/res_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ class ResUser(models.Model):
66

77
property_ids = fields.One2many(
88
"estate.property", "salesperson"
9-
, domain="['|', ('state', '=', 'new'), ('state', '=', 'received')]"
9+
, domain=['|', ('state', '=', 'new'), ('state', '=', 'received')]
1010
)
File renamed without changes.

estate/views/estate_property_offer_views.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<field name="domain">[('property_type_id', '=', active_id)]</field>
77
</record>
88

9-
<record id="estate_property_offer_list_view" model="ir.ui.view">
9+
<record id="estate_property_offer_view_list" model="ir.ui.view">
1010
<field name="name">estate.property.offer.list</field>
1111
<field name="model">estate.property.offer</field>
1212
<field name="arch" type="xml">
@@ -25,7 +25,7 @@
2525
</field>
2626
</record>
2727

28-
<record id="estate_property_offer_form_view" model="ir.ui.view">
28+
<record id="estate_property_offer_view_form" model="ir.ui.view">
2929
<field name="name">estate.property.offer.form</field>
3030
<field name="model">estate.property.offer</field>
3131
<field name="arch" type="xml">

estate/views/estate_property_tag_views.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<field name="view_mode">list,form</field>
66
</record>
77

8-
<record id="estate_property_tag_list_view" model="ir.ui.view">
8+
<record id="estate_property_tag_view_list" model="ir.ui.view">
99
<field name="name">estate.property.tag.list</field>
1010
<field name="model">estate.property.tag</field>
1111
<field name="arch" type="xml">

0 commit comments

Comments
 (0)