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
9 changes: 9 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from "@odoo/owl";

export class Card extends Component {
static template = "awesome_owl.card";
static props = {
title: String,
content: String,
}
}
14 changes: 14 additions & 0 deletions awesome_owl/static/src/card/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<templates xml:space="preserve">
<t t-name="awesome_owl.card">
<div class="card d-inline-block m-2" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">
<t t-out="props.title"/>
</h5>
<p class="card-text">
<t t-out="props.content"/>
</p>
</div>
</div>
</t>
</templates>
13 changes: 13 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Component, useState } from "@odoo/owl";

export class Counter extends Component {
static template = "awesome_owl.counter";

setup() {
this.state = useState({ value: 0});
}

increment() {
this.state.value++;
}
}
8 changes: 8 additions & 0 deletions awesome_owl/static/src/counter/counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<templates xml:space="preserve">

<t t-name="awesome_owl.counter">
Component Counter: <t t-esc="state.value"/>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</t>

</templates>
15 changes: 14 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { Component } from "@odoo/owl";
import { Component, markup, useState } from "@odoo/owl";
import { Counter } from "./counter/counter";
import { Card } from "./card/card"

export class Playground extends Component {
static template = "awesome_owl.playground";
static components = { Counter, Card };

setup() {
this.state = useState({ value: 0});
this.str1 = "<div class='text-primary'>some content</div>";
this.str2 = markup("<div class='text-primary'>some content</div>");
}

increment() {
this.state.value++;
}
}
9 changes: 6 additions & 3 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
</div>
First Counter: <t t-esc="state.value"/>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
<Counter/>
<Card title="'My beautiful card'" content="'This card is really beautiful'"/>
<Card title="'HTML card'" content="str1"/>
<Card title="'Markup card'" content="str2"/>
</t>

</templates>
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": "Estate",
"author": "sever",
"depends": [
"base",
],
"application": True,
"license": "LGPL-3",
"data": [
"views/estate_property_tag_views.xml",
"views/estate_property_type_views.xml",
"views/estate_property_offer_views.xml",
"views/estate_property_views.xml",
"views/estate_property_users_view.xml",
"views/estate_menus.xml",
"security/ir.model.access.csv",
],
}
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 # noqa: I001
from . import estate_property_type
from . import estate_property_tag
from . import estate_property_offer
from . import user_property
110 changes: 110 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 Module"
_order = "id desc"

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

_check_expected_price = models.Constraint(
"CHECK(expected_price > 0)",
"The expected price must be strictly positive.",
)
_check_selling_price = models.Constraint(
"CHECK(selling_price >= 0)",
"The selling price must be positive",
)

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

@api.depends("offer_ids.price")
def _compute_best_price(self):
for property in self:
property.best_price = (
max(property.offer_ids.mapped("price")) if len(property.offer_ids) > 0 else 0
)

@api.constrains("expected_price", "selling_price")
def _check_selling_price(self):
for property in self:
too_low = float_compare(property.selling_price, 0.9 * property.expected_price, 3) == -1
if not float_is_zero(property.selling_price, 3) and too_low:
msg = "The selling price cannot be lower than 90 percents of the expected price."
raise ValidationError(msg)

@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 = None

@api.ondelete(at_uninstall=False)
def _unlink_if_new_or_cancelled_property(self):
for property in self:
if property.state != "new" and property.state != "cancelled":
msg = "Only new or cancelled properties can be deleted"
raise UserError(msg)
return super().unlink()
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 don't need the super().unlink() with the ondelete decorator


def action_sold_property(self):
for property in self:
if property.state == "cancelled":
msg = "Sold properties cannot be cancelled"
raise UserError(msg)
property.state = "sold"
return True

def action_cancel_property(self):
for property in self:
if property.state == "sold":
msg = "Cancelled properties cannot be sold"
raise UserError(msg)
property.state = "cancelled"
return True
79 changes: 79 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from odoo import api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_compare


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(default=7)
date_deadline = fields.Date(
compute="_compute_date_deadline",
inverse="_inverse_date_deadline",
)

_check_offer_price = models.Constraint(
"CHECK(price > 0)",
"The offer price must be strictly positive",
)

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

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

@api.model_create_multi
def create(self, vals):
for offer in vals:
linked_property = self.env["estate.property"].browse(offer["property_id"])
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid making search and browse calls on each iteration of the loop (this is quite important as it could cause performance issues). Batching the search and filtering inside the loop for the relevant results could be a better approach for that.

if float_compare(offer["price"], linked_property.best_price, 3) == -1:
msg = "Offer price cannot be lower than an existing offer"
raise ValidationError(msg)
linked_property.state = "offer_received"
return super().create(vals)

@api.constrains("status", "property_id")
def _check_south_facing_garden_accept_offer(self):
for offer in self:
price_below_expected = float_compare(offer.price, offer.property_id.expected_price, 3) == -1
if offer.status == "accepted" and offer.property_id.garden_orientation == "south" and price_below_expected:
msg = "Offers for properties with south facing garden can only be accepted if the price of the offer is above the expected price of the property"
raise ValidationError(msg)

def action_accept_offer(self):
for offer in self:
offer.status = "accepted"
offer.property_id.selling_price = offer.price
offer.property_id.buyer_id = offer.partner_id
offer.property_id.state = "offer_accepted"
return True

def action_refuse_offer(self):
for offer in self:
offer.status = "refused"
return True
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()

_check_name_unique = models.Constraint(
"UNIQUE(name)",
"The tag name must be unique",
)
16 changes: 16 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from odoo import fields, models


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

name = fields.Char(required=True)
property_ids = fields.One2many("estate.property", "property_type_id")
sequence = fields.Integer("Sequence", default=1)

_check_name_unique = models.Constraint(
"UNIQUE(name)",
"The type name must be unique",
)
9 changes: 9 additions & 0 deletions estate/models/user_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from odoo import fields, models


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

property_ids = fields.One2many(
"estate.property", "seller_id", domain=[('state', '=', 'offer_received')],
)
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
1 change: 1 addition & 0 deletions estate/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_estate_property
Loading