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

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

static props = {
title: {type: String},
slots: {
type: Object,
shape: {
default: true
}
},
};

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

toggleState() {
this.state.value = !this.state.value;
}
}
18 changes: 18 additions & 0 deletions awesome_owl/static/src/card/card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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 btn-primary" t-on-click="toggleState">Toggle</button>
</h5>
<p class="card-text" t-if="state.value">
<t t-slot="default"/>
</p>
</div>
</div>
</t>

</templates>
20 changes: 20 additions & 0 deletions awesome_owl/static/src/counter/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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++;
if (this.props.onChange) {
this.props.onChange();
}
}
}
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="m-2 p-2 border d-inline-block">
<p>Counter: <t t-esc="state.value"/></p>
<button class="btn btn-primary" t-on-click="increment">Increment</button>
</div>
</t>

</templates>
20 changes: 19 additions & 1 deletion awesome_owl/static/src/playground.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
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/todo_list/todo_list"

export class Playground extends Component {
static template = "awesome_owl.playground";

static components = { Card, Counter, TodoList };

static props = {};

value = markup("<h5 class=\"card-title\">some content</h5>");

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

incrementSum() {
this.sum.value++;
}

}
18 changes: 17 additions & 1 deletion awesome_owl/static/src/playground.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@

<t t-name="awesome_owl.playground">
<div class="p-3">
hello world
<Counter onChange.bind="incrementSum"/>
<Counter onChange.bind="incrementSum"/>
<p>The sum is: <t t-esc="sum.value"/></p>
</div>
<div>
<Card title="'card 1'">
<span>some content</span>
</Card>
<Card title="'card 2'">
<t t-out="value"/>
</Card>
<Card title="'card 3'">
<Counter/>
</Card>
</div>
<div class="m-2 p-2 border d-inline-block">
<TodoList/>
</div>
</t>

Expand Down
26 changes: 26 additions & 0 deletions awesome_owl/static/src/todo/todo_item/todo_item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Component } from "@odoo/owl";

export class TodoItem extends Component {
static template = "awesome_owl.todo.todo_item";

static props = {
todo: {
type: Object,
shape: {
id: Number,
description: String,
isCompleted: Boolean,
}
},
toggleState: {type: Function},
removeTodo: {type: Function},
};

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

removeTodo() {
this.props.removeTodo(this.props.todo.id);
}
}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/todo/todo_item/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.todo.todo_item">
<p t-att-class="{'text-muted text-decoration-line-through': props.todo.isCompleted}">
<input type="checkbox" t-on-change="toggleState"/>
<t t-esc="props.todo.id"/>.
<t t-esc="props.todo.description"/>
<span class="fa fa-remove" t-on-click="removeTodo"/>
</p>
</t>

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

export class TodoList extends Component {
static template = "awesome_owl.todo.todo_list";

static components = { TodoItem };

static props = {};

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

addTodo(event) {
if (event.type !== "keyup" || event.keyCode !== 13 || event.target.value === "") {
return;
}
this.todos.push({id: this.id++, description: event.target.value, isCompleted: false});
event.target.value = "";
}

toggleTodo(id) {
let todo = this.todos.find(todo => todo.id === id);
todo.isCompleted = !todo.isCompleted;
}

#reOrderItems() {
for (let i = 0; i < this.todos.length; i++) {
this.todos[i].id = i;
}
}

removeTodo(id) {
const todo = this.todos.findIndex(todo => todo.id === id);
this.todos.splice(todo, 1);
this.#reOrderItems();
this.id--;
}
}
13 changes: 13 additions & 0 deletions awesome_owl/static/src/todo/todo_list/todo_list.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.todo.todo_list">
<div>
<div>Enter a new task: <input t-on-keyup="addTodo" t-ref="input"/></div>
<t t-foreach="todos" t-as="t" t-key="t.id">
<TodoItem todo="t" 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 { onMounted, useRef } from "@odoo/owl";

export function useAutoFocus(name) {
let ref = useRef(name);
onMounted(() => {
ref.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'],
'data': [
'security/ir.model.access.csv',

'views/estate_property_tag_views.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_views.xml',
'views/estate_menus.xml',
'views/res_users_views.xml',
],
'installable': True,
'application': True,
'author': "Odoo",
'license': 'AGPL-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 property
from . import property_offer
from . import property_tag
from . import property_type
from . import res_users
110 changes: 110 additions & 0 deletions estate/models/property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from dateutil.relativedelta import relativedelta

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


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

name = fields.Char(string="Title", required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(
string="Available From",
default=lambda self: fields.Date.today() + relativedelta(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(string="Garden Area (sqm)")
garden_orientation = fields.Selection(
selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('west', 'West')]
)
state = fields.Selection(
default="new",
string="Status",
selection=[
('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'),
('cancelled', 'Cancelled'),
],
required=True,
copy=False,
)
active = fields.Boolean(default=True)
property_type_id = fields.Many2one('estate.property.type', string="Property Type")
partner_id = fields.Many2one('res.partner', string="Buyer", readonly=True)
user_id = fields.Many2one('res.users', string="Salesman", default=lambda self: self.env.user)
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_offer = fields.Float(default=0.0, compute="_compute_best_offer")

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

@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')
def _compute_best_offer(self):
for record in self:
record.best_offer = max(record.offer_ids.mapped('price')) if record.offer_ids else 0.0

@api.onchange('garden')
def _onchange_garden(self):
self.garden_area = 10 if self.garden else 0
self.garden_orientation = 'north' if self.garden else ''

@api.constrains('selling_price', 'expected_price')
def _check_selling_price(self):
for record in self:
if (
not float_is_zero(record.selling_price, precision_digits=2) and
float_compare(record.selling_price, record.expected_price * 0.9, precision_digits=2) == -1
):
raise ValidationError(
self.env._(
"The selling price must be at least 90% of the expected price! "
"You must reduce the expected price if you want to accept this offer."
)
)

def action_set_sold(self):
if self.state == 'cancelled':
raise UserError(self.env._("Cancelled properties cannot be sold."))

self.state = 'sold'
return True

def action_set_cancelled(self):
if self.state == 'sold':
raise UserError(self.env._("Sold properties cannot be cancelled."))

self.state = 'cancelled'
return True

@api.ondelete(at_uninstall=False)
def _unlink_except_new_or_cancelled(self):
for record in self:
if record.state not in ['new', 'cancelled']:
raise UserError(
self.env._("Only new and cancelled properties can be deleted.")
)
Loading