-
Notifications
You must be signed in to change notification settings - Fork 3.3k
empir - Technical training #1411
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
lipsumar
wants to merge
14
commits into
odoo:19.0
Choose a base branch
from
odoo-dev:19.0-server-framework-101-empir
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e3ffd53
[ADD] estate: add manifest for new Estate module
aba085f
[IMP] estate: add estate_property model
9d8b5a8
[LINT] estate: add missing blank line
52fd8b3
[IMP] estate: add estate_property model access rules
f478092
[LINT] estate: fix lint issues
a0b81fd
[IMP] estate: add basic UI
99b3070
[IMP] estate: add list, form and search views
57ccb87
[FIX] estate: rename model estate_property to estate.property
5b1be28
[IMP] estate: chapter 7: relations
a4e8a1b
[IMP] estate: chapter 8
047ef1a
[FIX] estate: fix _compute_validity
59b6b3b
[IMP] estate: chapter 9
2e10257
[IMP] estate: chapter 10 (constraints)
116bf88
[IMP] estate: chapter 11
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| 'name': 'Estate', | ||
| 'author': 'empir', | ||
| 'depends': ['base'], | ||
| 'application': True, | ||
| 'license': 'LGPL-3', | ||
| 'data': [ | ||
| 'security/ir.model.access.csv', | ||
| 'views/property.xml', | ||
| 'views/offer.xml', | ||
| 'views/tag.xml', | ||
| 'views/type.xml', | ||
| 'views/estate_menus.xml', | ||
| ], | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from . import estate_property | ||
| from . import property_type | ||
| from . import property_tag | ||
| from . import property_offer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| from dateutil.relativedelta import relativedelta | ||
|
|
||
| from odoo import api, fields, models | ||
| from odoo.exceptions import UserError, ValidationError | ||
| from odoo.tools.float_utils import float_compare | ||
|
|
||
|
|
||
| class EstateProperty(models.Model): | ||
| _name = "estate.property" | ||
| _description = "Real estate properties" | ||
| _order = "id desc" | ||
|
|
||
| name = fields.Char(required=True, default="Unknown") | ||
| active = fields.Boolean(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, | ||
| ) | ||
| description = fields.Text() | ||
| postcode = fields.Char() | ||
| date_availability = fields.Date( | ||
| copy=False, | ||
| default=fields.Date.today() + relativedelta(months=3), | ||
|
lipsumar marked this conversation as resolved.
|
||
| ) | ||
| 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( | ||
| string="Garden orientation", | ||
| selection=[ | ||
| ("north", "North"), | ||
| ("south", "South"), | ||
| ("east", "East"), | ||
| ("west", "West"), | ||
| ], | ||
| ) | ||
| 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="Tags") | ||
| offer_ids = fields.One2many( | ||
| "estate.property.offer", | ||
| inverse_name="property_id", | ||
| string="Offers", | ||
| ) | ||
| total_area = fields.Integer(compute="_compute_total_area") | ||
| best_price = fields.Float(compute="_compute_best_price") | ||
|
|
||
| _check_expected_price_positive = models.Constraint( | ||
| "CHECK(expected_price > 0)", | ||
| "Expected price must always be positive", | ||
| ) | ||
|
|
||
| _check_selling_price_positive = models.Constraint( | ||
| "CHECK(selling_price >= 0)", | ||
| "Selling price must be positive", | ||
| ) | ||
|
|
||
| @api.constrains("selling_price", "expected_price") | ||
| def _check_selling_price_minimum(self): | ||
| for record in self: | ||
| comp = float_compare( | ||
| record.selling_price, | ||
| record.expected_price * 0.9, | ||
| precision_digits=2, | ||
| ) | ||
| if record.selling_price > 0 and comp < 0: | ||
| msg = "Selling price must be at least 90% of expected price" | ||
| raise ValidationError(msg) | ||
|
|
||
| @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_best_price(self): | ||
| for record in self: | ||
| if len(record.offer_ids) > 0: | ||
| record.best_price = max(record.offer_ids.mapped("price")) | ||
| else: | ||
| record.best_price = 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 = None | ||
|
|
||
| def action_cancel(self): | ||
| for record in self: | ||
| if record.state == "sold": | ||
| msg = "Sold properties can not be cancelled" | ||
| raise UserError(msg) | ||
|
lipsumar marked this conversation as resolved.
|
||
| record.state = "cancelled" | ||
| return True | ||
|
|
||
| def action_sold(self): | ||
| for record in self: | ||
| if record.state == "cancelled": | ||
| msg = "Cancelled properties can not be sold" | ||
| raise UserError(msg) | ||
| record.state = "sold" | ||
| return True | ||
|
|
||
| @api.onchange("offer_ids") | ||
| def _onchange_offer_ids(self): | ||
| for record in self: | ||
| if record.state == "new" and len(record.offer_ids) == 1: | ||
| record.state = "offer_received" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from dateutil.relativedelta import relativedelta | ||
|
|
||
| from odoo import api, fields, models | ||
| from odoo.exceptions import UserError | ||
|
|
||
|
|
||
| class PropertyOffer(models.Model): | ||
| _name = "estate.property.offer" | ||
| _description = "Real estate property offers" | ||
| _order = "price desc" | ||
|
|
||
| price = fields.Float() | ||
| status = fields.Selection( | ||
| string="Status", | ||
| selection=[ | ||
| ("accepted", "Accepted"), | ||
| ("refused", "Refused"), | ||
| ], | ||
| copy=False, | ||
| ) | ||
| partner_id = fields.Many2one("res.partner", required=True) | ||
| property_id = fields.Many2one("estate.property", required=True) | ||
| 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_expected_price_positive = models.Constraint( | ||
| "CHECK(price > 0)", | ||
| "Offer price must always be positive", | ||
| ) | ||
|
|
||
| @api.depends("validity") | ||
| def _compute_date_deadline(self): | ||
| for record in self: | ||
| if record.validity: | ||
| crdate = record.create_date or fields.Date.today() | ||
| record.date_deadline = crdate + relativedelta( | ||
| days=record.validity, | ||
| ) | ||
|
|
||
| def _inverse_date_deadline(self): | ||
| for record in self: | ||
| if record.date_deadline: | ||
| record.validity = record._compute_validity() | ||
|
|
||
| # inverse doesn't update the UI when date_deadline changes, | ||
| # so we define an additional onchange to not confuse users | ||
| @api.onchange("date_deadline") | ||
| def _onchange_date_deadline(self): | ||
| self.validity = self._compute_validity() | ||
|
|
||
| def _compute_validity(self): | ||
| crdate = fields.Date.today() | ||
| if self.create_date: | ||
| crdate = self.create_date.date() | ||
| delta = self.date_deadline - crdate | ||
| return delta.days | ||
|
|
||
| def action_accept(self): | ||
| # prevent accepting multiple offers | ||
| if len(self) > 1: | ||
| msg = "Only one offer can be accepted at a time" | ||
| raise UserError(msg) | ||
|
lipsumar marked this conversation as resolved.
|
||
|
|
||
| # no need to process an already accepted offer | ||
| if self.status == "accepted": | ||
| return True | ||
|
|
||
| # ensure no other offer is already accepted | ||
| accepted_offers = [ | ||
| offer for offer in self.property_id.offer_ids if offer.status == "accepted" | ||
| ] | ||
|
lipsumar marked this conversation as resolved.
|
||
| if len(accepted_offers) > 0: | ||
| msg = "Another offer was already accepted" | ||
| raise UserError(msg) | ||
|
|
||
| self.status = "accepted" | ||
| self.property_id.buyer_id = self.partner_id | ||
| self.property_id.selling_price = self.price | ||
| self.property_id.state = "offer_accepted" | ||
| return True | ||
|
|
||
| def action_refuse(self): | ||
| for record in self: | ||
| record.status = "refused" | ||
| return True | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from odoo import fields, models | ||
|
|
||
|
|
||
| class PropertyTag(models.Model): | ||
| _name = "estate.property.tag" | ||
| _description = "Real estate property tags" | ||
| _order = "name" | ||
|
|
||
| name = fields.Char(required=True) | ||
| color = fields.Integer() | ||
|
|
||
| _name_uniq = models.Constraint( | ||
| "unique(name)", | ||
| "A tag with the same name already exists", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from odoo import api, fields, models | ||
|
|
||
|
|
||
| class PropertyType(models.Model): | ||
| _name = "estate.property.type" | ||
| _description = "Real estate property types" | ||
| _order = "sequence, name" | ||
|
|
||
| sequence = fields.Integer(string="Sequence", default=1) | ||
| name = fields.Char(required=True, string="Name") | ||
| property_ids = fields.One2many("estate.property", inverse_name="property_type_id") | ||
| offer_ids = fields.One2many( | ||
| "estate.property.offer", | ||
| inverse_name="property_type_id", | ||
| ) | ||
| offer_count = fields.Integer(compute="_compute_offer_counts") | ||
|
|
||
| @api.depends("offer_ids") | ||
| def _compute_offer_counts(self): | ||
| for record in self: | ||
| record.offer_count = len(record.offer_ids) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_property_user,estate.estate_property.user,model_estate_property,base.group_user,1,1,1,1 | ||
| access_estate_property_type_user,estate.estate_property_type.user,model_estate_property_type,base.group_user,1,1,1,1 | ||
| access_estate_property_tag_user,estate.estate_property_tag.user,model_estate_property_tag,base.group_user,1,1,1,1 | ||
| access_estate_property_offer_user,estate.estate_property_offer.user,model_estate_property_offer,base.group_user,1,1,1,1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?xml version="1.0"?> | ||
| <odoo> | ||
| <menuitem id="estate_menu_root" name="Real Estate"> | ||
| <menuitem id="estate_menu_topbar_advertisments" name="Advertisements"> | ||
| <menuitem id="estate_menu_action" action="estate_property_action"></menuitem> | ||
| </menuitem> | ||
| <menuitem id="estate_menu_topbar_settings" name="Settings"> | ||
| <menuitem id="estate_menu_property_types" action="estate_property_type_action"></menuitem> | ||
| <menuitem id="estate_menu_property_tags" action="estate_property_tag_action"></menuitem> | ||
| </menuitem> | ||
| </menuitem> | ||
| </odoo> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?xml version="1.0"?> | ||
| <odoo> | ||
| <record id="estate_property_offer_action" model="ir.actions.act_window"> | ||
| <field name="name">Offers</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> | ||
| </record> | ||
|
|
||
| <record id="property_offer_list" model="ir.ui.view"> | ||
| <field name="name">estate.property.offer.list</field> | ||
| <field name="model">estate.property.offer</field> | ||
| <field name="arch" type="xml"> | ||
| <list> | ||
| <field name="price" /> | ||
| <field name="partner_id" /> | ||
| <field name="status" /> | ||
| <field name="validity" /> | ||
| <field name="date_deadline" /> | ||
| </list> | ||
| </field> | ||
| </record> | ||
| <record id="estate_property_offer_form_view" model="ir.ui.view"> | ||
| <field name="name">estate.property.offer.form</field> | ||
| <field name="model">estate.property.offer</field> | ||
| <field name="arch" type="xml"> | ||
| <form string="Property"> | ||
| <sheet> | ||
| <group> | ||
| <group> | ||
| <field name="price" /> | ||
| <field name="partner_id" /> | ||
| <field name="status" /> | ||
| <field name="validity" /> | ||
| <field name="date_deadline" /> | ||
| </group> | ||
| </group> | ||
| </sheet> | ||
| </form> | ||
| </field> | ||
| </record> | ||
| </odoo> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.