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
16 changes: 16 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Real estate",
"author": "Odoo",
"license": "LGPL-3",
"depends": ["base"],
"application": True,
"data": [
"security/ir.model.access.csv",
"views/estate_property_offer_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_tag_views.xml",
"views/estate_property_views.xml",
"views/res_user_views.xml",
"views/estate_menus.xml",
],
}
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
113 changes: 113 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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"
_order = "sequence, id desc"

name = fields.Char("Title", required=True)
sequence = fields.Integer(default=1)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
"Available From", default=lambda self: 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("Living Area (sqm)")
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer("Garden Area (sqm)")
garden_orientation = fields.Selection([("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")])
active = fields.Boolean(default=True)
state = fields.Selection(
[
("new", "New"),
("offer", "Offer Received"),
("accepted", "Offer Accepted"),
("sold", "Sold"),
("canceled", "Cancelled"),
],
default="new",
readonly=True,
)
property_type_id = fields.Many2one("estate.property.type")
salesman_id = fields.Many2one("res.users", copy=False, default=lambda self: self.env.user)
buyer_id = fields.Many2one("res.partner", copy=False, readonly=True)
tag_ids = fields.Many2many("estate.property.tag")
offer_ids = fields.One2many("estate.property.offer", "property_id")
total_area = fields.Integer("Total Area (sqm)", compute="_compute_total_area")
best_offer = fields.Float(compute="_compute_best_offer")

_check_expected_price = models.Constraint("CHECK(expected_price>0)", "The expected price needs to be bigger then 0")
_check_selling_price = models.Constraint("CHECK(selling_price>=0)", "The selling price needs to be positive")
Comment on lines +49 to +50
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sql Constraints should be above fields with other private properties.
Refer to this to check the order in a model:
https://www.odoo.com/documentation/18.0/contributing/development/coding_guidelines.html#symbols-and-conventions

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I was following the 19 guidelines where it was indicating that this is below the field declaration
https://www.odoo.com/documentation/19.0/contributing/development/coding_guidelines.html#symbols-and-conventions


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

@api.depends("offer_ids.price")
def _compute_best_offer(self):
for estate_property in self:
estate_property.best_offer = max(estate_property.offer_ids.mapped("price"), default=None)

@api.onchange("garden")
def _onchange_garden(self):
self.garden_area = 10 if self.garden else 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you realize why you don't need to iterate over the records in self in the onchange method like you did in the compute?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

because when calling this method it is done with a specific record that is changed, while with the compute it needs to be done on all the records that need to be shown which is a list of records

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One thing to add:
onchange methods only run in the form view. Therefore, you're always sure that it will run for only one record. (If the method is not called by us in a unit test)

self.garden_orientation = "north" if self.garden else None

@api.constrains("selling_price", "expected_price")
def _check_selling_price(self):
for estate_property in self:
if (
not float_is_zero(estate_property.selling_price, 2)
and float_compare(estate_property.selling_price, estate_property.expected_price * 0.9, 2) < 0
):
raise ValidationError("The property can not be sold for a price lower than 90% of the expected price")

@api.ondelete(at_uninstall=False)
def _unlink_property(self):
if any(estate_property.state not in ["new", "canceled"] for estate_property in self):
raise UserError("Only properties with state new or canceled can be removed!")

def action_state_to_sold(self):
self.ensure_one()
if self.state == "canceled":
raise UserError("Canceled properties can not be sold")
if not [offer for offer in self.offer_ids if offer.status == 'accepted']:
raise UserError("Properties can not be marked as sold if there is no accepted offer")
self.state = "sold"

def action_state_to_canceled(self):
self.ensure_one()
if self.state == "sold":
raise UserError("Sold properties can not be canceled")
self.state = "canceled"

def accept_offer(self, price, buyer):
self.ensure_one()
if self.state not in ["new", "offer"]:
raise UserError(
"An offer can can only be accepted when the building is still for sale and no other offer is already accepted"
)
self.selling_price = price
self.buyer_id = buyer
self.state = "accepted"

def offer_made(self, price):
self.ensure_one()
if self.state not in ["new", "offer"]:
raise UserError(
"An offer can can only be accepted when the building is still for sale and no other offer is already accepted"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

"An offer can only be made" probably

)
if float_compare(price, self.best_offer, 2) <= 0:
raise UserError("Only offers that are over the current best offer can be accepted")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't this check be in accept_offer?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

question was to only allow offers that are higher than previous made offers

self.state = "offer"
48 changes: 48 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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"
_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("Validity (days)", default=7)
date_deadline = fields.Date("Deadline", compute="_calculate_date_deadline", inverse="_inverse_date_deadline")
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)

_check_price = models.Constraint("CHECK(price>0)", "The selling price needs to be bigger then 0")
Comment thread
cgun-odoo marked this conversation as resolved.

@api.depends("validity", "create_date")
def _calculate_date_deadline(self):
for offer in self:
offer.date_deadline = (offer.create_date or fields.Date.today()) + relativedelta(days=offer.validity)

def _inverse_date_deadline(self):
for offer in self:
offer.validity = (offer.date_deadline - fields.Date.today()).days

@api.model
def create(self, vals_list):
for vals in vals_list:
self.env["estate.property"].browse(vals["property_id"]).offer_made(vals["price"])
return super().create(vals_list)

def action_accept_offer(self):
self.ensure_one()
if self.status:
raise UserError("The offer was already revised")
self.property_id.accept_offer(self.price, self.partner_id)
self.status = "accepted"

def action_refuse_offer(self):
self.ensure_one()
if self.status:
raise UserError("The offer was already revised")
self.status = "refused"
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 = "Estate Property Tag"
_order = "name"

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

_unique_name = models.UniqueIndex(
"(name)",
"Another entry with the same name already exists.",
)
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 = "Estate property type"
_order = "name"

name = fields.Char(required=True)
property_ids = fields.One2many("estate.property", "property_type_id")
offer_ids = fields.One2many("estate.property.offer", "property_type_id")
offer_count = fields.Integer(compute="_compute_offer_count")

_unique_name = models.UniqueIndex(
"(name)",
"Another entry with the same name already exists.",
)

@api.depends("offer_ids")
def _compute_offer_count(self):
for property_type in self:
property_type.offer_count = len(property_type.offer_ids)
7 changes: 7 additions & 0 deletions estate/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from odoo import fields, models


class ResUsers(models.Model):
_inherit = "res.users"

property_ids = fields.One2many("estate.property", "salesman_id")
6 changes: 6 additions & 0 deletions estate/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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,0
access_estate_property_offer_admin,access_estate_property_offer_admin,model_estate_property_offer,base.group_system,1,1,1,1
2 changes: 2 additions & 0 deletions estate/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import test_estate_property
from . import test_estate_property_offer
33 changes: 33 additions & 0 deletions estate/tests/test_estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from odoo.exceptions import UserError
from odoo.tests import TransactionCase, tagged, Form


@tagged('post_install', '-at_install')
class TestEstateProperty(TransactionCase):

@classmethod
def setUpClass(cls):
super().setUpClass()

def test_mark_as_sold_without_offer(self):
self.property = self.env['estate.property'].create({
'name': 'property1',
'expected_price': 100,
'state': 'offer'
})
with self.assertRaises(UserError):
self.property.action_state_to_sold()

def test_reset_garden(self):
self.property = self.env['estate.property'].create({
'name': 'property',
'expected_price': 100,
'garden': True,
'garden_area': 20,
'garden_orientation': 'north'
})
with Form(self.property) as f1:
f1.garden = False
self.assertFalse(self.property.garden, 'the garden should not be there anymore')
self.assertEqual(self.property.garden_area, 0, "garden area should be reset")
self.assertFalse(self.property.garden_orientation, "garden orientation is expected to be unset")
42 changes: 42 additions & 0 deletions estate/tests/test_estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from odoo.exceptions import UserError
from odoo.tests import TransactionCase, tagged


@tagged('post_install', '-at_install')
class TestEstatePropertyOffer(TransactionCase):

@classmethod
def setUpClass(cls):
super().setUpClass()

cls.buyer = cls.env['res.partner'].create({'name': 'buyer'})

def test_good_offer(self):
self.property = self.env['estate.property'].create({
'name': 'property1',
'expected_price': 100,
})
self.env['estate.property.offer'].create({
'price': 90,
'partner_id': self.buyer.id,
'property_id': self.property.id,
})
self.assertRecordValues(self.property.offer_ids, [
{'price': 90, 'partner_id': self.buyer.id}
])
self.assertEqual(self.property.state, "offer")

def test_offer_for_sold(self):
self.property = self.env['estate.property'].create({
'name': 'property1',
'expected_price': 100,
'state': 'sold'
})
with self.assertRaises(UserError):
self.env['estate.property.offer'].create({
'price': 90,
'partner_id': self.buyer.id,
'property_id': self.property.id,
})
self.assertFalse(self.property.offer_ids, "after failure there should be no offer added")
self.assertEqual(self.property.state, "sold", "after failure the state should not change")
11 changes: 11 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<odoo>
<menuitem id="estate_menu_root" name="Real Estate">
<menuitem id="estate_advertisement_menu" name="Advertisement">
<menuitem id="estate_property_menu" name="Properties" action="estate_property_action"/>
</menuitem>
<menuitem id="estate_settings_menu" name="Settings">
<menuitem id="estate_property_type" name="Property Types" action="estate_property_type_action"/>
<menuitem id="estate_property_tag" name="Property Tags" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
25 changes: 25 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<odoo>
<record id="estate_property_offer_view_list" model="ir.ui.view">
<field name="name">estate.property.offer.view.list</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<list string="Offers" editable="bottom"
decoration-success="status == 'accepted'"
decoration-danger="status == 'refused'">
<field name="price"/>
<field name="partner_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="action_accept_offer" type="object" title="Accept" icon="fa-check" invisible="status"/>
<button name="action_refuse_offer" type="object" title="Refuse" icon="fa-times" invisible="status"/>
</list>
</field>
</record>

<record id="estate_property_offer_action" model="ir.actions.act_window">
<field name="name">Offers</field>
<field name="res_model">estate.property.offer</field>
<field name="view_mode">list</field>
<field name="domain">[('property_type_id', '=', active_id)]</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="estate_property_tag_view_list" model="ir.ui.view">
<field name="name">estate.property.tag.view.list</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<list string="Tags" editable="bottom">
<field name="name"/>
</list>
</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</field>
</record>
</odoo>
Loading