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
129 changes: 0 additions & 129 deletions .gitignore

This file was deleted.

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
16 changes: 16 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# __manifest__.py
{ # noqa: B018
"author": "Odoo S.A.",
"name": "estate",
"depends": ["base"],
"application": True,
"license": "LGPL-3",
"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
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
102 changes: 102 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from odoo.tools.date_utils import add
from odoo.tools.float_utils import float_compare, float_is_zero
from odoo.exceptions import UserError, ValidationError
from odoo import api, fields, models


class EstateProperty(models.Model):
_name = "estate.property"
_description = "Estate property model"

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

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(copy=False, default=lambda self: add(fields.Date.today(), months=3), string="Available From")
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()
has_garage = fields.Boolean()
has_garden = fields.Boolean()
garden_area = fields.Integer(string="Garden Area (sqm)")
garden_orientation = fields.Selection(
string='Type',

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
string='Type',
string='Garden Orientation',

selection=[
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West')
]
)
Comment on lines +32 to +40

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Having the selection list in one line is okay but when we have such multiple values we style it like this.
Also it's better to have the keys of the selection tuples to be all lower case letters to avoid confusion when used later in the code.

Suggested change
garden_orientation = fields.Selection(
string='Type',
selection=[('North', 'North'), ('South', 'South'), ('East', 'East'), ('West', 'West')]
)
garden_orientation = fields.Selection(
string='Garden Orientation',
selection=[
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West')
]
)

property_type_id = fields.Many2one("estate.property.type", string="Property Type")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

stick to double quotes or single quotes :)

buyer = fields.Many2one('res.partner', copy=False)
salesperson = fields.Many2one('res.users', default=lambda self: self.env.user, string="Salesman")
tag_ids = fields.Many2many('estate.property.tag')
offer_ids = fields.One2many('estate.property.offer', 'property_id', string="Offers")
total_area = fields.Integer(compute="_compute_total_area", string="Total Area (sqm)")
best_price = fields.Float(compute="_compute_best_offer", string="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")
def _compute_best_offer(self):
for record in self:
prices = record.offer_ids.mapped("price")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There will be an issue in case some properties don't have offer_ids.
The if statement you have should check on the offer_ids

record.best_price = max(prices) if prices else 0.0

@api.onchange("has_garden")
def _onchange_has_garden(self):
if self.has_garden:
self.garden_area = 10
self.garden_orientation = "north"
else:
self.garden_area = 0
self.garden_orientation = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

okay, we conventionally use False


def mark_order_as_sold(self):
for record in self:
if record.state == "cancelled":
Comment on lines +70 to +71

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When we can write logic on the whole recordset instead of looping over each record, we do that. It's better for reading and writing through the database. For example here you can check if any of self has a state cancelled and in that case you raise the error, you can also write the state to all of them at once.

raise UserError("Cancelled properties cannot be sold")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

add this _ imported from odoo to support translation as that string is shown to the user

Suggested change
raise UserError("Cancelled properties cannot be sold")
raise UserError(_("Cancelled properties cannot be sold"))

else:
record.state = "sold"
return True

def mark_order_as_cancelled(self):
for record in self:
if record.state == "sold":
raise UserError("Sold properties cannot be cancelled")
else:
record.state = "cancelled"
return True
Comment on lines +78 to +83

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same comments apply here for recordset handling (instead of per record) and the translation thing :)


@api.constrains("selling_price", "expected_price")
def _check_selling_price(self):
for record in self:
if float_is_zero(record.selling_price, precision_digits=2):
continue

if float_compare(record.selling_price, 0.9 * record.expected_price, precision_digits=2) < 0:
raise ValidationError("The selling price must be at leat 90% of the expected price ! You must reduce the expected price if you want to accept this order !")

_expected_price_strictly_positive_constraint = models.Constraint(
"CHECK(expected_price > 0)",
"The expected price should be strictly greater than 0!"
)

_selling_price_positive_constraint = models.Constraint(
"CHECK(selling_price >= 0)",
"The selling price should be greater or equal to 0!"
)
55 changes: 55 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from odoo.tools.date_utils import add
from odoo.exceptions import UserError
from odoo import api, fields, models


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

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

@api.depends("create_date", "validity")
def _compute_date_deadline(self):
for record in self:
base_date = record.create_date.date() if record.create_date else fields.Date.today()
record.date_deadline = add(base_date, days=record.validity)

def _inverse_date_deadline(self):
for record in self:
base_date = record.create_date.date() if record.create_date else fields.Date.today()
if record.date_deadline and base_date:
record.validity = (record.date_deadline - base_date).days

def accept_offer(self):
for record in self:

if record.status == "accepted":
continue

accepted_offer = record.property_id.offer_ids.filtered(lambda offer: offer.status == "accepted")
if accepted_offer:
raise UserError("Only one offer can be accepted for a giver property !")
else:
record.status = "accepted"
record.property_id.buyer = record.partner_id
record.property_id.selling_price = record.price
Comment on lines +41 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you can remove the else, the error raised if the condition is true will prevent the rest of the code from running

Suggested change
else:
record.status = "accepted"
record.property_id.buyer = record.partner_id
record.property_id.selling_price = record.price
record.status = "accepted"
record.property_id.buyer = record.partner_id
record.property_id.selling_price = record.price

return True

def refuse_offer(self):
for record in self:
record.status = "refused"
Comment on lines +48 to +49

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you can do self.state = "refused"

return True

_offer_price_strictly_positive_constraint = models.Constraint(
"CHECK(price > 0)",
"The price of an offer should be strictly greater than 0!"
)
13 changes: 13 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from odoo import fields, models


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

name = fields.Char(required=True)

_property_tag_unique_constraint = models.Constraint(
"UNIQUE(name)",
"Property tag should have an unique name!"
)
8 changes: 8 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from odoo import fields, models


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

name = fields.Char(required=True)
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,estate_property,model_estate_property,base.group_user,1,1,1,1
access_estate_property_type,estate_property_type,model_estate_property_type,base.group_user,1,1,1,1
access_estate_property_tag,estate_property_tag,model_estate_property_tag,base.group_user,1,1,1,1
access_estate_property_offer,estate_property_offer,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>
<menuitem id="estate_menu_root" name="Real Estate">
<menuitem id="estate_advertisements_menu" name="Advertisements">
<menuitem id="estate_property_menu_action" action="estate_property_action"/>
</menuitem>
<menuitem id="estate_settings_menu" name="Settings">
<menuitem id="estate_property_settings_menu_type_action" action="estate_property_type_action"/>
<menuitem id="estate_property_settings_menu_tag_action" action="estate_property_tag_action"/>
</menuitem>
</menuitem>

</odoo>
43 changes: 43 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0"?>
<odoo>
<record id="estate_property_offer_view_form" 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 string="Property">
<header>
<button name="accept_offer" type="object" string="Accept"/>
<button name="refuse_offer" type="object" string="Refuse"/>
</header>
<sheet>
<h1>
<field name="property_id"/>
</h1>
<group>
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<field name="status"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_offer_view_list" 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 string="Properties">
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="accept_offer" type="object" title="Accept" icon="fa-check"/>
<button name="refuse_offer" type="object" title="Refuse" icon="fa-times"/>
<field name="status"/>
</list>
</field>
</record>
</odoo>
Loading