Skip to content
Open
Show file tree
Hide file tree
Changes from 18 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
21 changes: 21 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
'name': 'Real Estate',
'license': 'LGPL-3',
'version': '1.0',
'depends': ['base'],
'author': 'Odoo S.A.',
'category': 'Category',
'description': """
Real Estate Advertisement module
""",
'application': True,
'data': [
'security/ir.model.access.csv',
'views/estate_property_views.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_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_type
from . import estate_property_tag
from . import estate_property_offer
from . import estate_user
121 changes: 121 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools.float_utils import float_is_zero, float_compare


class EstateProperty(models.Model):
_name = 'estate.property'
_description = 'All properties'
_order = 'id desc'

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
copy=False,
default=fields.Date.add(fields.Date.today(), months=3),
)
expected_price = fields.Float(required=True)
selling_price = fields.Float(readonly=True, copy=False)
best_price = fields.Float(compute='_compute_best_price')
bedrooms = fields.Integer(default=2)
living_area = fields.Integer()
garden_area = fields.Integer()
total_area = fields.Integer(compute='_compute_total_area')
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_orientation = fields.Selection(
string='Garden orientation',
selection=[
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West'),
],
)
active = fields.Boolean(default=True)
state = fields.Selection(
string='State',
required=True,
default='new',
selection=[
('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'),
('cancelled', 'Cancelled'),
],
)
property_type_id = fields.Many2one(
'estate.property.type', string='Property Type',
)
buyer_id = fields.Many2one(
'res.partner', string='Buyer', copy=False,
)
salesperson_id = fields.Many2one(
'res.users', string='Salesperson',
default=lambda self: self.env.user,
)
tag_ids = fields.Many2many('estate.property.tag', string='Tags')
offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers')

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

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

@api.depends('offer_ids')
def _compute_best_price(self):
for record in self:
offers = record.offer_ids or []
record.best_price = max(offers.mapped('price')) if len(offers) else 0

@api.onchange('garden')
def _onchange_garden(self):
if self.garden:
self.garden_area = 10
self.garden_orientation = 'north'
else:
self.garden_area = None
self.garden_orientation = None

@api.constrains('selling_price', 'expected_price')
def _check_selling_price(self):
for record in self:
if (
not float_is_zero(record.selling_price, 2)
and float_compare(record.selling_price, record.expected_price * 0.90, 2) < 0
):
raise ValidationError("Selling price cannot be lower than 90% of 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(f"Can't delete property in {record.state} state")

def action_mark_as_sold(self):
for record in self:
if record.state == 'cancelled':
raise UserError('Property is already cancelled')

record.state = 'sold'

def action_mark_as_cancelled(self):
for record in self:
if record.state == 'sold':
raise UserError('Property is already sold')

record.state = 'cancelled'
71 changes: 71 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from odoo import api, fields, models
from odoo.exceptions import UserError


class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = 'All offers'
_order = 'price desc'

price = fields.Float(required=True)
status = fields.Selection(
string='Status',
selection=[
('accepted', 'Accepted'),
('refused', 'Refused'),
],
)
partner_id = fields.Many2one('res.partner', string='Partner')
property_id = fields.Many2one('estate.property', string='Property')
validity = fields.Integer(default=7)
date_deadline = fields.Date(compute='_compute_deadline', inverse="_inverse_deadline")
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)

_check_price = models.Constraint(
'CHECK(price > 0)',
'The price of an offer should be strictly positive.',
)

@api.depends('create_date', 'validity')
def _compute_deadline(self):
for record in self:
created_date = record.create_date or fields.Date.today()
record.date_deadline = fields.Date.add(
created_date, days=record.validity,
)

@api.model
def create(self, val_lists):
for vals in val_lists:
linked_property = self.env['estate.property'].browse(vals['property_id'])
if vals['price'] < linked_property.best_price:
raise UserError('Offer price cannot be lower than existing offers')

linked_property.state = 'offer_received'
return super().create(val_lists)

def _inverse_deadline(self):
for record in self:
created_date = record.create_date.date() or fields.Date.today()
record.validity = (record.date_deadline - created_date).days

def action_mark_as_accepted(self):
for record in self:
if (
record.property_id.state != 'new'
and record.property_id.state != 'offer_received'
):
raise UserError('Cannot accept offer in this state')

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

def action_mark_as_refused(self):
for record in self:
if record.status == 'accepted':
record.property_id.state = 'offer_received'
record.property_id.selling_price = None
record.property_id.buyer_id = None
record.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 = 'All property tags'
_order = 'name asc'

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

_check_unique_name = models.Constraint(
'unique (name)',
'The name of a tag should be unique.',
)
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 = 'All property types'
_order = 'name asc'

name = fields.Char(required=True)
property_ids = fields.One2many(
'estate.property', 'property_type_id', string="Properties",
)
sequence = fields.Integer('Sequence', default=1, help="Used to order property types.")
offer_ids = fields.One2many(
'estate.property.offer', 'property_type_id', string="Offers",
)
offer_count = fields.Integer(compute="_compute_offer_count")

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


class EstateUser(models.Model):
_inherit = 'res.users'

property_ids = fields.One2many(
'estate.property', 'salesperson_id', string='Real Estate Properties',
)
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
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="menu_root" name="Real Estate">
<menuitem id="advertisement" name="Advertisement">
<menuitem id="estate_property_menu_action" action="estate_property_action"/>
</menuitem>
<menuitem id="settings" name="Settings">
<menuitem id="estate_property_type_menu_action" action="estate_property_type_action"/>
<menuitem id="estate_property_tag_menu_action" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
71 changes: 71 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<odoo>
<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>

<record id="estate_property_offer_view_tree" 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="Offers"
editable="bottom"
decoration-success="status == 'accepted'"
decoration-danger="status == 'refused'"
>
<field name="price"/>
<field name="partner_id"/>
<field name="validity" string="Validity (days)"/>
<field name="date_deadline" string="Deadline"/>
<button
name="action_mark_as_accepted"
type="object"
icon="fa-check"
title="accept"
invisible="status"
/>
<button
name="action_mark_as_refused"
type="object"
icon="fa-times"
title="refuse"
invisible="status"
/>
</list>
</field>
</record>

<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 Offer">
<sheet>
<group>
<group>
<field name="property_id"/>
</group>
<group>
<field name="partner_id"/>
</group>
<group>
<field name="price"/>
</group>
<group>
<field name="status"/>
</group>
<group>
<field name="validity" string="Validity (days)"/>
</group>
<group>
<field name="date_deadline" string="Deadline"/>
</group>

Choose a reason for hiding this comment

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

You don't have to put each field alone in a group. Am I missing something?

Copy link
Author

Choose a reason for hiding this comment

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

Seems like we don't. This is the code block we used as example in Chapter 6, using group for each fields.

Codeblock

I tried having the fields in 1 group and everything works fine.

</group>
</sheet>
</form>
</field>
</record>
</odoo>
35 changes: 35 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<odoo>
<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>

<record id="estate_property_tag_view_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 Types" editable="bottom">
<field name="name"/>
<field name="color" widget="color_picker"/>
</list>
</field>
</record>

<record id="estate_property_tag_view_form" 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 string="Property Tag">
<sheet>
<group>
<field name="name"/>
</group>
<group>
<field name="color" widget="color_picker"/>
</group>
</sheet>
</form>
</field>
</record>
</odoo>
Loading