-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] estate: Created Action and Constraints
Button & Action -Creating two button cancelled and sold -Initialize action for both the buttons -Added functionality cancelled property can not be sold and and sold property can not be cancelled -Created two button for offer accepted and refused through the icon and added functionality on it, if click on refused icon then my offer status is refused and click on accepted icon status will accepted. End here chapter 9 Sql constraints & Python constrains -Added functionality for expected price,offer price and selling price must be positive and property_tag_name and property_type_name must be unique through the _sql_constraints and using of unique keyword -Add a constraint so that the selling price cannot be lower than 90% of the expected price using of python_constrains and showing error using of ValidationError .
- Loading branch information
Showing
8 changed files
with
211 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,18 @@ | ||
{ | ||
'name': 'Real Estate', | ||
'version': '1.0', | ||
'category': 'Real Estate', | ||
'summary': 'Manage properties', | ||
'depends': ['base_setup'], | ||
'data': [ | ||
|
||
'security/ir.model.access.csv', | ||
'views/real_estate_property.xml', | ||
'views/estate_property_tree_views.xml', | ||
'views/property_type_views.xml', | ||
'views/property_tag.xml', | ||
'views/real_estate_menu.xml', | ||
|
||
"name": "Real Estate", | ||
"version": "1.0", | ||
"category": "Real Estate", | ||
"summary": "Manage properties", | ||
"depends": ["base_setup"], | ||
"data": [ | ||
"security/ir.model.access.csv", | ||
"views/real_estate_property.xml", | ||
"views/estate_property_tree_views.xml", | ||
"views/property_type_views.xml", | ||
"views/property_tag.xml", | ||
"views/real_estate_menu.xml", | ||
], | ||
'installable': True, | ||
'application': True, | ||
'auto_install': False, | ||
"installable": True, | ||
"application": True, | ||
"auto_install": False, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,3 @@ | |
from . import property_type | ||
from . import property_tag | ||
from . import property_offer | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,123 @@ | ||
from odoo import models, fields | ||
from odoo import models, fields, api | ||
from odoo.exceptions import UserError, ValidationError | ||
|
||
|
||
class MyModel(models.Model): | ||
_name = 'estate_property' | ||
_description = 'Real Estate' | ||
|
||
name = fields.Char(string='Title', required=True) | ||
description = fields.Text(string='Description') | ||
postcode = fields.Char(string='Postcode') | ||
date_availability = fields.Date(string='Available From') | ||
expected_price = fields.Float(string='Expected Price') | ||
selling_price = fields.Float(string='Selling Price') | ||
bedrooms = fields.Integer(string='Bedrooms') | ||
living_area = fields.Integer(string='Living Area (sqm)') | ||
facades = fields.Integer(string='Facades') | ||
garage = fields.Boolean(string='Garage') | ||
garden = fields.Boolean(string='Garden') | ||
garden_area = fields.Integer(string='Garden Area (sqm)') | ||
_name = "estate_property" | ||
_description = "Real Estate" | ||
|
||
name = fields.Char(string="Title", required=True) | ||
description = fields.Text(string="Description") | ||
postcode = fields.Char(string="Postcode") | ||
date_availability = fields.Date(string="Available From") | ||
expected_price = fields.Float(string="Expected Price") | ||
selling_price = fields.Float(string="Selling Price", default="0.00") | ||
bedrooms = fields.Integer(string="Bedrooms") | ||
living_area = fields.Integer(string="Living Area (sqm)") | ||
facades = fields.Integer(string="Facades") | ||
garage = fields.Boolean(string="Garage") | ||
garden = fields.Boolean(string="Garden") | ||
garden_area = fields.Integer(string="Garden Area (sqm)") | ||
garden_orientation = fields.Selection( | ||
selection=[ | ||
('north', 'North'), | ||
('south', 'South'), | ||
('east', 'East'), | ||
('west', 'West')], | ||
("north", "North"), | ||
("south", "South"), | ||
("east", "East"), | ||
("west", "West"), | ||
], | ||
) | ||
active = fields.Boolean(default=True) | ||
state = fields.Selection(string='Status', | ||
state = fields.Selection( | ||
string="Status", | ||
selection=[ | ||
('new', 'New'), | ||
('offer received', 'Offer Received'), | ||
('offer accepted', 'Offer Accepted'), | ||
('sold', 'Sold'), | ||
('canceled', 'Canceled')], | ||
default='new' | ||
("new", "New"), | ||
("offer received", "Offer Received"), | ||
("offer accepted", "Offer Accepted"), | ||
("sold", "Sold"), | ||
("canceled", "Canceled"), | ||
], | ||
default="new", | ||
) | ||
|
||
salesman_id = fields.Many2one( | ||
'res.users', # The model this field relates to | ||
string='Salesman', # Label for the field | ||
default=lambda self: self.env.user # Set default to the current user | ||
"res.users", # The model this field relates to | ||
string="Salesman", # Label for the field | ||
default=lambda self: self.env.user.partner_id, # Set default to the current user | ||
) | ||
buyer_id = fields.Many2one( | ||
'res.partner', # The model this field relates to | ||
string='Buyer', # Label for the field | ||
copy=False # Prevent the field value from being copied when duplicating the record | ||
"res.partner", # The model this field relates to | ||
string="Buyer", # Label for the field | ||
copy=False, # Prevent the field value from being copied when duplicating the record | ||
) | ||
property_type_id = fields.Many2one( | ||
"real.estate.property.type", string="Property Type" | ||
) | ||
tag_ids = fields.Many2many("real.estate.property.tag", string="tags") | ||
offer_ids = fields.One2many("estate.property.offer", "property_id", string="Offers") | ||
total_area = fields.Float(compute="_compute_total_area") | ||
best_offer = fields.Float(compute="_compute_best_offer") | ||
|
||
@api.depends("living_area", "garden_area") | ||
def _compute_total_area(self): | ||
for record in self: | ||
record.total_area = record.living_area + record.garden_area | ||
|
||
@api.depends("offer_ids.price", "best_offer") | ||
def _compute_best_offer(self): | ||
for record in self: | ||
if record.offer_ids: | ||
record.best_offer = max(record.offer_ids.mapped("price")) | ||
else: | ||
record.best_offer = 0.0 | ||
|
||
@api.onchange("garden") | ||
def _onchange_garden(self): | ||
if self.garden: | ||
self.garden_area = 10 | ||
self.garden_orientation = "north" | ||
else: | ||
self.garden_area = False | ||
self.garden_orientation = False | ||
|
||
def action_cancel(self): | ||
for record in self: | ||
if record.state == "sold": | ||
raise UserError("Sold property cannot be cancelled.") | ||
record.state = "cancelled" | ||
|
||
def action_sold(self): | ||
for record in self: | ||
if record.state == "cancelled": | ||
raise UserError("Cancelled property cannot be sold.") | ||
record.state = "sold" | ||
|
||
@api.depends("offer_ids.price", "selling_price") | ||
def _compute_best_offer(self): | ||
for record in self: | ||
if record.offer_ids: | ||
record.best_offer = max(record.offer_ids.mapped("price")) | ||
else: | ||
record.best_offer = 0.0 | ||
|
||
_sql_constraints = [ | ||
( | ||
"selling_price", | ||
"CHECK(selling_price >= 0)", | ||
"A property selling price must be strictly positive.", | ||
), | ||
( | ||
"expected_price", | ||
"CHECK(expected_price >= 0)", | ||
"A property expected price must be strictly positive.", | ||
), | ||
] | ||
|
||
tag_ids = fields.Many2many('real.estate.property.tag', string='tags') | ||
offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers') | ||
@api.constrains("selling_price", "expected_price") | ||
def _check_selling_price(self): | ||
for record in self: | ||
if record.selling_price > 0: | ||
price_per = (record.expected_price * 90) / 100 | ||
if record.selling_price < price_per: | ||
raise ValidationError( | ||
"selling price cannot be lower than 90% of the expected price." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,54 @@ | ||
from odoo import models, fields | ||
from odoo import api, models, fields | ||
from dateutil.relativedelta import relativedelta | ||
|
||
|
||
class EstatePropertyOffer(models.Model): | ||
_name = 'estate.property.offer' | ||
_description = 'Estate Property Offer' | ||
_name = "estate.property.offer" | ||
_description = "Estate Property Offer" | ||
|
||
price = fields.Float(string="Price", required=True) | ||
status = fields.Selection( | ||
[("accepted", "Accepted"), ("refused", "Refused")], | ||
string="Status", | ||
required=True, | ||
copy=False, | ||
) | ||
partner_id = fields.Many2one("res.partner", string="Partner", required=True) | ||
property_id = fields.Many2one("estate_property", string="Property", required=True) | ||
validity = fields.Integer(string="Validity (days)", default=7) | ||
deadline_date = fields.Date( | ||
string="Deadline", compute="_compute_deadline", inverse="_inverse_deadline" | ||
) | ||
|
||
@api.depends("property_id.create_date", "validity") | ||
def _compute_deadline(self): | ||
for record in self: | ||
if record.property_id.create_date: | ||
record.deadline_date = record.property_id.create_date + relativedelta( | ||
days=record.validity | ||
) | ||
else: | ||
record.deadline_date = False | ||
|
||
def _inverse_deadline(self): | ||
for record in self: | ||
if record.property_id.create_date: | ||
flag = fields.Date.from_string(record.deadline_date) | ||
flag1 = fields.Date.from_string(record.property_id.create_date) | ||
record.validity = (flag - flag1).days | ||
else: | ||
record.validity = 7 | ||
|
||
def action_refused(self): | ||
for record in self: | ||
record.status = "refused" | ||
|
||
price = fields.Float(string='Price', required=True) | ||
status = fields.Selection([ | ||
('accepted', 'Accepted'), | ||
('refused', 'Refused') | ||
], string='Status', required=True, copy=False) | ||
partner_id = fields.Many2one('res.partner', string='Partner', required=True) | ||
property_id = fields.Many2one('estate_property', string='Property', required=True) | ||
def action_accepted(self): | ||
for record in self: | ||
record.status = "accepted" | ||
record.property_id.selling_price = record.price | ||
record.property_id.buyer_id = record.partner_id | ||
|
||
_sql_constraints = [ | ||
("price", "CHECK(price >= 0)", "A price must be strictly positive.") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from odoo import models, fields | ||
|
||
|
||
class PropertyTag(models.Model): | ||
_name = 'real.estate.property.tag' | ||
_description = 'Property Type' | ||
_name = "real.estate.property.tag" | ||
_description = "Property Type" | ||
|
||
name = fields.Char(required=True) | ||
|
||
name = fields.Char(required=True) | ||
_sql_constraints = [("name", "UNIQUE(name)", "A property tag name must be unique")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
from odoo import models, fields | ||
|
||
|
||
class PropertyType(models.Model): | ||
_name = 'real.estate.property.type' | ||
_description = 'Property Type' | ||
_name = "real.estate.property.type" | ||
_description = "Property Type" | ||
|
||
name = fields.Char(string="Property Type", required=True) | ||
description = fields.Text(string="Description") | ||
|
||
name = fields.Char(string='Property Type', required=True) | ||
description = fields.Text(string='Description') | ||
_sql_constraints = [("name", "UNIQUE(name)", "A property type name must be unique")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
from odoo import models, fields | ||
|
||
|
||
class MyModel(models.Model): | ||
_name = 'test_model' | ||
_description = 'My New Model' | ||
_name = "test_model" | ||
_description = "My New Model" | ||
|
||
name = fields.Char(string='Name', required=True) | ||
name = fields.Char(string="Name", required=True) | ||
description = fields.Text() | ||
address = fields.Text() | ||
address = fields.Text() |
Oops, something went wrong.