Skip to content

Commit d8b4942

Browse files
author
joyep
committed
[ADD] estate: Chapter 12 - add inheritances
Implement inherited user model and views, add CRUD validation for property offers
1 parent 670422d commit d8b4942

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

estate/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"views/estate_property_type_views.xml",
1111
"views/estate_property_tag_views.xml",
1212
"views/estate_menus.xml",
13+
"views/inherited_users_views.xml",
1314
],
1415
"installable": True,
1516
"application": True,

estate/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
estate_property_offer,
44
estate_property_tag,
55
estate_property_type,
6+
res_users,
67
)

estate/models/estate_property.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ def _onchange_garden(self):
9999
if not self.garden_orientation:
100100
self.garden_orientation = "north"
101101

102+
# ----------------------------------------
103+
# CRUD methods
104+
# ----------------------------------------
105+
@api.ondelete(at_uninstall=False)
106+
def _unlink_if_new_or_canceled(self):
107+
for record in self:
108+
if record.status not in ("new", "canceled"):
109+
msg = "You can only delete properties with status 'New' or 'Canceled'."
110+
raise UserError(msg)
111+
102112
# ----------------------------------------
103113
# Action methods
104114
# ----------------------------------------

estate/models/estate_property_offer.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ def _inverse_date_deadline(self):
6363
# ----------------------------------------
6464
@api.model_create_multi
6565
def create(self, vals_list):
66+
# Validate offer amounts before creation
67+
for vals in vals_list:
68+
if "property_id" in vals and "price" in vals:
69+
# Get the property record
70+
property_record = self.env["estate.property"].browse(vals["property_id"])
71+
# Check if there are existing offers with higher or equal prices
72+
existing_offers = property_record.offer_ids
73+
if existing_offers:
74+
max_existing_price = max(existing_offers.mapped("price"))
75+
if vals["price"] <= max_existing_price:
76+
msg = f"The offer amount must be higher than the existing offer of {max_existing_price}."
77+
raise UserError(msg)
78+
6679
# Create the offers
6780
offers = super().create(vals_list)
6881
# Update property status to 'offer_received' for all related properties

estate/models/res_users.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from odoo import fields, models
2+
3+
4+
class ResUsers(models.Model):
5+
# ----------------------------------------
6+
# Private attributes
7+
# ----------------------------------------
8+
_inherit = "res.users"
9+
10+
# ----------------------------------------
11+
# Field declarations
12+
# ----------------------------------------
13+
property_ids = fields.One2many("estate.property", "salesperson_id", string="Real Estate Properties")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<odoo>
3+
<data>
4+
5+
<record id="res_users_view_form" model="ir.ui.view">
6+
<field name="name">res.users.view.form.inherit.estate</field>
7+
<field name="model">res.users</field>
8+
<field name="inherit_id" ref="base.view_users_form"/>
9+
<field name="arch" type="xml">
10+
<xpath expr="//notebook" position="inside">
11+
<page string="Real Estate Properties">
12+
<field name="property_ids"/>
13+
</page>
14+
</xpath>
15+
</field>
16+
</record>
17+
18+
</data>
19+
</odoo>

0 commit comments

Comments
 (0)