Skip to content
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# VScode
.vscode/
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
18 changes: 18 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
'name': 'Real Estate',
'description': 'Real Estate Advertising',
# 'version': '1.0',
'author': 'robal',
'license': 'LGPL-3',
'depends': ['base'],
'application': True,
'data': [
'security/ir.model.access.csv',

"views/estate_property_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_offer_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_offer
from . import estate_property_tag
from . import estate_property_type
from . import estate_property
112 changes: 112 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
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 = "Estate property"

name = fields.Char(string="Title", required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(string="Available From", default=fields.Date.today() + relativedelta(months=3), copy=False)
expected_price = fields.Float(required=True)
selling_price = fields.Float(readonly=True, copy=False)
bedrooms = fields.Integer(default=2)
living_area = fields.Integer(string="Living Area (sqm)")
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer(string="Garden Area (sqm)")
garden_orientation = fields.Selection(
selection=[("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")],
)

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

# Relations
property_type_id = fields.Many2one("estate.property.type", string="Property Type")
salesman_id = fields.Many2one("res.users", default=lambda self: self.env.user)
buyer_id = fields.Many2one("res.partner", copy=False)
tag_ids = fields.Many2many("estate.property.tag")
offer_ids = fields.One2many("estate.property.offer", "property_id")

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

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

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

# Methods that trigger on changes
@api.onchange("garden")
def _compute_garden_defaults(self) -> None:
if self.garden:
self.garden_area = 10
self.garden_orientation = "north"
else:
self.garden_area = None
self.garden_orientation = None

# Public methods
def action_set_sold(self) -> bool:
for record in self:
if record.state == "sold":
error_msg = "This property has already been sold."
raise UserError(error_msg)

if record.state == "cancelled":
error_msg = "Cancelled properties cannot be sold."
raise UserError(error_msg)

record.state = "sold"
return True

def action_set_cancelled(self) -> bool:
for record in self:
if record.state == "sold":
error_msg = "Sold properties cannot be cancelled."
raise UserError(error_msg)

if record.state == "cancelled":
error_msg = "This property has already been cancelled."
raise UserError(error_msg)

record.state = "cancelled"
return True

# Constraints
_expected_price_strict_positive = models.Constraint(
"CHECK(expected_price > 0)",
"A property's expected price must be strictly greater than 0.",
)

_selling_price_positive = models.Constraint(
"CHECK(selling_price >= 0)",
"A property's selling price must be equal to or greater than 0.",
)

@api.constrains("expected_price", "selling_price")
def _restrict_selling_price(self) -> None:
for record in self:
# Selling price is zero when no offer has been accepted
if not float_is_zero(record.selling_price, precision_digits=2) and float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0:
error_msg = "A property's selling price cannot be lower that 90 percent of its expected price."
raise ValidationError(error_msg)
68 changes: 68 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from datetime import date

from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
from odoo.exceptions import UserError


class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
_description = "Estate property offer"

price = fields.Float()
status = fields.Selection(
selection=[("accepted", "Accepted"), ("refused", "Refused")],
copy=False,
)
validity = fields.Integer(default=7, string="Validity (days)")

# Relations
partner_id = fields.Many2one("res.partner", required=True)
property_id = fields.Many2one("estate.property", required=True)

# Computed
date_deadline = fields.Date(compute="_compute_deadline", inverse="_inverse_deadline", string="Deadline")

@api.depends("create_date", "validity")
def _compute_deadline(self) -> None:
for record in self:
record.date_deadline = _get_reference_date(record) + relativedelta(days=record.validity)

def _inverse_deadline(self) -> None:
for record in self:
record.validity = (record.date_deadline - _get_reference_date(record)).days

# Public methods
def action_accept(self) -> bool:
for record in self:
if record.status == "accepted":
error_msg = "This offer has already been accepted."
raise UserError(error_msg)

if record.property_id.offer_ids.filtered(lambda r: r.status == "accepted"):
error_msg = "Cannot accept this offer because another offer has already been accepted for the property."
raise UserError(error_msg)

record.status = "accepted"
record.property_id.selling_price = record.price
record.property_id.buyer_id = record.partner_id
return True

def action_refuse(self) -> bool:
for record in self:
if record.status == "accepted":
record.property_id.selling_price = 0
record.property_id.buyer_id = None

record.status = "refused"
return True

# Constraints
_price_strict_positive = models.Constraint(
"CHECK(price > 0)",
"An offer's price must be strictly greater than 0.",
)


def _get_reference_date(offer: EstatePropertyOffer) -> date:
return fields.Date.today() if not offer.create_date else offer.create_date.date()
14 changes: 14 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import fields, models


class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Estate property tag"

name = fields.Char(required=True)

# Constraints
_uniq_name = models.Constraint(
"UNIQUE(name)",
"A property tag's name must be unique.",
)
14 changes: 14 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import fields, models


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Estate property type"

name = fields.Char(required=True)

# Constraints
_uniq_name = models.Constraint(
"UNIQUE(name)",
"A property type's name 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
access_estate_property,access_estate_property,model_estate_property,base.group_user,1,1,1,1
access_estate_property_type,access_estate_property_type,model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag,access_estate_property_tag,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer,access_estate_property_offer,model_estate_property_offer,base.group_user,1,1,1,1
12 changes: 12 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<odoo>
<menuitem id="estate_menu_root" name="Real Estate">
<menuitem id="estate_properties_first_level" name="Properties">
<menuitem id="estate_properties_menu" action="estate_property_action"/>
</menuitem>
<menuitem id="estate_settings_first_level" name="Settings">
<menuitem id="estate_property_types_menu" action="estate_property_type_action"/>
<menuitem id="estate_property_tags_menu" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
36 changes: 36 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0"?>
<odoo>
<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>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="action_accept" type="object" icon="fa-check" string="Accept"/>
<button name="action_refuse" type="object" icon="fa-times" string="Refuse"/>
<field name="status"/>
</list>
</field>
</record>

<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"/>
</group>
</sheet>
</form>
</field>
</record>
</odoo>
32 changes: 32 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_tag_list_view" 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>
<field name="name"/>
</list>
</field>
</record>

<record id="estate_property_tag_form_view" model="ir.ui.view">
<field name="name">estate.property.tag.form</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="name"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_tag_action" 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>
</odoo>
32 changes: 32 additions & 0 deletions estate/views/estate_property_type_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_type_list_view" model="ir.ui.view">
<field name="name">estate.property.type.list</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<list>
<field name="name"/>
</list>
</field>
</record>

<record id="estate_property_type_form_view" model="ir.ui.view">
<field name="name">estate.property.type.form</field>
<field name="model">estate.property.type</field>
<field name="arch" type="xml">
<form>
<sheet>
<h1>
<field name="name"/>
</h1>
</sheet>
</form>
</field>
</record>

<record id="estate_property_type_action" model="ir.actions.act_window">
<field name="name">Property Types</field>
<field name="res_model">estate.property.type</field>
<field name="view_mode">list,form</field>
</record>
</odoo>
Loading