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

export class Card extends Component {
static template = "awesome_owl.Card";
static props = {
title: String,
slots: {
type: Object,
shape: {
default: true,
},
},
};

setup() {
this.state = useState({ isOpen: true });
}

toggleOpen() {
this.state.isOpen = !this.state.isOpen;
}
}
16 changes: 16 additions & 0 deletions awesome_owl/static/src/card/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<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"/>
<button class="btn" t-on-click="toggleOpen">Toggle</button>
</h5>
<p class="card-text" t-if="state.isOpen">
<t t-slot="default"/>
</p>
</div>
</div>
</t>
</templates>
17 changes: 17 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component, useState } from "@odoo/owl";

export class Counter extends Component {
static template = "awesome_owl.Counter";
static props = {onChange: {type: Function, optional: true}};

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

increment() {
this.state.value++;
if (this.props.onChange) {
this.props.onChange();
}
}
}
9 changes: 9 additions & 0 deletions awesome_owl/static/src/counter/counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_owl.Counter">
<div class="m-2 p-2 border d-inline-block">
<span class="me-2">Counter: <t t-esc="state.value"/></span>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</div>
</t>
</templates>
19 changes: 17 additions & 2 deletions awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { Component } from "@odoo/owl";
import { Component, markup, useState } from "@odoo/owl";
import { Counter } from "./counter/counter";
import { Card } from "./card/card";
import { TodoList } from "./todo_list/todo_list";

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

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

incrementSum() {
this.sum.value++;
}
}
18 changes: 14 additions & 4 deletions awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.playground">
<t t-name="awesome_owl.Playground">
<div class="p-3">
hello world
<Counter onChange.bind="incrementSum" />
<Counter onChange.bind="incrementSum" />
<div>The sum is: <t t-esc="sum.value"/></div>
</div>
<div>
<Card title="'card 1'">
content of card 1
</Card>
<Card title="'card 2'">
<Counter />
</Card>
</div>
<TodoList />
</t>

</templates>
21 changes: 21 additions & 0 deletions awesome_owl/static/src/todo_list/todo_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component } from "@odoo/owl";

export class TodoItem extends Component {
static template = "awesome_owl.TodoItem";
static props = {
todo: {
type: Object,
shape: { id: Number, description: String, isCompleted: Boolean },
},
toggleState: { type: Function },
removeTodo: { type: Function },
};

onChange() {
this.props.toggleState(this.props.todo.id);
}

onDelete() {
this.props.removeTodo(this.props.todo.id);
}
}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/todo_list/todo_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-name="awesome_owl.TodoItem">
<div class="form-check">
<input class="form-check-input" type="checkbox" t-att-id="props.todo.id" t-att-checked="props.todo.isCompleted" t-on-change="onChange"/>
<label t-att-for="props.todo.id" t-att-class="props.todo.isCompleted ? 'text-decoration-line-through text-muted' : '' ">
<t t-esc="props.todo.id"/>.
<t t-esc="props.todo.description"/>
</label>
<span role="button" class="fa fa-trash ms-3 text-danger" t-on-click="onDelete"/>
</div>
</t>
</templates>
37 changes: 37 additions & 0 deletions awesome_owl/static/src/todo_list/todo_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Component, useState } from "@odoo/owl";
import { TodoItem } from "./todo_item"
import { useAutoFocusInput } from "../utils"

export class TodoList extends Component {
static template = "awesome_owl.TodoList";
static components = { TodoItem };
static props = [];


setup() {
this.todo_id = 0;
this.todos = useState([]);
useAutoFocusInput("input");
}

addTodo(ev) {
if (ev.keyCode !== 13 || ev.target.value.length === 0) {
return
}
this.todos.push({ id: this.todo_id++, description: ev.target.value, isCompleted: false });
ev.target.value = "";
}

toggleTodo(todoId) {
const todo = this.todos.find(e => e.id === todoId);
if (todo) {
todo.isCompleted = !todo.isCompleted;
}
}
removeTodo(todoId) {
const todo_index = this.todos.findIndex((elem) => elem.id === todoId);
if (todo_index >= 0) {
this.todos.splice(todo_index, 1);
}
}
}
11 changes: 11 additions & 0 deletions awesome_owl/static/src/todo_list/todo_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<t t-name="awesome_owl.TodoList">
<div class="d-inline-block border p-2 m-2">
<input class="form-control mb-3" type="text" placeholder="Enter a new task" t-on-keyup="addTodo" t-ref="input"/>
<t t-foreach="todos" t-as="todo" t-key="todo.id">
<TodoItem todo="todo" toggleState.bind="toggleTodo" removeTodo.bind="removeTodo"/>
</t>
</div>
</t>
</templates>
8 changes: 8 additions & 0 deletions awesome_owl/static/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useRef, onMounted } from "@odoo/owl";

export function useAutoFocusInput(refName) {
const inputRef = useRef(refName);
onMounted(() => {
inputRef.el.focus();
})
}
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
25 changes: 25 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.


{
'name': 'estate',
'category': 'Tutorials',
'depends': [
'base',
],
'data': [
'security/ir.model.access.csv',
'views/estate_property_offer_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_views.xml',
'views/res_users_views.xml',
'views/estate_menus.xml',
],
'application': True,
'installable': True,
'auto_install': True,
'author': 'Odoo S.A.',
'license': 'LGPL-3',

}
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 res_users
94 changes: 94 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare, float_is_zero


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

name = fields.Char(required=True, string="Title")
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(default=lambda self: 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_area = fields.Integer(string="Living Area (sqm)")
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")])
total_area = fields.Integer(compute='_compute_total_area')
property_type_id = fields.Many2one(comodel_name='estate.property.type', string="House Type")
buyer_id = fields.Many2one(comodel_name='res.partner', string="Buyer", copy=False)
seller_id = fields.Many2one(comodel_name='res.users', string="Seller", default=lambda self: self.env.user)
tag_ids = fields.Many2many(comodel_name='estate.property.tag', string="Tags")
offer_ids = fields.One2many(comodel_name='estate.property.offer', inverse_name='property_id', string="")
best_offer = fields.Float(compute='_compute_best_offer')
active = fields.Boolean(string="Active", default=True)
state = fields.Selection(
string="Status",
selection=[
('new', "New"),
('offer_received', "Offer received"),
('offer_accepted', "Offer accepted"),
('sold', "Sold"),
('cancelled', "Cancelled"),
],
required=True,
default='new',
copy=False,
)

_check_expected_price = models.Constraint('CHECK(expected_price > 0)', "The expected price must be stricly positive")

_check_selling_price = models.Constraint('CHECK(selling_price > 0)', "The selling price must be stricly positive")

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

@api.depends('offer_ids.price')
def _compute_best_offer(self):
for record_property in self:
record_property.best_offer = max(record_property.offer_ids.mapped('price'), default=0)

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

@api.constrains('selling_price', 'expected_price')
def _check_selling_expected_price(self):
for record_property in self:
if not float_is_zero(record_property.selling_price, precision_digits=2) and (
float_compare(record_property.expected_price * 0.9, record_property.selling_price, precision_digits=2) > 0):
raise UserError(self.env._("The selling price must be a least 90% of the expected price!"))

def action_cancel(self):
for record_property in self:
if record_property.state == 'sold':
raise UserError(record_property.env._("Sold properties cannot be cancelled"))
record_property.state = 'cancelled'
return True

def action_sold(self):
for record_property in self:
if record_property.state == 'cancelled':
raise UserError(record_property.env_("Canceled properties cannot be sold"))
record_property.state = 'sold'
return True

@api.ondelete(at_uninstall=False)
def _ondelete(self):
if any((property_id.state not in ('new', 'cancelled')) for property_id in self):
raise UserError(self.env._("You can only delete property in the state New or Cancelled"))
58 changes: 58 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_compare


class EstatePropertyOffer(models.Model):
_name = 'estate.property.offer'
_description = "Property Offer"
_order = "price desc"

price = fields.Float(string="Price")
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, ondelete='cascade')
validity = fields.Integer(default=7)
date_deadline = fields.Date(compute='_compute_date_deadline', inverse='_inverse_date_deadline')
property_type_id = fields.Many2one(related='property_id.property_type_id')

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

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

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

def action_accept_offer(self):
for record in self:
if record.property_id.offer_ids.filtered(lambda offer: offer.status == 'accepted'):
raise UserError(record.env_("Another offer has already been accepted."))
record.status = 'accepted'
record.property_id.buyer_id = record.partner_id
record.property_id.selling_price = record.price
record.property_id.state = 'offer_accepted'
return True

def action_refuse_offer(self):
for record in self:
record.status = 'refused'
return True

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
estate_property = self.env['estate.property'].browse(vals['property_id'])
if float_compare(vals['price'], estate_property.best_offer, precision_digits=2) < 0:
raise UserError(self.env._("The price must be higher than %s", estate_property.best_offer))

estate_property.state = 'offer_received'

return super().create(vals_list)
Loading