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
4 changes: 2 additions & 2 deletions awesome_clicker/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
{
'name': "Awesome Clicker",
'name': 'Awesome Clicker',

Choose a reason for hiding this comment

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

Not sure it was necessary to change this, we try not to change code for aestetic reasons once it has been merged, as it makes it harder to get the git blame to see who wrote it and find the commit that introduced the line.


'summary': """
Starting module for "Master the Odoo web framework, chapter 1: Build a Clicker game"
Expand All @@ -10,7 +10,7 @@
Starting module for "Master the Odoo web framework, chapter 1: Build a Clicker game"
""",

'author': "Odoo",
'author': 'Odoo',
'website': "https://www.odoo.com/",
'category': 'Tutorials',
'version': '0.1',
Expand Down
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
23 changes: 23 additions & 0 deletions estate/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Real Estate",
"version": "1.0",
"summary": "Real Estate Property Management",
"description": """
Tutorial module for managing real estate properties.
""",
"author": "Your Name",
"category": "Tutorial",
"depends": ["base"],
"data": [
'security/ir.model.access.csv',
'views/res_users_views.xml',
'views/estate_property_offer_views.xml',
'views/estate_property_tag_views.xml',
'views/estate_property_type_views.xml',
'views/estate_property_views.xml',
'views/estate_menus.xml'
],
"installable": True,
"application": True,
'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
116 changes: 116 additions & 0 deletions estate/models/estate_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from odoo import api, fields, models
from dateutil.relativedelta import relativedelta
from odoo.tools.float_utils import float_is_zero
from odoo.exceptions import UserError, ValidationError


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

name = fields.Char(required=True)
description = fields.Text()
postcode = fields.Char()
date_availability = fields.Date(copy=False, default=lambda self: fields.Date.context_today(self) + relativedelta(months=3))
expected_price = fields.Float(required=True)
selling_price = fields.Float(readonly=True, copy=False)
bedrooms = fields.Integer(default=2)
living_area = fields.Integer()
facades = fields.Integer()
garage = fields.Boolean()
garden = fields.Boolean()
garden_area = fields.Integer()
garden_orientation = fields.Selection(
selection=[
('north', 'North'),
('south', 'South'),
('east', 'East'),
('west', 'West'),
]
)
state = fields.Selection(
[
('new', 'New'),
('offer_received', 'Offer Received'),
('offer_accepted', 'Offer Accepted'),
('sold', 'Sold'),
('canceled', 'Cancelled'),
],
required=True,
copy=False,
default='new',
)
active = fields.Boolean(default=True)

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

buyer_id = fields.Many2one('res.partner', string='Buyer', copy=False)

salesperson_id = fields.Many2one('res.users', string='Salesperson', default=lambda self: self.env.user)

tag_ids = fields.Many2many('estate.property.tag', string='Property Tag')

offer_ids = fields.One2many('estate.property.offer', 'property_id', string='Offers')

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.constrains('selling_price', 'expected_price')
def _check_selling_price(self):
for record in self:
if float_is_zero(record.selling_price, precision_digits=2):
continue
if record.selling_price < record.expected_price * 0.9:
raise ValidationError('The selling price cannot be lower than 90% of the expected price.')

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

@api.depends('offer_ids.price')
def _compute_best_price(self):
for record in self:
prices = record.offer_ids.mapped('price')
record.best_price = max(prices, default=0.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 = False

@api.ondelete(at_uninstall=False)
def _unlink_if_new_or_cancelled(self):
for record in self:
if record.state not in ('new', 'cancelled'):
raise UserError('You can only delete a property when its state is New or Cancelled.')

def action_sold(self):
for record in self:
if record.state == 'canceled':
raise UserError('A canceled property cannot be set as sold.')
record.state = 'sold'
return True

def action_cancel(self):
for record in self:
if record.state == 'sold':
raise UserError('A sold property cannot be canceled.')
record.state = 'canceled'
return True
83 changes: 83 additions & 0 deletions estate/models/estate_property_offer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from odoo import api, fields, models
from datetime import timedelta
from odoo.exceptions import UserError


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

partner_id = fields.Many2one('res.partner', string='Partner', required=True)
property_id = fields.Many2one('estate.property', string='Estate Property', required=True)
price = fields.Float()
status = fields.Selection(
copy=False,
selection=[
('accepted', 'Accepted'),
('refused', 'Refused'),
]
)
property_type_id = fields.Many2one('estate.property.type', related='property_id.property_type_id', store=True, readonly=True)
validity = fields.Integer(default=7)
date_deadline = fields.Date(compute='_compute_date_deadline', inverse='_inverse_date_deadline', store=True)

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

@api.depends('create_date', 'validity')
def _compute_date_deadline(self):
for offer in self:
create_dt = offer.create_date if offer.create_date else fields.Datetime.now()
offer.date_deadline = (create_dt + timedelta(days=offer.validity)).date()

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

def action_confirm(self):
for offer in self:
if offer.property_id.state in ('offer_accepted', 'sold', 'canceled'):
raise UserError('A sold property cannot accept new offers.')

offer.status = 'accepted'

offer.property_id.write({
'buyer_id': offer.partner_id.id,
'selling_price': offer.price,
'state': 'offer_accepted',
})

return True

def action_refuse(self):
for offer in self:
if offer.status == 'accepted':
raise UserError('You cannot refuse an accepted offer.')

offer.status = 'refused'

property_record = offer.property_id

active_offers = property_record.offer_ids.filtered(lambda o: o.status in ('pending', 'accepted'))

if not active_offers:
property_record.write({
'buyer_id': False,
'selling_price': 0,
'state': 'new',
})

return True

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals['property_id'] and vals['price'] is not None:
if vals['price'] < self.env['estate.property'].browse(vals['property_id']).best_price:
raise UserError('The offer must be higher than the existing offers.')
return super().create(vals_list)
10 changes: 10 additions & 0 deletions estate/models/estate_property_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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(name="Color")
24 changes: 24 additions & 0 deletions estate/models/estate_property_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from odoo import api, fields, models


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

name = fields.Char(required=True)
sequence = fields.Integer('Sequence', default=1)

_unique_type_name = models.Constraint(
'UNIQUE(name)',
'The property type name must be unique.',
)

property_ids = fields.One2many('estate.property', 'property_type_id', string='Properties')
offer_ids = fields.One2many('estate.property.offer', 'property_type_id', string='Offers')
offer_count = fields.Integer(compute='_compute_offer_count', string='Offer Count')

@api.depends('offer_ids')
def _compute_offer_count(self):
for record in self:
record.offer_count = len(record.offer_ids)
6 changes: 6 additions & 0 deletions estate/models/res_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from odoo import fields, models

class ResUsers(models.Model):
_inherit = 'res.users'

property_ids = fields.One2many('estate.property', 'salesperson_id', string='Properties', domain=[('state', 'in', ['new', '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
estate.access_estate_property,access_estate_property,estate.model_estate_property,base.group_user,1,1,1,1
estate.access_estate_property_type,access_estate_property_type,estate.model_estate_property_type,base.group_user,1,1,1,1
estate.access_estate_property_tag,access_estate_property_tag,estate.model_estate_property_tag,base.group_user,1,1,1,1
estate.access_estate_property_offer,access_estate_property_offer,estate.model_estate_property_offer,base.group_user,1,1,1,1
27 changes: 27 additions & 0 deletions estate/views/estate_menus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version='1.0' encoding='utf-8'?>
<odoo>
<!-- Root menu (App switcher) -->
<menuitem id='estate_menu_root' name='Real Estate'/>

<!-- First level menu (top bar) -->
<menuitem id='estate_menu_adv'
name='Advertisements'
parent='estate_menu_root'/>

<menuitem id='estate_menu_settings'
name='Settings'
parent='estate_menu_root'/>

<!-- Action menu (opens the list/form view) -->
<menuitem id='estate_menu_properties'
parent='estate_menu_adv'
action='estate_property_action'/>

<menuitem id='estate_menu_property_type_properties'
parent='estate_menu_settings'
action='estate_property_type_action'/>

<menuitem id='estate_menu_property_tag_properties'
parent='estate_menu_settings'
action='estate_property_tag_action'/>
</odoo>
45 changes: 45 additions & 0 deletions estate/views/estate_property_offer_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version='1.0' encoding='utf-8'?>
<odoo>
<record id='estate_property_offer_view_list' model='ir.ui.view'>
<field name='name'>estate.property.offer.view.list</field>
<field name='model'>estate.property.offer</field>
<field name='arch' type='xml'>
<list string='Properties' editable='bottom' decoration-success="status in ('accepted')" decoration-danger="status in ('refused')">
<field name='price'/>
<field name='partner_id' string='Partner'/>
<field name='property_type_id' string='Property Type'/>
<field name='validity' string='Validity (days)'/>
<field name='date_deadline' string='Deadline'/>
<button name='action_confirm' string='Confirm' type='object' icon='fa-check' invisible="status in ('accepted', 'refused')"/>
<button name='action_refuse' string='Refuse' type='object' icon='fa-times' invisible="status in ('accepted', 'refused')"/>
<field name='status' column_invisible='1'/>
</list>
</field>
</record>

<record id='estate_property_offer_view_form' model='ir.ui.view'>
<field name='name'>estate.property.offer.view.form</field>
<field name='model'>estate.property.offer</field>
<field name='arch' type='xml'>
<form string='Property'>
<sheet>
<group>
<field name='price'/>
<field name='partner_id' string='Partner'/>
<field name='validity' string='Validity (days)'/>
<field name='date_deadline' string='Deadline'/>
<field name='status'/>
</group>
</sheet>
</form>
</field>
</record>

<record id='estate_property_offer_action_from_types' model='ir.actions.act_window'>
<field name='name'>Offers From Types</field>
<field name='res_model'>estate.property.offer</field>
<field name='view_mode'>list,form</field>
<field name='domain'>[('property_type_id', '=', active_id)]</field>
<field name='view_id' ref='estate_property_offer_view_list'/>
</record>
</odoo>
18 changes: 18 additions & 0 deletions estate/views/estate_property_tag_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version='1.0' encoding='utf-8'?>
<odoo>
<record id='estate_property_tag_action' model='ir.actions.act_window'>
<field name='name'>Property Tag</field>
<field name='res_model'>estate.property.tag</field>
<field name='view_mode'>list,form</field>
</record>

<record id='estate_property_tag_view_list' model='ir.ui.view'>
<field name='name'>estate.property.tag.view.list</field>
<field name='model'>estate.property.tag</field>
<field name='arch' type='xml'>
<list string='Properties' editable='bottom'>
<field name='name'/>
</list>
</field>
</record>
</odoo>
Loading