Skip to content

Commit

Permalink
[ADD] estate: Added inheritance to extend the functionality of curren…
Browse files Browse the repository at this point in the history
…t model

Chapter-12: Implement inheritance for CRUD methods and model extensions

- Added custom business logic to prevent property deletion if the state is not
  'New' or 'Canceled'.
- Modified property state to 'Offer Received' upon offer creation.
- Prevented creation of offers with a lower price than existing ones.
- Extended `res.users` model by adding `property_ids` field to display
  salesperson's properties.
- Implemented view inheritance to include the new field in the user form view.
  • Loading branch information
pach-odoo committed Aug 13, 2024
1 parent 959c941 commit 2d0213c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
3 changes: 2 additions & 1 deletion estate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
"data": [
"security/ir.model.access.csv",
"views/estate_property_views.xml",
"views/res_user_model_views.xml",
"views/estate_property_offer_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_offer_views.xml",
"views/estate_menus.xml",
],
}
1 change: 1 addition & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import res_user
6 changes: 6 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ def action_sold_property(self):
else:
self.write({"state": "sold"})
return True

@api.ondelete(at_uninstall=False)
def _unlink_except_certain_state(self):
for record in self:
if record.state not in ['new', 'canceled']:
raise UserError(_('Only new or canceled property can be deleted.'))
16 changes: 13 additions & 3 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from odoo import api, fields, models
from odoo import api, fields, models, _
from datetime import timedelta, date

from odoo.exceptions import UserError

class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
Expand All @@ -18,7 +18,9 @@ class EstatePropertyOffer(models.Model):
compute="_compute_date_deadline", inverse="_inverse_date_deadline"
)

property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)
property_type_id = fields.Many2one(
related="property_id.property_type_id", store=True
)
_sql_constraints = [
("positive_offer_price", "CHECK(price > 0)", "Offer Price Must be Positive")
]
Expand Down Expand Up @@ -52,3 +54,11 @@ def action_accept_offer(self):
def action_refuse_offer(self):
self.write({"status": "refused"})
return True

@api.model
def create(self, vals):
property_id = self.env["estate.property"].browse(vals["property_id"])
property_id.state = "offer received"
if any(offer.price > vals.get("price", 0) for offer in property_id.offer_ids):
raise UserError(_("You cannot create an offer with a lower price than an existing offer."))
return super().create(vals)
11 changes: 11 additions & 0 deletions estate/models/res_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from odoo import models, fields


class ResUser(models.Model):
_inherit = "res.users"

property_ids = fields.One2many(
"estate.property",
"salesman_id",
domain="['|', ('state', '=', 'new'), ('state', '=', 'offer received')]"
)
27 changes: 27 additions & 0 deletions estate/views/res_user_model_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<odoo>
<record id="view_users_form_inherited" model="ir.ui.view">
<field name="name">res.users.form.inherit.properties</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook" position="inside">
<page string="Properties">
<field name="property_ids">
<tree string="Properties">
<field name="name"/>
<field name="property_type_id"/>
<field name="state" />
<field name="postcode"/>
<field name="tag_ids" widget="many2many_tags" />
<field name="bedrooms"/>
<field name="living_area"/>
<field name="expected_price"/>
<field name="selling_price"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 2d0213c

Please sign in to comment.