Skip to content

Commit 1264496

Browse files
committed
[ADD] real_estate: implement inheritance and cross-module chapters 12 & 13
Implemented inheritance examples from chapter 12: - Extended existing models using _inherit - Added new fields per tutorial instructions - Verified behavior via demo UI Completed chapter 13 on inter-module communication: - Used cross-module relations and method calls
1 parent bd01dfa commit 1264496

16 files changed

+178
-27
lines changed

real_estate/__manifest__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
'summary': "summary of the real estate",
44
'description': """Rugot first module""",
55
'author': "Ruchita Gothi (Rugot)",
6-
'category': 'Uncategorized',
6+
'category': 'Real Estate',
77
'version': '0.1',
88
'depends': ['base', 'web'],
99
'data': [
1010
'security/ir.model.access.csv',
11-
'views/real_estate_properties_offer_views.xml',
12-
'views/real_estate_properties_views.xml',
13-
'views/real_estate_property_type.xml',
11+
'views/real_estate_property_offer_views.xml',
12+
'views/real_estate_property_views.xml',
13+
'views/real_estate_property_type_views.xml',
1414
'views/real_estate_tag_views.xml',
15-
'views/real_estate_properties_maintenance_request_view.xml',
15+
'views/real_estate_property_maintenance_request_views.xml',
16+
'views/res_users_views.xml',
1617
'views/real_estate_menus.xml',
1718
],
1819
'application': True,

real_estate/models/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from . import real_estate_properties
1+
from . import real_estate_property
22
from . import real_estate_tag
33
from . import real_estate_property_offer
44
from . import real_estate_property_type
5-
from . import real_estate_properties_maintenance_request
5+
from . import real_estate_property_maintenance_request
6+
from . import res_users

real_estate/models/real_estate_properties.py renamed to real_estate/models/real_estate_property.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ class real_estate(models.Model):
5959
selling_price = fields.Float()
6060
maintenance_request_ids = fields.One2many(
6161
"real.estate.property.maintenance.request", "property_id", string="Maintenance Requests")
62-
total_maintenance_cost = fields.Float(compute="_compute_total_maintenance_cost", store=True)
62+
total_maintenance_cost = fields.Float(compute="_compute_total_maintenance_cost", store=True, string="Total Cost")
63+
salesperson_id = fields.Many2one(
64+
'res.users',
65+
string='Salesperson'
66+
)
6367
_check_expected_price_positive = models.Constraint(
6468
'CHECK(expected_price > 0)',
6569
'The expected price must be strictly positive.',
@@ -75,14 +79,15 @@ def _check_selling_price(self):
7579
rec.expected_price * 0.9,
7680
precision_rounding=0.01) < 0:
7781
raise ValidationError(
78-
'The selling price cannot be lower than 90% of the expected price.'
79-
)
82+
'The selling price cannot be lower than 90% of the expected price.')
8083

8184
@api.depends('offer_ids.price')
8285
def _compute_best_price(self):
8386
for record in self:
84-
prices = record.offer_ids.mapped('price')
85-
record.best_price = max(prices) if prices else 0.0
87+
prices=[]
88+
for offer in record.offer_ids:
89+
prices.append(offer.price)
90+
record.best_price=max(prices) if prices else 0.0
8691

8792
@api.depends('maintenance_request_ids.cost')
8893
def _compute_total_maintenance_cost(self):
@@ -135,3 +140,10 @@ def action_sold(self):
135140
if maintenace_request:
136141
raise UserError("CProperty cannot be sold , there is any maintenance request not done")
137142
self.stage = 'sold'
143+
144+
@api.constrains('expected_price')
145+
def _check_expected_price(self):
146+
for rec in self:
147+
if rec.expected_price < 0:
148+
raise ValidationError(
149+
'The selling price cannot be lower than 90% of the expected price.')

real_estate/models/real_estate_properties_maintenance_request.py renamed to real_estate/models/real_estate_property_maintenance_request.py

File renamed without changes.

real_estate/models/real_estate_property_offer.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from datetime import timedelta
2+
from collections import defaultdict
23

34
from odoo import models, fields, api
45
from odoo.exceptions import UserError
6+
from odoo.tools import float_is_zero, float_compare
57

68

79
class real_estate_property_offer(models.Model):
@@ -32,22 +34,67 @@ class real_estate_property_offer(models.Model):
3234
'The offer price must be strictly positive.',
3335
)
3436

37+
# @api.model
38+
# def create(self, vals):
39+
# grouped = defaultdict(list)
40+
# for val in vals:
41+
# property_id = val.get('property_id')
42+
# price = val.get('price')
43+
# if property_id and price:
44+
# grouped[property_id].append(price)
45+
#
46+
# for property_id, new_prices in grouped.items():
47+
# property_rec = self.env['real.estate'].browse(property_id)
48+
# existing_prices = property_rec.best_price
49+
# new_max = max(new_prices)
50+
# max_price = max(existing_prices, new_max)
51+
# for price in new_prices:
52+
# if price < max_price:
53+
# raise UserError(
54+
# "Only the highest offer is allowed"
55+
# )
56+
# offers = super().create(vals)
57+
# for offer in offers:
58+
# if offer.property_id and offer.property_id.stage == 'new':
59+
# offer.property_id.stage = 'offer_received'
60+
# return offers
61+
3562
@api.model
3663
def create(self, vals):
37-
# property_id = vals.get('property_id')
38-
# price = vals.get('price')
39-
# if property_id and price:
40-
# property_rec = self.env['estate.property'].browse(property_id)
41-
# if property_rec.offer_ids:
42-
# max_offer = max(property_rec.offer_ids.mapped('price'))
43-
# if price < max_offer:
44-
# raise UserError(
45-
# "The offer must be higher than existing offers."
46-
# )
47-
offer = super().create(vals)
48-
if offer.property_id and offer.property_id.stage == 'new':
49-
offer.property_id.stage = 'offer_received'
50-
return offer
64+
for val in vals:
65+
price = val.get('price')
66+
property_id = val.get('property_id')
67+
property = self.env['real.estate'].browse(property_id)
68+
if property.stage == "new":
69+
property.best_price = price
70+
elif float_compare(price, property.best_price, precision_rounding=0.01) < 0:
71+
raise UserError(
72+
f"Price should be greater than {property.best_price}")
73+
else:
74+
property.best_price = price
75+
if property and property.stage == 'new':
76+
property.stage = 'offer_received'
77+
78+
return super().create(vals)
79+
80+
# @api.model
81+
# def create(self, vals):
82+
# for property_id, n
83+
# for val in vals:
84+
# property_id = val.get('property_id')
85+
# price = val.get('price')
86+
# if property_id and price:
87+
# property_rec = self.env['real.estate'].browse(property_id)
88+
# if property_rec.offer_ids:
89+
# # max_offer = max(property_rec.offer_ids.mapped('price'))
90+
# if price < property_rec.best_price:
91+
# raise UserError(
92+
# "The offer must be higher than existing offers."
93+
# )
94+
# offer = super().create(vals)
95+
# if offer.property_id and offer.property_id.stage == 'new':
96+
# offer.property_id.stage = 'offer_received'
97+
# return offer
5198

5299
@api.depends('create_date', 'validity')
53100
def _compute_date_deadline(self):
@@ -88,7 +135,7 @@ def action_accept(self):
88135
self.status = 'accepted'
89136
self.property_id.write({
90137
'selling_price': self.price,
91-
'stage': 'sold',
138+
'stage': 'offer_accepted',
92139
'buyer_id': self.partner_id.id,
93140
})
94141
refused_offer = self.property_id.offer_ids.filtered_domain([

real_estate/models/res_users.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from odoo import api, fields, models
2+
3+
4+
class ResUsers(models.Model):
5+
_inherit = 'res.users'
6+
7+
property_ids = fields.One2many(
8+
'real.estate',
9+
'salesperson_id',
10+
string='Properties'
11+
)

real_estate/views/real_estate_properties_maintenance_request_view.xml renamed to real_estate/views/real_estate_property_maintenance_request_views.xml

File renamed without changes.

real_estate/views/real_estate_properties_offer_views.xml renamed to real_estate/views/real_estate_property_offer_views.xml

File renamed without changes.
File renamed without changes.

real_estate/views/real_estate_properties_views.xml renamed to real_estate/views/real_estate_property_views.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<field name="tag_ids" widget="many2many_tags" options="{'color_field': 'color'}"/>
5757
<field name="expected_price"/>
5858
<field name="best_price" readonly="1"/>
59+
<field name="salesperson_id"/>
5960
</group>
6061
</group>
6162
<notebook>

0 commit comments

Comments
 (0)