Skip to content

leol - Technical Training #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: 18.0
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component, useState } from "@odoo/owl";

export class Card extends Component {
static template = "awesome_owl.card";

static props = {
title: { type: String },
slots: { type: Object, optional: true },
// content: { type: String }, removed following 13. Generic Card with slots
};

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

toggleOpened() {
this.state.opened = !this.state.opened;
}
}
19 changes: 19 additions & 0 deletions awesome_owl/static/src/card/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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">
<p><t t-esc="props.title" /></p>
<button t-on-click="toggleOpened">Toggle</button>
</h5>
<p class="card-text" t-if="state.opened">
<!-- <t t-out="props.content" removed following 13. Generic Card with slots/> -->
<t t-slot="default" />
</p>
</div>
</div>
</t>

</templates>
18 changes: 18 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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: 0 });
}

increment() {
this.state.value++;
this.props.onChange?.();
}
}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/counter/counter.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.counter">
<div class="card d-inline-block m-2" style="width: 18rem;">
<div class="card-body">
<p>Counter: <t t-esc="state.value" /></p>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</div>
</div>
</t>

</templates>
22 changes: 19 additions & 3 deletions awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";
import { Component, markup, useState } from "@odoo/owl";
import { Card } from "./card/card";
import { Counter } from "./counter/counter";
import { TodoList } from "./todolist/todolist";

export class Playground extends Component {
static components = { Counter, Card, TodoList };

static template = "awesome_owl.playground";

static props = {};

html1 = '<div style="background-color: red">some content</div>';
html2 = markup('<div style="background-color: red">some content</div>');

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

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

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
hello world, The sum is <t t-esc="sum.value" />
</div>
<div>
<Counter onChange.bind="incrementSum" />
<Counter onChange.bind="incrementSum" />
</div>
<div>
<Card title="'my title'">some content</Card>
<Card title="'my title'"><t t-out="this.html1" /></Card>
<Card title="'my title'"><t t-out="this.html2" /></Card>
<!-- <Card title="'my title'" content="0" /> Invalid props for component 'Card': 'content' is not a string -->
<Card title="'my title'"><Counter /></Card>
</div>
<div>
<TodoList />
</div>
</t>

Expand Down
25 changes: 25 additions & 0 deletions awesome_owl/static/src/todolist/todoitem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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,
},
},
removeTodo: { type: Function },
};

toggleState(ev) {
if (!ev.target) {
return;
}

this.props.todo.isCompleted = ev.target.checked;
}
}
24 changes: 24 additions & 0 deletions awesome_owl/static/src/todolist/todoitem.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todoitem">
<input
type="checkbox"
t-att-checked="props.todo.isCompleted"
t-on-change="toggleState"
style="display: inline-block; margin-right: 5px;" />
<div
t-att-class="{
'text-decoration-line-through' : props.todo.isCompleted,
'text-muted': props.todo.isCompleted,
}"
style="display: inline-block;" >
<t t-esc="props.todo.id" />. <t t-esc="props.todo.description" />
</div>
<span
class="fa fa-remove"
t-on-click="() => props.removeTodo(props.todo.id)"
style="display: inline-block; margin-left: 5px; color: red;" />
</t>

</templates>
55 changes: 55 additions & 0 deletions awesome_owl/static/src/todolist/todolist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Component, useRef, useState } from "@odoo/owl";
import { useAutofocus } from "../utils";
import { TodoItem } from "./todoitem";

export class TodoList extends Component {
static components = { TodoItem };

static template = "awesome_owl.todolist";

static props = {};

setup() {
this.id = 1; // could be calculated every iteration but would get very slow with a lot of todos
this.todos = useState([
// removed following 9. Adding a todo
// { id: 2, description: "write tutorial", isCompleted: true },
// { id: 3, description: "buy milk", isCompleted: false },
]);
this.inputRef = useRef("addTask");
useAutofocus("addTask");
}

addTodo(ev) {
if (ev.keyCode != 13 || !this.inputRef.el) {
return;
}

const desc = this.inputRef.el.value;

if (desc === "") {
return;
}

this.todos.push({
id: this.id,
description: desc,
isCompleted: false,
});

this.inputRef.el.value = "";
this.id++;
}

removeTodo(elemId) {
if (elemId < 0 || elemId >= this.id) {
return;
}

const index = this.todos.findIndex((elem) => elem.id === elemId);
if (index >= 0) {
// remove the element at index from list
this.todos.splice(index, 1);
}
}
}
15 changes: 15 additions & 0 deletions awesome_owl/static/src/todolist/todolist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todolist">
<div class="card d-inline-block m-2" style="width: 18rem;">
<div class="card-body">
<input type="text" class="" placeholder="Enter a new task" t-on-keyup="addTodo" t-ref="addTask"/>
<div t-foreach="todos" t-as="todo" t-key="todo.id" style="margin: 2px;">
<TodoItem todo="todo" removeTodo.bind="removeTodo" />
</div>
</div>
</div>
</t>

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

export function useAutofocus(name) {
const ref = useRef(name);
useEffect(
(el) => el && el.focus(),
() => [ref.el],
);
}
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
26 changes: 26 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
'name': 'Real Estate',
'summary': """
Starting module for "Server framework 101"
""",
'description': """
Starting module for "Server framework 101"
""",
'author': 'Odoo',
'website': 'https://www.odoo.com',
'license': 'LGPL-3',
'category': 'Tutorials/Real Estate',
'version': '0.1',
'application': True,
'installable': True,
'data': [
'security/ir.model.access.csv',
'views/estate_property_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_type_views.xml',
'views/estate_menus.xml',
'views/res_users_views.xml',
],
'depends': ['base'],
}
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
104 changes: 104 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from dateutil import relativedelta

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


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

name = fields.Char(string='Name of the Property', required=True)
description = fields.Text(string='Description')
postcode = fields.Char(string='Postcode')
date_availability = fields.Date(
string='Available From',
copy=False,
default=fields.Date.today() + relativedelta.relativedelta(months=3),
)
expected_price = fields.Float(string='Expected Price', required=True)
selling_price = fields.Float(string='Selling Price', readonly=True, copy=False)
bedrooms = fields.Integer(string='Number Bedrooms', default=2)
living_area = fields.Integer(string='Living Area (sqm)')
facades = fields.Integer(string='Facades')
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')],
)
active = fields.Boolean(default=True)
state = fields.Selection(
string='State',
selection=[
('new', 'New'),
('received', 'Offer Received'),
('accepted', 'Offer Accepted'),
('sold', 'Sold'),
('cancelled', 'Cancelled'),
],
required=True,
copy=False,
default='new',
)
property_type_id = fields.Many2one('estate.property.type', string='Property Type')
users_id = fields.Many2one('res.users', string='Salesman', default=lambda self: self.env.user)
partner_id = fields.Many2one('res.partner', string='Buyer', readonly=True)
tag_ids = fields.Many2many('estate.property.tag', string='Property Tags')
offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers')
total_area = fields.Integer(string='Total Area (sqm)', compute='_compute_total_area')
best_price = fields.Float(string='Best Offer', compute='_compute_price')

_sql_constraints = [
('check_expected_price', 'CHECK(expected_price > 0)', 'The expected price must strictly be positive.'),
('check_selling_price', 'CHECK(selling_price >= 0)', 'The selling price must be positive.'),
]

@api.ondelete(at_uninstall=False)
def prevent_delete_based_on_state(self):
if any(record.state in {'new', 'cancelled'} for record in self):
raise UserError(_('A new or cancelled property cannot be deleted.'))

@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')
def _compute_price(self):
for record in self:
record.write({'best_price': max(record.offer_ids.mapped('price'), default=0)})

@api.onchange('garden')
def _onchange_garden(self):
if not self.garden:
self.write({'garden_orientation': False, 'garden_area': 0})
return

self.write({'garden_area': 10, 'garden_orientation': self.garden_orientation or 'north'})

def action_sell(self):
if 'cancelled' in self.mapped('state'):
raise UserError(_('A property cancelled cannot be set as sold.'))

self.write({'state': 'sold'})
return True

def action_cancel(self):
if 'sold' in self.mapped('state'):
raise UserError(_('A property sold cannot be set as cancelled.'))

self.write({'state': 'cancelled'})
return True

@api.constrains('selling_price')
def _check_price(self):
for record in self:
if not record.selling_price:
continue

if float_utils.float_compare(record.selling_price, record.expected_price * 0.9, precision_rounding=3) == -1:
raise ValidationError(_('The selling cannot be lower than 90% of the expected price.'))
Loading