diff --git a/base_week_day/README.rst b/base_week_day/README.rst new file mode 100644 index 0000000000..bfed84d371 --- /dev/null +++ b/base_week_day/README.rst @@ -0,0 +1,91 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +==================== +Base for Day of Week +==================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:62241057d7b23cb1e6849a9079e04c6705e61a842c418a24dc5919de41c18cc9 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--ux-lightgray.png?logo=github + :target: https://github.com/OCA/server-ux/tree/14.0/base_week_day + :alt: OCA/server-ux +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-ux-14-0/server-ux-14-0-base_week_day + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/server-ux&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Simple technical module to get the Day of Week from records. +This module does nothing visible to the user on its own, but is used as base for other modules, like `sale_order_week_day`. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* PyTech + +Contributors +~~~~~~~~~~~~ + +* `PyTech `_: + + * Simone Rubino + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-SirPyTech| image:: https://github.com/SirPyTech.png?size=40px + :target: https://github.com/SirPyTech + :alt: SirPyTech + +Current `maintainer `__: + +|maintainer-SirPyTech| + +This module is part of the `OCA/server-ux `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_week_day/__init__.py b/base_week_day/__init__.py new file mode 100644 index 0000000000..31660d6a96 --- /dev/null +++ b/base_week_day/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models diff --git a/base_week_day/__manifest__.py b/base_week_day/__manifest__.py new file mode 100644 index 0000000000..7f7cee097e --- /dev/null +++ b/base_week_day/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2026 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Base for Day of Week", + "summary": "Base module for Day of Week computation.", + "version": "14.0.1.0.0", + "author": "PyTech, Odoo Community Association (OCA)", + "maintainers": [ + "SirPyTech", + ], + "website": "https://github.com/OCA/server-ux", + "license": "AGPL-3", + "depends": [ + "base", + ], + "external_dependencies": { + "python": [ + "openupgradelib", + ], + }, +} diff --git a/base_week_day/hooks.py b/base_week_day/hooks.py new file mode 100644 index 0000000000..4c937a71a5 --- /dev/null +++ b/base_week_day/hooks.py @@ -0,0 +1,53 @@ +# Copyright 2026 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from openupgradelib import openupgrade +from psycopg2.extensions import AsIs + + +def populate_date_order_week_day( + env, + module, + model_name, + dow_field_name, + date_field_name, +): + """Populate the new field `model_name.dow_field_name`. + + The computation is based on the week day of `model_name.date_field_name`. + Use SQL to avoid Memory/Timeout errors when there are a lot of records. + + This is not used in this module but can be used in inheriting modules. + """ + model = env[model_name] + + if not openupgrade.column_exists(env.cr, model._table, dow_field_name): + openupgrade.add_fields( + env, + [ + ( + dow_field_name, + model._name, + model._table, + "selection", + None, + module, + ) + ], + ) + + openupgrade.logged_query( + env.cr, + """ + UPDATE %(model_table)s so + SET + %(dow_field_name)s = (EXTRACT(ISODOW FROM %(date_field_name)s) - 1)::TEXT + WHERE + %(date_field_name)s IS NOT NULL + """, + { + "model_table": AsIs(model._table), + "dow_field_name": AsIs(dow_field_name), + "date_field_name": AsIs(date_field_name), + }, + ) diff --git a/base_week_day/models/__init__.py b/base_week_day/models/__init__.py new file mode 100644 index 0000000000..6eb3322bb6 --- /dev/null +++ b/base_week_day/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import mixin diff --git a/base_week_day/models/mixin.py b/base_week_day/models/mixin.py new file mode 100644 index 0000000000..2e09b5f1f3 --- /dev/null +++ b/base_week_day/models/mixin.py @@ -0,0 +1,23 @@ +# Copyright 2026 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + +WEEKDAYS = [ + ("0", "Monday"), + ("1", "Tuesday"), + ("2", "Wednesday"), + ("3", "Thursday"), + ("4", "Friday"), + ("5", "Saturday"), + ("6", "Sunday"), +] +# Use for selection fields + + +class WeekDayMixin(models.AbstractModel): + _name = "base_week_day.mixin" + _description = "Day of Week computation" + + def _get_week_day(self, value): + return str(value.weekday()) if value else None diff --git a/base_week_day/readme/CONTRIBUTORS.rst b/base_week_day/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..3e2b10402d --- /dev/null +++ b/base_week_day/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `PyTech `_: + + * Simone Rubino diff --git a/base_week_day/readme/DESCRIPTION.rst b/base_week_day/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..7ec9a4b076 --- /dev/null +++ b/base_week_day/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +Simple technical module to get the Day of Week from records. +This module does nothing visible to the user on its own, but is used as base for other modules, like `sale_order_week_day`. diff --git a/base_week_day/static/description/index.html b/base_week_day/static/description/index.html new file mode 100644 index 0000000000..3b15a557bc --- /dev/null +++ b/base_week_day/static/description/index.html @@ -0,0 +1,435 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

Base for Day of Week

+ +

Beta License: AGPL-3 OCA/server-ux Translate me on Weblate Try me on Runboat

+

Simple technical module to get the Day of Week from records. +This module does nothing visible to the user on its own, but is used as base for other modules, like sale_order_week_day.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • PyTech
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

SirPyTech

+

This module is part of the OCA/server-ux project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/base_week_day/tests/__init__.py b/base_week_day/tests/__init__.py new file mode 100644 index 0000000000..cbee137581 --- /dev/null +++ b/base_week_day/tests/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_hooks +from . import test_mixin diff --git a/base_week_day/tests/common.py b/base_week_day/tests/common.py new file mode 100644 index 0000000000..2b2d249fed --- /dev/null +++ b/base_week_day/tests/common.py @@ -0,0 +1,31 @@ +# Copyright 2026 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import datetime + +from odoo_test_helper import FakeModelLoader + +from odoo import tests + + +class Common(tests.SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.loader = FakeModelLoader(cls.env, cls.__module__) + cls.loader.backup_registry() + + from .models import TestDateModel + + cls.loader.update_registry((TestDateModel,)) + cls.addClassCleanup(cls.loader.restore_registry) + + cls.records = cls.env["base_week_day.test.date_model"].create( + [ + { + "some_date": datetime.date(2020, 1, day_number), + "some_datetime": datetime.datetime(2020, 1, day_number), + } + for day_number in range(1, 10) + ] + ) diff --git a/base_week_day/tests/models.py b/base_week_day/tests/models.py new file mode 100644 index 0000000000..65c2e76e0e --- /dev/null +++ b/base_week_day/tests/models.py @@ -0,0 +1,31 @@ +# Copyright 2026 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + +from odoo.addons.base_week_day.models.mixin import WEEKDAYS + + +class TestDateModel(models.Model): + _inherit = "base_week_day.mixin" + _name = "base_week_day.test.date_model" + _description = "Test model with date fields" + + some_date = fields.Date() + some_date_week_day = fields.Selection( + selection=WEEKDAYS, + compute="_compute_some_date_week_day", + ) + some_datetime = fields.Datetime() + some_datetime_week_day = fields.Selection( + selection=WEEKDAYS, + compute="_compute_some_datetime_week_day", + ) + + def _compute_some_date_week_day(self): + for record in self: + record.some_date_week_day = record._get_week_day(record.some_date) + + def _compute_some_datetime_week_day(self): + for record in self: + record.some_datetime_week_day = record._get_week_day(record.some_datetime) diff --git a/base_week_day/tests/test_hooks.py b/base_week_day/tests/test_hooks.py new file mode 100644 index 0000000000..e08e66e86a --- /dev/null +++ b/base_week_day/tests/test_hooks.py @@ -0,0 +1,35 @@ +# Copyright 2026 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from ..hooks import populate_date_order_week_day +from .common import Common + + +class TestHooks(Common): + def test_populate(self): + """The Day of Week is populated correctly.""" + self.records.update( + { + "some_date_week_day": False, + } + ) + + populate_date_order_week_day( + self.env, + "base_week_day", + self.records._name, + "some_date_week_day", + "some_date", + ) + + self.records.invalidate_cache( + fnames=["some_date_week_day"], + ids=self.records.ids, + ) + self.assertRecordValues( + self.records, + [ + {"some_date_week_day": str(record.some_date.weekday())} + for record in self.records + ], + ) diff --git a/base_week_day/tests/test_mixin.py b/base_week_day/tests/test_mixin.py new file mode 100644 index 0000000000..f4c5190935 --- /dev/null +++ b/base_week_day/tests/test_mixin.py @@ -0,0 +1,19 @@ +# Copyright 2026 Simone Rubino - PyTech +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from .common import Common + + +class TestMixin(Common): + def test_week_day(self): + """The Day of Week is computed correctly.""" + self.assertRecordValues( + self.records, + [ + { + "some_date_week_day": str(record.some_date.weekday()), + "some_datetime_week_day": str(record.some_datetime.weekday()), + } + for record in self.records + ], + ) diff --git a/requirements.txt b/requirements.txt index 2186a97932..98691ceb99 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ # generated from manifests external_dependencies +openupgradelib pdf2image pyzbar diff --git a/setup/base_week_day/odoo/addons/base_week_day b/setup/base_week_day/odoo/addons/base_week_day new file mode 120000 index 0000000000..ea8d11b63c --- /dev/null +++ b/setup/base_week_day/odoo/addons/base_week_day @@ -0,0 +1 @@ +../../../../base_week_day \ No newline at end of file diff --git a/setup/base_week_day/setup.py b/setup/base_week_day/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/base_week_day/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)