Skip to content
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
17 changes: 17 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
'name': 'Real Estate',
'depends': ['base'],
'application': True,
'installable': True,
'author': 'Odoo S.A.',
'license': 'LGPL-3',
'data': [
'security/ir.model.access.csv',
'views/estate_property_offer_views.xml',
'views/estate_property_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_menus.xml'
],

}
4 changes: 4 additions & 0 deletions estate/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import estate_property
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
124 changes: 124 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from dateutil.relativedelta import relativedelta

from odoo import models, fields, api
from odoo.exceptions import UserError, ValidationError
from odoo.tools.float_utils import float_compare, float_is_zero


class EstateProperty(models.Model):
_name = "estate.property"
_description = "Real Estate Property"
_order = "id desc"

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()

date_availability = fields.Date(
"Availability Date",
default=lambda self: fields.Date.today() + relativedelta(months=3),
)

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

expected_price = fields.Float("Expected Price", required=True)
selling_price = fields.Float("Selling Price", readonly=True)

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

bedrooms = fields.Integer(default=2)
living_area = fields.Integer("Living Area(sqft)")
facades = fields.Integer()
garage = fields.Boolean()

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

garden = fields.Boolean()
garden_area = fields.Integer("Garden Area(sqft)")
garden_orientation = fields.Selection(
[
("north", "North"),
("south", "South"),
("east", "East"),
("west", "West"),
],
string="Garden Orientation",
)

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

state = fields.Selection(
[
("new", "New"),
("offer_received", "Offer Received"),
("offer_accepted", "Offer Accepted"),
("sold", "Sold"),
("cancelled", "Cancelled"),
],
"Status",
required=True,
copy=False,
default="new",
)

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

active = fields.Boolean(default=True)

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

property_type_id = fields.Many2one("estate.property.type", "Property Type")
buyer_id = fields.Many2one("res.partner", "Buyer", copy=False)
salesperson_id = fields.Many2one("res.users", string="Salesperson")

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

tag_ids = fields.Many2many("estate.property.tag", string="Tags")

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

offer_ids = fields.One2many(
"estate.property.offer",
"property_id",
string="Offers"
)

Choose a reason for hiding this comment

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

Empty line not required, unnecessary diff.

total_area = fields.Integer("Total Area(sqm)", compute="_compute_total_area")
best_price = fields.Float("Best Offer", compute="_compute_best_price")

_check_expected_price = models.Constraint(
'CHECK(expected_price > 0)',
'The expected price of a property must be strictly positive.',
)


Comment on lines +77 to +78

Choose a reason for hiding this comment

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

Should be only one empty line. Please check odoo base code for reference.

@api.depends("living_area", "garden_area")
def _compute_total_area(self):
for record in self:
record.total_area = (record.living_area or 0) + (record.garden_area or 0)

@api.depends("offer_ids.price")
def _compute_best_price(self):
for record in self:
record.best_price = max(record.offer_ids.mapped("price"), default=0)

@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

def action_cancel(self):
for record in self:
if record.state == "sold":
raise UserError("A sold property cannot be cancelled")
record.state = "cancelled"

def action_sold(self):
for record in self:
if record.state == "cancelled":
raise UserError("A cancelled property cannot be set as sold")
record.state = "sold"

@api.constrains("selling_price", "expected_price")
def _check_selling_price(self):
for record in self:
if not float_is_zero(record.selling_price, precision_digits=2):
min_price = record.expected_price * 0.9
if float_compare(record.selling_price, min_price, precision_digits=2) < 0:
raise ValidationError(
"The selling price cannot be lower than 90% of the expected price."
)

@api.ondelete(at_uninstall=False)
def _unlink_if_new_or_cancelled(self):
for record in self:
if record.state not in ("new", "cancelled"):
raise UserError("Only 'New' or 'Cancelled' property can be deleted.")
99 changes: 99 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from dateutil.relativedelta import relativedelta
from odoo import fields, models, api
from odoo.exceptions import UserError


class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
_description = "Real Estate Property Offer"
_order = "price desc"

price = fields.Float()
status = fields.Selection(
[("accepted", "Accepted"), ("refused", "Refused")],
copy=False,
)
partner_id = fields.Many2one("res.partner", required=True)
property_id = fields.Many2one("estate.property", required=True)
validity = fields.Integer(default=7)
date_deadline = fields.Date(
string="Deadline Date",
compute="_compute_date_deadline",
inverse="_inverse_date_deadline"
)

property_type_id = fields.Many2one(
related="property_id.property_type_id",
store=True
)

@api.depends('validity')
def _compute_date_deadline(self):
for record in self:
creation_date = record.create_date or fields.Date.today()
record.date_deadline = creation_date + relativedelta(days=record.validity)

def _inverse_date_deadline(self):
for record in self:
creation_date = record.create_date or fields.Date.today()
record.validity = (record.date_deadline - creation_date).days

def action_accept(self):
for record in self:
if record.property_id.buyer_id:
raise UserError("Property already has an accepted offer.")

record.status = 'accepted'
record.property_id.selling_price = record.price
record.property_id.state = 'offer_accepted'
record.property_id.buyer_id = record.partner_id

def action_refuse(self):
self.status = 'refused'
return True

@api.model
def create(self, vals):
"""
Handles:
> avoid search inside loop
> validate in bulk
> update property state
"""
Comment on lines +58 to +62

Choose a reason for hiding this comment

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

The comment should be important.


vals_list = vals if isinstance(vals, list) else [vals]

property_offer_map = {}
for v in vals_list:
pid = v.get("property_id")
if pid:
property_offer_map.setdefault(pid, []).append(v)

for pid, offers in property_offer_map.items():
prices = [o.get("price") for o in offers if o.get("price") is not None]

if prices:
max_new_price = max(prices)

existing_offers = self.search([
("property_id", "=", pid),
("price", ">=", max_new_price),
], limit=1)

if existing_offers:
raise UserError(
"You cannot create an offer with a lower amount than an existing offer for this property."
)

records = super().create(vals)

if isinstance(records, models.Model):
for offer in records:
offer.property_id.state = "offer_received"

return records

_check_offer_price = models.Constraint(

Choose a reason for hiding this comment

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

Constraints should be after the field declaration.

'CHECK(price > 0)',
'The price of an offer must be strictly positive.'
)
15 changes: 15 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import fields, models


class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Real Estate Property Tag"
_order = "name"

name = fields.Char(required=True)
color = fields.Integer()

_check_tag_name_unique = models.Constraint(
'UNIQUE(name)',
'The name of the property tag must be unique.'
)
23 changes: 23 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from odoo import fields, models, api


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Real Estate Property Type"
_order = "sequence, name"

name = fields.Char(required=True)
property_ids = fields.One2many("estate.property", "property_type_id", string="Properties")
sequence = fields.Integer("Sequence", default=1)
offer_ids = fields.One2many("estate.property.offer", "property_type_id", string="Offers")
offer_count = fields.Integer(string="Offer 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)

_check_type_name_unique = models.Constraint(

Choose a reason for hiding this comment

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

Same here.

'UNIQUE(name)',
'The name of the property type must be unique.'
)
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_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag,estate.property.tag.access,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer,estate.property.offer.access,model_estate_property_offer,base.group_user,1,1,1,1
21 changes: 21 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<odoo>
<menuitem id="estate_menu_root" name="Real Estate" />
<menuitem id="menu_estate_advertisements"
name="Advertisements"
parent="estate_menu_root" />
<menuitem id="menu_estate_property_action"
name="Properties"
parent="menu_estate_advertisements"
action="action_estate_property" />
<menuitem id="menu_estate_settings"
name="Settings"
parent="estate_menu_root" />
<menuitem id="menu_estate_property_type_action_settings"
name="Property Types"
parent="menu_estate_settings"
action="estate_property_type_action" />
<menuitem id="menu_estate_property_tag_action_settings"
name="Property Tags"
parent="menu_estate_settings"
action="action_estate_property_tag" />
</odoo>
54 changes: 54 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<odoo>
<!-- Action -->
<record id="estate_property_offer_action" model="ir.actions.act_window">
<field name="name">Property Offers</field>
<field name="res_model">estate.property.offer</field>
<field name="view_mode">list,form</field>
<field name="domain">[('property_type_id', '=', active_id)]</field>
</record>

<!-- List View -->
<record id="estate_property_offer_list_view" model="ir.ui.view">
<field name="name">estate.property.offer.list</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<list decoration-danger="status == 'refused'"
decoration-success="status == 'accepted'"
editable="bottom">

<field name="price"/>
<field name="partner_id"/>
<field name="status"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="property_type_id"/>
</list>
</field>
</record>

<!-- Form View -->
<record id="estate_property_offer_form_view" model="ir.ui.view">
<field name="name">estate.property.offer.form</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="status"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="property_type_id"/>
</group>
</sheet>

<footer>
<button name="action_accept" type="object" string="Accept" class="btn-primary" />
<button name="action_refuse" type="object" string="Refuse" class="btn-secondary" />
</footer>
</form>
</field>
</record>

</odoo>
17 changes: 17 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<odoo>
<record id="action_estate_property_tag" model="ir.actions.act_window">
<field name="name">Property Tags</field>
<field name="res_model">estate.property.tag</field>
<field name="view_mode">list,form</field>
</record>

<record id="estate_property_tag_tree" model="ir.ui.view">
<field name="name">estate.property.tag.list</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<list string="Property Tags" editable="top">
<field name="name"/>
</list>
</field>
</record>
</odoo>
Loading