Skip to content
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

[ADD] awesome_owl: create a new page #180

Closed
wants to merge 4 commits into from
Closed
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
18 changes: 18 additions & 0 deletions awesome_owl/static/src/card/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 },
};

setup() {
this.state = useState({ showContent: true });
this.minimizeContent = this.minimizeContent.bind(this);
}

minimizeContent() {
this.state.showContent = !this.state.showContent;
}
}
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 @@
<?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-esc="props.title"/>
<button class="btn" t-on-click="minimizeContent">Toggle</button>
</h5>
<t t-slot="default" t-if="state.showContent"/>
</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";

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

increment() {
this.state.value++;
if (this.props.onChange) this.props.onChange();
}

static props = {
onChange: { type: Function, optional: true },
};
}
11 changes: 11 additions & 0 deletions awesome_owl/static/src/counter/counter.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.counter">
<div class="card d-inline-block m-2" style="width: 8rem;">
<p class="me-3 mb-0">Counter:
<t t-out="state.value"></t>
</p>
<button class="btn btn-success" t-on-click="increment">Increment</button>
</div>
</t>
</templates>
17 changes: 14 additions & 3 deletions awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
/** @odoo-module **/

import { Component } from "@odoo/owl";

import { Component, markup, useState } from "@odoo/owl";
import { Counter } from "./counter/counter";
import { Card } from "./card/card";
import { TodoList, TodoItem } from "./todo/todo";
export class Playground extends Component {
static template = "awesome_owl.playground";
static components = { Counter, Card, TodoItem, TodoList };
html = markup("<div class='text-primary'>some content</div>");

setup() {
this.state = useState({ sum: 2 });
}

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

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
<div class="container">
<div class="row">
<Counter onChange.bind="incrementSum"/>
<Counter onChange.bind="incrementSum"/>
</div>
<p>
<t t-esc="state.sum"/>
</p>
</div>

<div class="container">
<div class="row">
<Card title="'Card 1'">
<Counter onChange.bind="incrementSum"/>
</Card>
<Card title="'Card 2'"/>
</div>
</div>
</t>

</templates>
<div class="card m-5" style="width: 18rem;">
<TodoList/>
</div>
</t>
</templates>
68 changes: 68 additions & 0 deletions awesome_owl/static/src/todo/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Component, useState } from "@odoo/owl";
import { useAutofocus } from "../utils";

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

setup() {
this.onChange = this.onChange.bind(this);
this.onClick = this.onClick.bind(this);
}

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

onClick() {
this.props.remove_callback(this.props.todo.id);
}
}

export class TodoList extends Component {
static template = "awesome_owl.todo.list";
static components = { TodoItem };

setup() {
this.todos = useState([]);
this.addToDo = this.addToDo.bind(this);
this.toggleState = this.toggleState.bind(this);
this.removeTodo = this.removeTodo.bind(this);

useAutofocus("input");
}

addToDo(event) {
let name = event.target.value;
if (event.keyCode === 13 && name.length) {
this.todos.push({
id: this.todos.length + 1,
description: name,
isCompleted: false,
});
event.target.value = "";
}
}

removeTodo(id) {
const index = this.todos.findIndex((todo) => todo.id === id);
if (index >= 0) {
this.todos.splice(index, 1);
}
}

toggleState(id) {
this.todos[id - 1].isCompleted = !this.todos[id - 1].isCompleted;
}
}
21 changes: 21 additions & 0 deletions awesome_owl/static/src/todo/todo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">

<t t-name="awesome_owl.todo.list">
<input t-ref="input" placeholder="Enter a new task." t-on-keyup="ev => addToDo(ev)"/>
<t t-foreach="todos" t-as="todo" t-key="todo.id">
<TodoItem todo="todo" toggle_callback="toggleState" remove_callback="removeTodo"/>
</t>
</t>

<t t-name="awesome_owl.todo.item">
<div>
<p t-att-class="{ 'text-muted': props.todo.isCompleted, 'text-decoration-line-through': props.todo.isCompleted }">
<input type="checkbox" t-on-change="onChange"/>
<t t-out="props.todo.id"/>:
<t t-out="props.todo.description"/>
<span class="fa fa-remove text-danger" t-on-click="onClick"/>
</p>
</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 useAutofocus(ref) {
let inputRef = useRef(ref);
onMounted(() => {
if (inputRef) 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
18 changes: 18 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
'name': 'Real Estate',
'depends': ['base'],
'category': 'Tutorials',
'application': True,
'data': ['data/ir.model.access.csv',
'views/res_users.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_menus.xml',
],
'demo': [
"demo/demo.xml",
],
'license': 'AGPL-3'
}
5 changes: 5 additions & 0 deletions estate/data/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_prop,access_estate_prop,model_estate_property,base.group_user,1,1,1,1
access_estate_prop_type,access_estate_prop_type,model_estate_property_type,base.group_user,1,1,1,1
access_estate_prop_offer,access_estate_prop_offer,model_estate_property_offer,base.group_user,1,1,1,1
access_estate_prop_tag,access_estate_prop_tag,model_estate_property_tag,base.group_user,1,1,1,1
13 changes: 13 additions & 0 deletions estate/demo/demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<record id="model_estate_property_action_cancel" model="ir.actions.server">
<field name="name">Mass cancel</field>
<field name="model_id" ref="estate.model_estate_property"/>
<field name="binding_model_id" ref="estate.model_estate_property"/>
<field name="binding_view_types">list</field>
<field name="state">code</field>
<field name="code">action = records.action_cancel()</field>
</record>
</data>
</odoo>
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
91 changes: 91 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from dateutil.relativedelta import relativedelta

from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_compare
from odoo.fields import Many2many, One2many

from .helper import format_selection


class EstateProperty(models.Model):
_name = 'estate.property'
_description = 'Estate Property modelisation'
_order = 'id desc'
_sql_constraints = [
('expected_price_strictly_positive', 'CHECK(expected_price > 0)', 'Expected price must be stricly positive'),
('selling_price_positive', 'CHECK(selling_price >= 0)', 'Selling price must be positive'),
]

name = fields.Char(required=True, string="Name")
description = fields.Text(string="Description")
postcode = fields.Char(string="Postal code")
date_availability = fields.Date(default=lambda x: fields.Date.today() + relativedelta(months=3), copy=False,
string='Availability date')
expected_price = fields.Float(required=True, string='Expected price')
selling_price = fields.Float(readonly=True, copy=False, string='Selling price')
bedrooms = fields.Integer(default=2, string='Bedrooms')
living_area = fields.Integer(string='Living area')
facades = fields.Integer(string='Facades')
garage = fields.Boolean(string='Garage')
garden = fields.Boolean(string='Garden')
garden_area = fields.Integer(string='Garden area')
garden_orientation = fields.Selection(string='Orientation',
selection=format_selection(['north', 'south', 'east', 'west']),
)
active = fields.Boolean(default=True, string='Active')
state = fields.Selection(string='State',
selection=format_selection(
['new', 'offer received', 'offer accepted', 'sold', 'canceled']),
default='new')

property_type_id = fields.Many2one('estate.property.type', string='Property type')

buyer_id = fields.Many2one('res.partner', copy=False, string='Buyer')
salesperson_id = fields.Many2one('res.users', default=lambda self: self.env.user, string='Salesperson')

tag_ids = Many2many('estate.property.tag', string='Tags')
offer_ids = One2many('estate.property.offer', 'property_id', string='Offers')

total_area = fields.Integer(compute='_compute_total_area', string='Total area')
best_price = fields.Float(compute='_compute_best_price', string='Best offer price')

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

@api.depends('offer_ids')
def _compute_best_price(self):
for estate in self:
estate.best_price = max(estate.offer_ids.mapped('price'), default=0)

@api.onchange('garden')
def _onchange_garden(self):
self.garden = [0, 10][self.garden]
self.garden_orientation = ['', 'north'][self.garden]

@api.constrains('selling_price')
def _check_price_offer_reasonable(self):
if float_compare(self.selling_price, 0.9 * self.expected_price, 3) <= 0:
raise ValidationError('The selling price must be at least 90% of the expected price.')

@api.ondelete(at_uninstall=False)
def _unlink_property_if_not_new_nor_canceled(self):
self.ensure_one()
if self.state not in ('new', 'canceled'):
raise UserError('Only new and canceled properties can be deleted.')

def action_sold(self):
self.ensure_one()
if self.state == 'canceled':
raise UserError('Canceled properties cannot be sold.')
self.state = 'sold'
return True

def action_cancel(self):
self.ensure_one()
if self.state == 'sold':
raise UserError('Sold properties cannot be canceled.')
self.state = 'canceled'
return True
Loading