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
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/

# Perso
README.md
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ tutorial's solutions, and one for the
[Master the Odoo web framework](https://www.odoo.com/documentation/latest/developer/tutorials/master_odoo_web_framework.html)
tutorial's solutions. For example, `17.0`, `17.0-discover-js-framework-solutions` and
`17.0-master-odoo-web-framework-solutions`.

Comment thread
AarnorDeDardaliel marked this conversation as resolved.

./odoo-bin --addons-path="addons/,../enterprise/,../tutorials" -d rd-demo -u estate --dev xml

dropdb rd-demo
createdb rd-demo
psql -d rd-demo


lsof -i :8069
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
20 changes: 20 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
'name': 'My Estate',
'version': '1.9',
'summary': 'Test module to remember how it works mdr',
'website': 'https://www.odoo.com/app/estate',
'depends': [
'base',
],
'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_menus.xml',
],
'installable': True,
'application': True,
'author': 'brbu',
Comment thread
AarnorDeDardaliel marked this conversation as resolved.
}
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
111 changes: 111 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 = "Properties of the estate"
_order = "id desc"

name = fields.Char(required=True, string="Title")
description = fields.Text()
tag_ids = fields.Many2many("estate.property.tag", string="Tags")

property_type_id = fields.Many2one("estate.property.type", string="Property Type")
salesperson_id = fields.Many2one("res.users", default=lambda self: self.env.user)
buyer_id = fields.Many2one("res.partner", copy=False)
offer_ids = fields.One2many("estate.property.offer", "property_id", copy=False)
best_offer = fields.Float(compute="_compute_best_offer", string="Best Offer", store=True)

postcode = fields.Char()
date_availability = fields.Date(copy=False, string="Available From", default=lambda self: fields.Date.add(fields.Date.today(), months=3))
expected_price = fields.Float(required=True, string="Expected Price")
selling_price = fields.Float(readonly=True, string="Selling Price", copy=False)

bedrooms = fields.Integer(default=2)
living_area = fields.Integer(string="Living Area (sqm)")
facade = fields.Integer(string="Façade")
garage = fields.Boolean(string="Garage")
garden = fields.Boolean(string="Garden")
garden_area = fields.Integer(string="Garden Area (sqm)")
garden_orientation = fields.Selection(
string="Garden Orientation",
selection=[("north", "North"), ("south", "South"), ("east", "East"), ("west", "West")],
)
total_area = fields.Integer(string="Total Area (sqm)", compute="_compute_total_area", store=True)

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

_positive_expected_price = models.Constraint(
'CHECK(expected_price > 0)',
'The expected price should be strictly positive.',
)

_positive_selling_price = models.Constraint(
'CHECK(selling_price >= 0)',
'The selling price should be strictly positive.',
)

@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.9, 2) == -1:
raise ValidationError(_("The selling price must be at least 90% of the expected price !"))

@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", "offer_ids.status")
def _compute_best_offer(self):
for record in self:
valid_offers = record.offer_ids.filtered(lambda offer: offer.status != "refused")
record.best_offer = max(valid_offers.mapped("price"), default=0)
if valid_offers and record.state=="new":
record.state = "offer_received"

def _update_state_from_offers(self):
for record in self:
if record.offer_ids.filtered(lambda offer: offer.status == "accepted"):
record.state = "offer_accepted"
elif any(not offer.status for offer in record.offer_ids):
record.state = "offer_received"
else:
record.state = "new"

@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

@api.ondelete(at_uninstall=False)
def _only_if_new_or_cancelled(self):
for record in self:
if record.state != 'new' and record.state != 'cancelled':
raise UserError("Can't delete property that is not new or cancelled !")

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

def action_cancelled(self):
for record in self:
if record.state == "sold":
raise UserError("A sold property cannot be cancelled !")
record.state = "cancelled"
return True
66 changes: 66 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from odoo import models, fields, api

class EstatePropertyOffer(models.Model):
_name = "estate.property.offer"
_description = "Property offers for the estate"
_order = "price desc"

price = fields.Float()
status = fields.Selection(
string="Status",
selection=[("accepted", "Accepted"), ("refused", "Refused")],
copy=False,
readonly=True,
)
validity = fields.Integer(string="Validity (days)", default=7)
date_deadline = fields.Date(string="Deadline", compute="_compute_date_deadline", inverse="_inverse_date_deadline", store=True)

partner_id = fields.Many2one("res.partner", copy=False)
property_id = fields.Many2one("estate.property", string="Property", copy=False)
property_type_id = fields.Many2one(related="property_id.property_type_id", store=True)

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

@api.depends("date_deadline", "create_date")
def _inverse_date_deadline(self):
for record in self:
record.validity = (record.date_deadline - record.create_date.date()).days

def action_accept(self):
for record in self:
for other_offer in record.property_id.offer_ids:
if other_offer.status == "accepted":
other_offer.status = False
raise Warning("An other offer acceptation was cancelled : only one offer can be accepted at a time !")
record.status = "accepted"
record.property_id.buyer_id = record.partner_id
record.property_id.selling_price = record.price
record.property_id._update_state_from_offers()
return True

def action_reset(self):
for record in self:
if record.status == "accepted":
record.property_id.buyer_id = False
record.property_id.selling_price = 0
record.status = False
record.property_id._update_state_from_offers()
return True

def action_refuse(self):
for record in self:
if record.status == "accepted":
record.property_id.buyer_id = False
record.property_id.selling_price = 0
record.status = "refused"
record.property_id._update_state_from_offers()
return True

_positive_offer_price = models.Constraint(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

try to keep the constraint bellow the field.

usually we do:

  • fields
  • constraints
  • compute
  • internal function
  • action
  • the rest

'CHECK(price > 0)',
'The offer prices should be strictly positive.',
)
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 models, fields

class EstatePropertyTag(models.Model):
_name = "estate.property.tag"
_description = "Property tags for the estate"
_order = "name"

name = fields.Char(required=True)
color = fields.Integer('Color Index', default=0)

_name_uniq = models.Constraint(
'unique(name)',
'The nae must be unique.',
)
24 changes: 24 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from odoo import models, fields, api


class EstatePropertyType(models.Model):
_name = "estate.property.type"
_description = "Property types of the estate"
_order = "sequence, name"

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

sequence = fields.Integer('Sequence', default=1, help="Used to order stages. Lower is better.")

_name_uniq = models.Constraint(
'unique(name)',
'The name must be unique.',
)

@api.depends("offer_ids")
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)
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" encoding="UTF-8"?>
<odoo>
<menuitem id="estate_menus_root" name="Real Estate">
<menuitem id="estate_menus_ads" name="Advertisements">
<menuitem id="estate_menus_property_action" action="estate_property_action"/>
</menuitem>
<menuitem id="estate_menus_settings" name="Settings">
<menuitem id="estate_menus_property_types_action" action="estate_property_type_action"/>
<menuitem id="estate_menus_property_tag_action" action="estate_property_tag_action"/>
</menuitem>
</menuitem>
</odoo>
56 changes: 56 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<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" editable="bottom"
decoration-success="status=='accepted'"
decoration-danger="status=='refused'">
<field name="price"/>
<field name="property_type_id"/>
<field name="validity"/>
<field name="date_deadline"/>
<button name="action_reset" string="Reset" type="object" icon="fa-circle-o-notch" invisible="not status"/>
<button name="action_accept" string="Accept" type="object" icon="fa-check" invisible="status"/>
<button name="action_refuse" string="Refuse" type="object" icon="fa-times" 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>
<field name="price"/>
<field name="partner_id"/>
<field name="status"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_offer_search" model="ir.ui.view">
<field name="name">estate.property.offer.search</field>
<field name="model">estate.property.offer</field>
<field name="arch" type="xml">
<search string="Property Offers">
<field name="property_type_id"/>
<field name="partner_id"/>
</search>
</field>
</record>

<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>

</odoo>
46 changes: 46 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record id="estate_property_tag_view_list" 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="Properties" 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"/>
<field name="color" widget="color_picker"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="estate_property_tag_search" model="ir.ui.view">
<field name="name">estate.property.tag.search</field>
<field name="model">estate.property.tag</field>
<field name="arch" type="xml">
<search string="Estate Property Tag">
<field name="name"/>
</search>
</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>
Loading