Skip to content

Commit

Permalink
[MIG] base_time_window: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimkhoi3010 committed Nov 6, 2024
1 parent ac714c5 commit 7a2c151
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 33 deletions.
6 changes: 6 additions & 0 deletions base_time_window/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Contributors
Trobz

- Dung Tran <[email protected]>
- Khoi (Kien Kim) <[email protected]>

Other credits
-------------
Expand All @@ -124,6 +125,11 @@ The development of this module has been financially supported by:

- Camptocamp

The migration of this module from 17.0 to 18.0 was financially supported
by:

- Camptocamp

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion base_time_window/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Base Time Window",
"summary": "Base model to handle time windows",
"version": "17.0.1.0.0",
"version": "18.0.1.0.0",
"category": "Technical Settings",
"author": "ACSONE SA/NV, Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
Expand Down
10 changes: 5 additions & 5 deletions base_time_window/models/time_weekday.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2020 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models, tools
from odoo import api, fields, models, tools


class TimeWeekday(models.Model):
Expand All @@ -20,7 +20,7 @@ class TimeWeekday(models.Model):
],
required=True,
)
_sql_constraints = [("name_uniq", "UNIQUE(name)", _("Name must be unique"))]
_sql_constraints = [("name_uniq", "UNIQUE(name)", ("Name must be unique"))]

@api.depends("name")
def _compute_display_name(self):
Expand All @@ -40,15 +40,15 @@ def _get_id_by_name(self, name):
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
self.env.registry.clear_cache() # _get_id_by_name
self.env.registry.clear_cache()
return records

def write(self, vals):
result = super().write(vals)
self.env.registry.clear_cache() # _get_id_by_name
self.env.registry.clear_cache()
return result

def unlink(self):
result = super().unlink()
self.env.registry.clear_cache() # _get_id_by_name
self.env.registry.clear_cache()
return result
36 changes: 16 additions & 20 deletions base_time_window/models/time_window_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from psycopg2.extensions import AsIs

from odoo import _, api, fields, models
from odoo import api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.misc import format_time

Expand All @@ -31,18 +31,16 @@ def check_window_no_overlaps(self):
for record in self:
if record.time_window_start > record.time_window_end:
raise ValidationError(
_("%(end_time)s must be > %(start_time)s")
% (
{
"end_time": self.float_to_time_repr(record.time_window_end),
"start_time": self.float_to_time_repr(
record.time_window_start
),
}
self.env._(
"%(end_time)s must be > %(start_time)s",
end_time=self.float_to_time_repr(record.time_window_end),
start_time=self.float_to_time_repr(record.time_window_start),
)
)
if not record.time_window_weekday_ids:
raise ValidationError(_("At least one time.weekday is required"))
raise ValidationError(
self.env._("At least one time.weekday is required")
)
# here we use a plain SQL query to benefit of the numrange
# function available in PostgresSQL
# (http://www.postgresql.org/docs/current/static/rangetypes.html)
Expand All @@ -55,8 +53,8 @@ def check_window_no_overlaps(self):
on d.%(relation_window_fkey)s = w.id
WHERE
NUMRANGE(w.time_window_start::numeric,
w.time_window_end::numeric) &&
NUMRANGE(%(start)s::numeric, %(end)s::numeric)
w.time_window_end::numeric) &&
NUMRANGE(%(start)s::numeric, %(end)s::numeric)
AND w.id != %(window_id)s
AND d.%(relation_week_day_fkey)s in %(weekday_ids)s
AND w.%(check_field)s = %(check_field_id)s;"""
Expand All @@ -79,27 +77,25 @@ def check_window_no_overlaps(self):
if res:
other = self.browse(res[0][0])
raise ValidationError(
_("%(record_name)s overlaps %(other_name)s")
% (
{
"record_name": record.display_name,
"other_name": other.display_name,
}
self.env._(
"%(record_name)s overlaps %(other_name)s",
record_name=record.display_name,
other_name=other.display_name,
)
)

@api.depends("time_window_start", "time_window_end", "time_window_weekday_ids")
def _compute_display_name(self):
for record in self:
record.display_name = _("{days}: From {start} to {end}").format(
record.display_name = self.env._("{days}: From {start} to {end}").format(
days=", ".join(record.time_window_weekday_ids.mapped("display_name")),
start=format_time(self.env, record.get_time_window_start_time()),
end=format_time(self.env, record.get_time_window_end_time()),
)

@api.constrains("time_window_start", "time_window_end")
def _check_window_under_twenty_four_hours(self):
error_msg = _("Hour should be between 00 and 23")
error_msg = self.env._("Hour should be between 00 and 23")
for record in self:
if record.time_window_start:
hour, minute = self._get_hour_min_from_value(record.time_window_start)
Expand Down
1 change: 1 addition & 0 deletions base_time_window/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
Trobz

- Dung Tran \<<[email protected]>\>
- Khoi (Kien Kim) \<<[email protected]>\>
4 changes: 4 additions & 0 deletions base_time_window/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
The development of this module has been financially supported by:

- Camptocamp

The migration of this module from 17.0 to 18.0 was financially supported by:

- Camptocamp
6 changes: 6 additions & 0 deletions base_time_window/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<p>Trobz</p>
<ul class="simple">
<li>Dung Tran &lt;<a class="reference external" href="mailto:dungtd&#64;trobz.com">dungtd&#64;trobz.com</a>&gt;</li>
<li>Khoi (Kien Kim) &lt;<a class="reference external" href="mailto:khoikk&#64;trobz.com">khoikk&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand All @@ -470,6 +471,11 @@ <h2><a class="toc-backref" href="#toc-entry-7">Other credits</a></h2>
<ul class="simple">
<li>Camptocamp</li>
</ul>
<p>The migration of this module from 17.0 to 18.0 was financially supported
by:</p>
<ul class="simple">
<li>Camptocamp</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
Expand Down
1 change: 0 additions & 1 deletion base_time_window/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from . import test_time_weekday
from . import test_time_window_mixin
from . import test_models
17 changes: 11 additions & 6 deletions base_time_window/tests/test_time_window_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ class TestTimeWindowMixin(TransactionCase):
def setUpClass(cls):
super().setUpClass()

cls.loader = FakeModelLoader(cls.env, cls.__module__)
cls.loader.backup_registry()
from .test_models import TestTimeWindowModel

cls.loader.update_registry((TestTimeWindowModel,))

cls.customer1 = cls.env["res.partner"].create({"name": "Test1"})
cls.customer2 = cls.env["res.partner"].create({"name": "Test2"})
cls.customer3 = cls.env["res.partner"].create({"name": "Test3"})

cls.weekday1 = cls.env["time.weekday"].search([("name", "=", "1")])
cls.weekday2 = cls.env["time.weekday"].search([("name", "=", "2")])

cls.loader = FakeModelLoader(cls.env, cls.__module__)
cls.loader.backup_registry()
from .test_models import TestTimeWindowModel

cls.loader.update_registry((TestTimeWindowModel,))

@classmethod
def tearDownClass(cls):
cls.loader.restore_registry()
super().tearDownClass()

def test_time_window_no_overlap(self):
with self.assertRaises(ValidationError):
self.record1 = self.env["test.time.window.model"].create(
Expand Down

0 comments on commit 7a2c151

Please sign in to comment.