-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Do Server Framework 101 Tutorial #1368
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
Shijakov
wants to merge
1
commit into
odoo:19.0
Choose a base branch
from
odoo-dev:19.0-server-framework-101-fishi
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
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,3 @@ | ||
| { | ||
| "python.languageServer": "None" | ||
| } |
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,17 @@ | ||
| { | ||
| 'name': "Real Estate", | ||
| 'version': '1.0', | ||
| 'depends': ['base'], | ||
| 'application': True, | ||
| 'data': [ | ||
| 'security/ir.model.access.csv', | ||
| 'views/res_users_view.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', | ||
| ], | ||
| 'author': 'odoo', | ||
| 'license': 'AGPL-3', | ||
| } |
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 @@ | ||
| from . import estate_property | ||
| from . import estate_property_type | ||
| from . import estate_property_tag | ||
| from . import estate_property_offer | ||
| from . import res_users |
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,146 @@ | ||
| from odoo import api, fields, models | ||
| from dateutil.relativedelta import relativedelta | ||
| from odoo.exceptions import UserError, ValidationError | ||
| from odoo.tools import float_compare | ||
|
|
||
|
|
||
| class Property(models.Model): | ||
| _name = "estate.property" | ||
| _description = "Real Estate Properties" | ||
| _order = "id desc" | ||
|
|
||
| name = fields.Char(string='Title', required=True) | ||
|
|
||
| description = fields.Text() | ||
|
|
||
| postcode = fields.Char() | ||
|
|
||
| date_availability = fields.Date('Available From', copy=False, default=(fields.Date.today() + relativedelta(months=3))) | ||
|
|
||
| expected_price = fields.Float(required=True) | ||
|
|
||
| _check_expected_price = models.Constraint( | ||
| 'CHECK(expected_price > 0)', | ||
| 'The Expected Price must be strictly positive' | ||
| ) | ||
|
|
||
| selling_price = fields.Float(readonly=True, copy=False) | ||
|
|
||
| _check_selling_price = models.Constraint( | ||
| 'CHECK(selling_price > 0)', | ||
| 'The Selling Price must be strictly positive' | ||
| ) | ||
|
|
||
| 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'), | ||
| ('west', 'West'), | ||
| ('east', 'East'), | ||
| ], | ||
| ) | ||
|
|
||
| active = fields.Boolean(default=True) | ||
|
|
||
| state = fields.Selection( | ||
| string='Status', | ||
| selection=[ | ||
| ('new', 'New'), | ||
| ('received', 'Offer Received'), | ||
| ('accepted', 'Offer Accepted'), | ||
| ('sold', 'Sold'), | ||
| ('cancelled', 'Cancelled'), | ||
| ], | ||
| required=True, | ||
| copy=False, | ||
| default="new" | ||
| ) | ||
|
|
||
| property_type_id = fields.Many2one(comodel_name="estate.property.type") | ||
|
|
||
| salesman_id = fields.Many2one(comodel_name="res.users", default=lambda self: self.env.uid) | ||
|
|
||
| buyer_id = fields.Many2one(comodel_name="res.partner", copy=False) | ||
|
|
||
| tag_ids = fields.Many2many(comodel_name="estate.property.tag") | ||
|
|
||
| offer_ids = fields.One2many(comodel_name="estate.property.offer", inverse_name="property_id") | ||
|
|
||
| total_area = fields.Integer(compute="_compute_total_area", string="Total Area (sqm)") | ||
|
|
||
| best_offer = fields.Float(compute="_compute_best_offer") | ||
|
|
||
| @api.depends('garden_area', 'living_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_offer(self): | ||
| for record in self: | ||
| record.best_offer = record.offer_ids[0]['price'] if record.offer_ids else 0 | ||
|
|
||
| @api.onchange('garden') | ||
| def _change_garden_fields(self): | ||
| if self.garden: | ||
| self.garden_area = 10 | ||
| self.garden_orientation = 'north' | ||
| else: | ||
| self.garden_area = None | ||
| self.garden_orientation = None | ||
|
|
||
| def _get_accepted_offers(self): | ||
| return self.offer_ids.filtered(lambda offer: offer.status == 'accepted') | ||
|
|
||
| @api.constrains('expected_price', 'selling_price') | ||
| def _constraint_expected_selling_price(self): | ||
| for record in self: | ||
| if record.offer_ids and float_compare(record.expected_price * 0.9, record.selling_price, precision_digits=2) > 0: | ||
| raise ValidationError(record.env._("Selling price must be at least %d%% of the expected price", 90)) | ||
|
|
||
| @api.ondelete(at_uninstall=False) | ||
| def _prevent_deletion_of_new_or_cancelled_properties(self): | ||
| for record in self: | ||
| if record.state in {"new", "cancelled"}: | ||
| raise UserError(self.env._("Cannot delete this property because it is 'New' or 'Cancelled'")) | ||
|
|
||
| def _accept_offer(self, offer): | ||
| self.ensure_one() | ||
| if self._get_accepted_offers(): | ||
| return False | ||
|
|
||
| self.state = 'accepted' | ||
| self.selling_price = offer.price | ||
| self.buyer_id = offer.partner_id | ||
|
|
||
| return True | ||
|
|
||
| def action_set_sold(self): | ||
| for record in self: | ||
| if record.state == 'cancelled': | ||
| raise UserError(record.env._('Cancelled properties cannot be sold')) | ||
|
|
||
| record.state = 'sold' | ||
|
|
||
| return True | ||
|
|
||
| def action_set_cancelled(self): | ||
| for record in self: | ||
| if record.state == 'sold': | ||
| raise UserError(record.env._('Sold properties cannot be cancelled')) | ||
|
|
||
| record.state = 'cancelled' | ||
|
|
||
| return True |
|
Shijakov marked this conversation as resolved.
|
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,85 @@ | ||
| from odoo import api, fields, models | ||
| from dateutil.relativedelta import relativedelta | ||
| from odoo.exceptions import UserError | ||
|
|
||
|
|
||
| class PropertyOffer(models.Model): | ||
| _name = "estate.property.offer" | ||
| _description = "Real Estate Property Offers" | ||
| _order = "price desc" | ||
|
|
||
| price = fields.Float() | ||
|
|
||
| _check_offer_price = models.Constraint( | ||
| 'CHECK(price > 0)', | ||
| 'The Offer Price must be strictly positive' | ||
| ) | ||
|
|
||
| status = fields.Selection( | ||
| selection=[ | ||
| ('new', 'New'), | ||
| ('accepted', 'Accepted'), | ||
| ('refused', 'Refused'), | ||
| ], | ||
| copy=False, | ||
| default='new', | ||
| ) | ||
|
|
||
| partner_id = fields.Many2one(comodel_name='res.partner', required=True) | ||
|
|
||
| property_id = fields.Many2one(comodel_name='estate.property', required=True) | ||
|
|
||
| property_type_id = fields.Many2one(related='property_id.property_type_id', store=True) | ||
|
|
||
| validity = fields.Integer(string='Validity (days)', default=7) | ||
|
|
||
| date_deadline = fields.Date(string='Deadline', compute='_compute_deadline', inverse='_inverse_total') | ||
|
|
||
| def _get_create_date(self, record): | ||
| return fields.Date.to_date(record.create_date) or fields.Date.today() | ||
|
|
||
| @api.depends('validity') | ||
| def _compute_deadline(self): | ||
| for record in self: | ||
| record.date_deadline = self._get_create_date(record) + relativedelta(days=record.validity) | ||
|
|
||
| def _inverse_total(self): | ||
| for record in self: | ||
| record.validity = (record.date_deadline - self._get_create_date(record)).days | ||
|
|
||
| def action_accept(self): | ||
| for record in self: | ||
| if record.status == 'refused': | ||
| raise UserError(self.env._("Cannout accept an offer that has been refused")) | ||
|
|
||
| if not record.property_id._accept_offer(self): | ||
| raise UserError(self.env._("Another offer is already accepted")) | ||
|
|
||
| record.status = 'accepted' | ||
|
|
||
| return True | ||
|
|
||
| def action_refuse(self): | ||
| for record in self: | ||
| if record.status == 'accepted': | ||
| raise UserError(self.env._("Cannout refuse an offer that has been accepted")) | ||
|
|
||
| record.status = 'refused' | ||
|
|
||
| return True | ||
|
|
||
| @api.model | ||
| def create(self, vals_list): | ||
| for val in vals_list: | ||
| property = self.env['estate.property'].browse(val['property_id']) | ||
|
|
||
| if property.state == "new": | ||
| property.state = "received" | ||
|
|
||
| price = val['price'] | ||
| max_price = property.offer_ids[0]['price'] if property.offer_ids else -1 | ||
|
|
||
| if max_price > price: | ||
| raise UserError(self.env._("New offer price must be greater or equal than %d", max_price)) | ||
|
|
||
| return super().create(vals_list) |
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,16 @@ | ||
| from odoo import fields, models | ||
|
|
||
|
Shijakov marked this conversation as resolved.
|
||
|
|
||
| class PropertyTag(models.Model): | ||
| _name = "estate.property.tag" | ||
| _description = "Real Estate Property Tags" | ||
| _order = "name" | ||
|
|
||
| name = fields.Char(required=True) | ||
|
|
||
| color = fields.Integer() | ||
|
|
||
| _uniq_name = models.Constraint( | ||
| 'unique(name)', | ||
| 'The name must be unique', | ||
| ) | ||
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,26 @@ | ||
| from odoo import api, fields, models | ||
|
|
||
|
|
||
| class PropertyType(models.Model): | ||
| _name = "estate.property.type" | ||
| _description = "Real Estate Property Types" | ||
| _order = "sequence, name" | ||
|
|
||
| name = fields.Char(required=True) | ||
| sequence = fields.Integer('Sequence', default=1) | ||
|
|
||
| _uniq_name = models.Constraint( | ||
| 'unique(name)', | ||
| 'The name must be unique', | ||
| ) | ||
|
|
||
| property_ids = fields.One2many(comodel_name="estate.property", inverse_name="property_type_id") | ||
|
|
||
| offer_ids = fields.One2many(comodel_name="estate.property.offer", inverse_name="property_type_id") | ||
|
|
||
| offer_count = fields.Integer(compute="_compute_offer_count") | ||
|
|
||
| @api.depends('offer_ids') | ||
| def _compute_offer_count(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,11 @@ | ||
| from odoo import fields, models | ||
|
|
||
|
|
||
| class ResUsers(models.Model): | ||
| _inherit = "res.users" | ||
|
|
||
| property_ids = fields.One2many( | ||
| comodel_name="estate.property", | ||
| inverse_name="salesman_id", | ||
| domain=[("state", "in", ["new", "received"])] | ||
|
Shijakov marked this conversation as resolved.
|
||
| ) | ||
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 | ||
| estate_property_access_right_user,estate_property_access_right,model_estate_property,base.group_user,1,1,1,1 | ||
| estate_property_type_access_right_user,estate_property_type_access_right,model_estate_property_type,base.group_user,1,1,1,1 | ||
| estate_property_tag_access_right_user,estate_property_tag_access_right,model_estate_property_tag,base.group_user,1,1,1,1 | ||
| estate_property_offer_access_right_user,estate_property_offer_access_right,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,13 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <odoo> | ||
| <menuitem id="estate_menu_root" name="Real Estate"> | ||
| <menuitem id="estate_menu_advertisements" name="Advertisements" sequence="1"> | ||
| <menuitem id="estate_property_menu_action" action="estate_property_action" /> | ||
| </menuitem> | ||
|
|
||
| <menuitem id="estate_menu_settings" name="Settings" sequence="2"> | ||
| <menuitem id="estate_property_type_menu_action" action="estate_property_type_action" /> | ||
| <menuitem id="estate_property_tag_menu_action" action="estate_property_tag_action" /> | ||
| </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,45 @@ | ||
| <?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="estate_property_offer_view_form" 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="Create Offer"> | ||
| <sheet> | ||
| <group> | ||
| <group> | ||
| <field name="price" /> | ||
| <field name="partner_id"/> | ||
| <field name="validity"/> | ||
| <field name="date_deadline"/> | ||
| <field name="status"/> | ||
| </group> | ||
| </group> | ||
| </sheet> | ||
| </form> | ||
| </field> | ||
| </record> | ||
|
|
||
| <record id="estate_property_offer_view_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 string="Offers" editable="bottom" decoration-danger="status == 'refused'" decoration-success="status == 'accepted'"> | ||
| <field name="price"/> | ||
| <field name="partner_id"/> | ||
| <field name='validity'/> | ||
| <field name='date_deadline'/> | ||
| <field name='property_type_id'/> | ||
| <button name="action_accept" string="Confirm" type="object" icon="fa-check" invisible="status != 'new'"/> | ||
| <button name="action_refuse" string="Refuse" type="object" icon="fa-times" invisible="status != 'new'"/> | ||
| </list> | ||
| </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.
Uh oh!
There was an error while loading. Please reload this page.