Skip to content

[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
wants to merge 1 commit into
base: 18.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions estate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions estate/__manifest__.py
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,
}
5 changes: 5 additions & 0 deletions estate/models/__init__.py
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
120 changes: 120 additions & 0 deletions estate/models/estate_property.py
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
106 changes: 106 additions & 0 deletions estate/models/estate_property_offer.py
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')
Comment on lines +60 to +61

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 and val_property_id are null ?
Maybe worth writing a test to ensure it never happens 🧐

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}).')
)
19 changes: 19 additions & 0 deletions estate/models/estate_property_tag.py
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)
22 changes: 22 additions & 0 deletions estate/models/estate_property_type.py
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)
12 changes: 12 additions & 0 deletions estate/models/res_users.py
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'])],
)
5 changes: 5 additions & 0 deletions estate/security/ir.model.access.csv
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
13 changes: 13 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<odoo>
<?xml version="1.0" encoding="utf-8"?>
<odoo>

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>
Loading