-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[ADD] estate: Server Framework 101 implementation #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lud0do1202
wants to merge
1
commit into
odoo:18.0
Choose a base branch
from
odoo-dev:18.0-training-ltra
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
'name': 'Real Estate', | ||
'version': '1.0', | ||
'description': 'Real Estate Management System', | ||
'summary': 'Real Estate Management System', | ||
'license': 'LGPL-3', | ||
'depends': ['base'], | ||
'data': [ | ||
'security/ir.model.access.csv', | ||
'views/estate_property_offer.xml', | ||
'views/estate_property_tag.xml', | ||
'views/estate_property_type.xml', | ||
'views/estate_property.xml', | ||
'views/estate_menus.xml', | ||
'views/res_user.xml', | ||
], | ||
'auto_install': False, | ||
'application': True, | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from . import estate_property | ||
from . import estate_property_offer | ||
from . import estate_property_tag | ||
from . import estate_property_type | ||
from . import res_users |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from dateutil.relativedelta import relativedelta | ||
from odoo import _, api, fields, models | ||
from odoo.exceptions import UserError | ||
|
||
|
||
class EstateProperty(models.Model): | ||
_name = 'estate.property' | ||
_description = 'Real estate property with offers, pricing, and availability.' | ||
_order = 'id desc' | ||
|
||
_sql_constraints = [ | ||
('check_expected_price', 'CHECK(expected_price > 0)', 'Expected price must be strictly positive!'), | ||
('check_selling_price', 'CHECK(selling_price >= 0)', 'Selling price must be positive!'), | ||
] | ||
|
||
name = fields.Char('Title', required=True) | ||
description = fields.Text('Description') | ||
postcode = fields.Char('Postcode') | ||
date_availability = fields.Date( | ||
'Availability From', | ||
copy=False, | ||
default=lambda self: fields.Date.today() + relativedelta(months=3), | ||
) | ||
expected_price = fields.Float('Expected Price', required=True) | ||
selling_price = fields.Float('Selling Price', readonly=True, copy=False) | ||
best_price = fields.Float('Best Price', compute='_compute_best_price', store=True) | ||
property_type_id = fields.Many2one('estate.property.type', 'Property Type') | ||
tag_ids = fields.Many2many('estate.property.tag', string='Tags') | ||
buyer_id = fields.Many2one('res.partner', 'Buyer') | ||
seller_id = fields.Many2one('res.users', 'Seller', default=lambda self: self.env.user) | ||
bedrooms = fields.Integer('Bedrooms', default=2) | ||
living_area = fields.Integer('Living Area (sqm)') | ||
facades = fields.Integer('Facades') | ||
garage = fields.Boolean('Garage') | ||
garden = fields.Boolean('Garden') | ||
garden_area = fields.Integer('Garden Area') | ||
garden_orientation = fields.Selection( | ||
[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')], | ||
'Garden Orientation', | ||
) | ||
total_area = fields.Integer('Total Area (sqm)', compute='_compute_total_area', store=True) | ||
state = fields.Selection( | ||
[ | ||
('new', 'New'), | ||
('offer_received', 'Offer Received'), | ||
('offer_accepted', 'Offer Accepted'), | ||
('sold', 'Sold'), | ||
('cancelled', 'Cancelled'), | ||
], | ||
'State', | ||
default='new', | ||
required=True, | ||
copy=False, | ||
compute='_compute_state', | ||
store=True, | ||
) | ||
offer_ids = fields.One2many('estate.property.offer', 'property_id', 'Offers') | ||
active = fields.Boolean('Active', default=True) | ||
|
||
###### COMPUTE ###### | ||
@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') | ||
def _compute_best_price(self): | ||
for record in self: | ||
prices = record.offer_ids.mapped('price') | ||
if prices: | ||
record.best_price = max(prices) | ||
else: | ||
record.best_price = 0.0 | ||
|
||
@api.depends('offer_ids') | ||
def _compute_state(self): | ||
for record in self: | ||
if not record.offer_ids: | ||
record.state = 'new' | ||
elif record.state == 'new': | ||
record.state = 'offer_received' | ||
|
||
###### ONCHANGE ###### | ||
@api.onchange('garden') | ||
def _onchange_garden(self): | ||
if self.garden: | ||
self.garden_area = 10 | ||
self.garden_orientation = 'north' | ||
else: | ||
self.garden_area = 0 | ||
self.garden_orientation = False | ||
|
||
###### CRUD ###### | ||
def unlink(self): | ||
if any(record.state not in ['new', 'cancelled'] for record in self): | ||
raise UserError(_('You cannot delete a property that is not new or cancelled.')) | ||
return super().unlink() | ||
|
||
###### ACTION ###### | ||
def action_sold(self): | ||
for record in self: | ||
if record.state == 'sold': | ||
raise UserError(_('Property is already sold.')) | ||
if record.state == 'cancelled': | ||
raise UserError(_('Property is cancelled and cannot be sold.')) | ||
if record.state != 'offer_accepted': | ||
raise UserError(_('You must accept an offer before marking the property as sold.')) | ||
|
||
record.state = 'sold' | ||
return True | ||
|
||
def action_cancel(self): | ||
for record in self: | ||
if record.state == 'cancelled': | ||
raise UserError(_('Property is already cancelled.')) | ||
if record.state == 'sold': | ||
raise UserError(_('Sold properties cannot be cancelled.')) | ||
|
||
record.state = 'cancelled' | ||
return True |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from dateutil.relativedelta import relativedelta | ||
from odoo import _, api, fields, models | ||
from odoo.exceptions import ValidationError | ||
from odoo.tools.float_utils import float_compare | ||
|
||
|
||
class EstatePropertyOffer(models.Model): | ||
_name = 'estate.property.offer' | ||
_description = 'An offer for a property' | ||
_order = 'price desc' | ||
|
||
_sql_constraints = [ | ||
('check_price', 'CHECK(price > 0)', 'Price must be strictly positive!'), | ||
] | ||
|
||
price = fields.Float('Price') | ||
status = fields.Selection( | ||
[('accepted', 'Accepted'), ('refused', 'Refused'), ('pending', 'Pending')], | ||
'Status', | ||
default='pending', | ||
required=True, | ||
copy=False, | ||
) | ||
partner_id = fields.Many2one('res.partner', 'Partner', required=True) | ||
property_id = fields.Many2one('estate.property', 'Property', required=True) | ||
property_type_id = fields.Many2one( | ||
'estate.property.type', | ||
'Property Type', | ||
related='property_id.property_type_id', | ||
store=True, | ||
) | ||
validity = fields.Integer('Validity (days)', help='Validity in days', default=7) | ||
date_deadline = fields.Date( | ||
'Deadline', | ||
compute='_compute_date_deadline', | ||
inverse='_inverse_date_deadline', | ||
store=True, | ||
) | ||
|
||
###### COMPUTE ###### | ||
@api.depends('validity') | ||
def _compute_date_deadline(self): | ||
for record in self: | ||
if not record.validity: | ||
record.date_deadline = False | ||
else: | ||
record.date_deadline = fields.Date.today() + relativedelta(days=record.validity) | ||
|
||
def _inverse_date_deadline(self): | ||
for record in self: | ||
if not record.date_deadline: | ||
record.validity = 0 | ||
else: | ||
record.validity = (record.date_deadline - fields.Date.today()).days | ||
|
||
###### CRUD ###### | ||
@api.model_create_multi | ||
def create(self, vals): | ||
for val in vals: | ||
val_price = val.get('price') | ||
val_property_id = val.get('property_id') | ||
if not val_price or not val_property_id: | ||
raise ValidationError(_('Price and Property ID are required.')) | ||
property_id = self.env['estate.property'].browse(val_property_id) | ||
if not property_id: | ||
raise ValidationError(_(f'Property with ID {val_property_id} does not exist.')) | ||
best_price = property_id.best_price | ||
if float_compare(val_price, best_price, precision_digits=2) <= 0: | ||
raise ValidationError(_(f'The offer price must be higher than the current best price (${best_price}).')) | ||
return super().create(vals) | ||
|
||
###### ACTIONS ###### | ||
def action_accept(self): | ||
for record in self: | ||
record.status = 'accepted' | ||
record.property_id.selling_price = record.price | ||
record.property_id.buyer_id = record.partner_id | ||
record.property_id.state = 'offer_accepted' | ||
|
||
other_offers = record.property_id.offer_ids.filtered(lambda x: x.id != record.id) | ||
other_offers.action_refuse() | ||
return True | ||
|
||
def action_refuse(self): | ||
for record in self: | ||
record.status = 'refused' | ||
return True | ||
|
||
def action_revert(self): | ||
for record in self: | ||
if record.status == 'accepted': | ||
record.property_id.state = 'offer_received' | ||
record.property_id.buyer_id = False | ||
record.property_id.selling_price = 0 | ||
record.status = 'pending' | ||
return True | ||
|
||
###### CONSTRAINTS ###### | ||
@api.constrains('price') | ||
def _check_price(self): | ||
for record in self: | ||
min_offer_price = record.property_id.expected_price * 0.9 | ||
if float_compare(record.price, min_offer_price, precision_digits=2) < 0: | ||
raise ValidationError( | ||
_(f'The offer price must be at least 90% of the expected price (${min_offer_price}).') | ||
) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from random import randint | ||
|
||
from odoo import fields, models | ||
|
||
|
||
class EstatePropertyTag(models.Model): | ||
_name = 'estate.property.tag' | ||
_description = 'Tag for categorizing estate properties' | ||
_order = 'name asc' | ||
|
||
_sql_constraints = [ | ||
('unique_name', 'UNIQUE(name)', 'Tag name must be unique!'), | ||
] | ||
|
||
def _get_default_color(self): | ||
return randint(1, 11) | ||
|
||
name = fields.Char('Name', required=True) | ||
color = fields.Integer('Color', default=_get_default_color) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from odoo import api, fields, models | ||
|
||
|
||
class EstatePropertyType(models.Model): | ||
_name = 'estate.property.type' | ||
_description = 'Type for categorizing estate properties' | ||
_order = 'name asc' | ||
|
||
_sql_constraints = [ | ||
('unique_name', 'UNIQUE(name)', 'Type name must be unique!'), | ||
] | ||
|
||
sequence = fields.Integer('Sequence', default=10) | ||
name = fields.Char('Name', required=True) | ||
property_ids = fields.One2many('estate.property', 'property_type_id', 'Properties') | ||
offer_ids = fields.One2many('estate.property.offer', 'property_type_id', 'Offers') | ||
offer_count = fields.Integer('Offers Count', compute='_compute_offer_count') | ||
|
||
@api.depends('offer_ids') | ||
def _compute_offer_count(self): | ||
for record in self: | ||
record.offer_count = len(record.offer_ids) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from odoo import fields, models | ||
|
||
|
||
class ResUsers(models.Model): | ||
_inherit = 'res.users' | ||
|
||
property_ids = fields.One2many( | ||
'estate.property', | ||
'seller_id', | ||
'Properties', | ||
domain=[('state', 'in', ['new', 'offer_received'])], | ||
) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink | ||
estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1 | ||
estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1 | ||
estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1 | ||
estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1 |
This file contains hidden or 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 | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||||||
<?xml version="1.0" encoding="utf-8"?> | ||||||||
<odoo> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please, keep the XML declaration as it may cause issues with some parsers |
||||||||
<menuitem id="real_estate_root_menu" name="Real Estate"> | ||||||||
<menuitem id="advertissements_menu" name="Advertissements" sequence="10"> | ||||||||
<menuitem id="advertissements_property_menu" name="Properties" action="estate_property_action" sequence="10"/> | ||||||||
</menuitem> | ||||||||
|
||||||||
<menuitem id="settings_menu" name="Settings" sequence="20"> | ||||||||
<menuitem id="settings_property_types_menu" name="Property Types" action="estate_property_types_action" sequence="10"/> | ||||||||
<menuitem id="settings_property_tags_menu" name="Property Tags" action="estate_property_tags_action" sequence="20"/> | ||||||||
</menuitem> | ||||||||
</menuitem> | ||||||||
</odoo> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avec you checked the behavior is correct when
val_price
andval_property_id
are null ?Maybe worth writing a test to ensure it never happens π§