-
-
Notifications
You must be signed in to change notification settings - Fork 385
[18.0][FIX] delivery_carrier_pricelist: prevent AccessError
#1062
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
base: 18.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
# Copyright 2020 Camptocamp | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from logging import getLogger | ||
|
||
from odoo import api, fields, models | ||
|
||
_logger = getLogger(__name__) | ||
|
||
|
||
class DeliveryCarrier(models.Model): | ||
_inherit = "delivery.carrier" | ||
|
@@ -25,18 +29,23 @@ class DeliveryCarrier(models.Model): | |
) | ||
|
||
def rate_shipment(self, order): | ||
if self.invoice_policy == "pricelist": | ||
# Force computation from pricelist based on invoicing policy | ||
# Required since core's `rate_shipment` relies on `self.delivery_type` | ||
# to lookup for the right handler. | ||
# using a 'temp record' with new() won't work since it has NewId | ||
# and rate_shipment() compares those | ||
tmp_type = self.delivery_type | ||
self.delivery_type = "pricelist" | ||
result = super().rate_shipment(order) | ||
self.delivery_type = tmp_type | ||
return result | ||
return super().rate_shipment(order) | ||
# OVERRIDE: in case ``invoice_policy`` is set as "pricelist", we want to use | ||
# method ``pricelist_rate_shipment`` to retrieve the proper prices. However, | ||
# Odoo uses ``getattr(self, '%s_rate_shipment' % self.delivery_type)`` in its | ||
# base method ``rate_shipment()`` to lookup which function to use, so we | ||
# temporarily change the ``delivery_type`` if needed. | ||
|
||
# Quick check: if the invoice policy is not "pricelist", or the delivery type is | ||
# already set to "pricelist", we don't have to do anything | ||
if self.invoice_policy != "pricelist" or self.delivery_type == "pricelist": | ||
return super().rate_shipment(order) | ||
|
||
# Use ``sudo()`` to prevent ``AccessError`` when updating the delivery type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should finally propose a change to odoo core avoid this stuff. |
||
delivery_type = self.delivery_type | ||
self.sudo().delivery_type = "pricelist" | ||
result = super().rate_shipment(order) | ||
self.sudo().delivery_type = delivery_type | ||
return result | ||
|
||
def send_shipping(self, pickings): | ||
result = super().send_shipping(pickings) | ||
|
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.
to be discarded