Skip to content

Commit

Permalink
sale_delivery_date: add cache to improve performance
Browse files Browse the repository at this point in the history
This avoids a lot of SQL requests (several hundreds on a SO with 20-30 lines),
and reduce the time needed to open orders.
  • Loading branch information
sebalix committed Mar 21, 2024
1 parent e81f274 commit 8b54842
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 8 additions & 4 deletions sale_delivery_date/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pytz import timezone, utc

from odoo import _, fields, models
from odoo import _, fields, models, tools
from odoo.exceptions import UserError
from odoo.tools.date_utils import date_range

Expand Down Expand Up @@ -91,6 +91,12 @@ def get_next_workdays_datetime(self, from_datetime, to_datetime):
if date.weekday() < 5
]

@tools.ormcache("weekday_number")
def _get_weekday(self, weekday_number):
return self.env["time.weekday"].search(
[("name", "=", weekday_number)], limit=1
)

def get_next_windows_start_datetime(self, from_datetime, to_datetime):
"""Get all delivery windows start time.
Expand All @@ -117,9 +123,7 @@ def get_next_windows_start_datetime(self, from_datetime, to_datetime):
from_datetime_tz, to_datetime_tz, timedelta(days=1)
):
this_weekday_number = this_datetime.weekday()
this_weekday = self.env["time.weekday"].search(
[("name", "=", this_weekday_number)], limit=1
)
this_weekday = self._get_weekday(this_weekday_number)
# Sort by start time to ensure the window we'll find will be the first
# one for the weekday
this_weekday_windows = self.delivery_time_window_ids.filtered(
Expand Down
6 changes: 5 additions & 1 deletion sale_delivery_date/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytz
from pytz import UTC, timezone

from odoo import api, models
from odoo import api, models, tools

from odoo.addons.partner_tz.tools import tz_utils

Expand Down Expand Up @@ -368,6 +368,7 @@ def _preparation_date_from_expedition_date(
# ======

@api.model
@tools.ormcache("date_from", "delay", "calendar")
def _add_delay(self, date_from, delay, calendar=False):
if calendar:
# Plan days is expecting a number of days, not a delay.
Expand All @@ -377,6 +378,7 @@ def _add_delay(self, date_from, delay, calendar=False):
return date_from + timedelta(days=delay)

@api.model
@tools.ormcache("date_from", "delay", "calendar")
def _deduct_delay(self, date_from, delay, calendar=False):
if calendar:
days = self._delay_to_days(delay)
Expand All @@ -399,6 +401,7 @@ def _apply_cutoff(self, date_order, cutoff, keep_same_day=False):
return self._get_utc_cutoff_datetime(cutoff, date_order, keep_same_day)

@api.model
@tools.ormcache("date_start", "calendar")
def _postpone_to_working_day(self, date_start, calendar=False):
"""Returns the nearest calendar's working day"""
if calendar:
Expand All @@ -415,6 +418,7 @@ def _apply_customer_window(self, delivery_date, partner):
return partner.next_delivery_window_start_datetime(from_date=delivery_date)

@api.model
@tools.ormcache("earliest_work_end", "latest_expedition_date", "calendar")
def _get_latest_work_end_from_date_range(
self, earliest_work_end, latest_expedition_date, calendar=False
):
Expand Down

0 comments on commit 8b54842

Please sign in to comment.