From 3af6b1d7f4be88463c7593ed960d893035f6ed01 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Tue, 16 Jul 2024 21:03:12 +0200 Subject: [PATCH] Support dynamic default time from another field --- web_datetime_picker_default_time/README.rst | 22 +- .../__manifest__.py | 3 +- .../readme/USAGE.md | 22 +- .../static/description/index.html | 457 ++++++++++++++++++ .../static/src/js/datepicker.esm.js | 4 +- .../static/src/js/datetime_field.esm.js | 42 ++ .../static/src/xml/datetime_field.xml | 8 + 7 files changed, 548 insertions(+), 10 deletions(-) create mode 100644 web_datetime_picker_default_time/static/description/index.html create mode 100644 web_datetime_picker_default_time/static/src/js/datetime_field.esm.js create mode 100644 web_datetime_picker_default_time/static/src/xml/datetime_field.xml diff --git a/web_datetime_picker_default_time/README.rst b/web_datetime_picker_default_time/README.rst index ac0fe1a53..529a383db 100644 --- a/web_datetime_picker_default_time/README.rst +++ b/web_datetime_picker_default_time/README.rst @@ -45,13 +45,27 @@ browser. Usage ===== -You can define the default time as follows : +You can define the default time as follows for a static value: -.. code-block:: xml +.. code:: xml -.. raw:: html + - +Otherwise you can also use a JSON field to make it dynamic through a +compute function, and reference this field in the view: + +.. code:: python + + start_time = field.Json(compute="_compute_start_time") + + def _compute_start_time(self): + for rec in self: + rec.start_time = {'hour': 8, 'minute': 30, 'second': 15 } + +.. code:: xml + + + Known issues / Roadmap ====================== diff --git a/web_datetime_picker_default_time/__manifest__.py b/web_datetime_picker_default_time/__manifest__.py index fab4d6e85..98d289a2b 100644 --- a/web_datetime_picker_default_time/__manifest__.py +++ b/web_datetime_picker_default_time/__manifest__.py @@ -14,7 +14,8 @@ ], "assets": { "web.assets_backend": [ - "/web_datetime_picker_default_time/static/src/js/datepicker.esm.js", + "/web_datetime_picker_default_time/static/src/js/*.js", + "/web_datetime_picker_default_time/static/src/xml/*.xml", ], }, } diff --git a/web_datetime_picker_default_time/readme/USAGE.md b/web_datetime_picker_default_time/readme/USAGE.md index 3e66343a5..03e2fc174 100644 --- a/web_datetime_picker_default_time/readme/USAGE.md +++ b/web_datetime_picker_default_time/readme/USAGE.md @@ -1,5 +1,21 @@ -You can define the default time as follows : +You can define the default time as follows for a static value: -.. code-block:: xml +```xml + +``` - +Otherwise you can also use a JSON field to make it dynamic through a compute function, +and reference this field in the view: + +```python + start_time = field.Json(compute="_compute_start_time") + + def _compute_start_time(self): + for rec in self: + rec.start_time = {'hour': 8, 'minute': 30, 'second': 15 } +``` + +```xml + + +``` diff --git a/web_datetime_picker_default_time/static/description/index.html b/web_datetime_picker_default_time/static/description/index.html new file mode 100644 index 000000000..f87f617e4 --- /dev/null +++ b/web_datetime_picker_default_time/static/description/index.html @@ -0,0 +1,457 @@ + + + + + +Web Datetime Picker Default Time + + + +
+

Web Datetime Picker Default Time

+ + +

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

+

This module customizes the datetime picker widget and allows to define a +default time to be applied in case the user selects only a Date.

+

For example, if a user wants to define a commitment date without having +to specify the time on that date, setting the default time value on the +field in the Form view allows to ensure the commitment date will be set +to this time instead of the time when the page was loaded by the +browser.

+

Table of contents

+ +
+

Usage

+

You can define the default time as follows for a static value:

+
+<field name="your_datetime_field" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
+
+

Otherwise you can also use a JSON field to make it dynamic through a +compute function, and reference this field in the view:

+
+start_time = field.Json(compute="_compute_start_time")
+
+def _compute_start_time(self):
+    for rec in self:
+        rec.start_time = {'hour': 8, 'minute': 30, 'second': 15 }
+
+
+<field name="start_time" invisible="1" />
+<field name="your_datetime_field" options="{'defaultTime': 'start_time'}"/>
+
+
+
+

Known issues / Roadmap

+
    +
  • Handle Timezone related to the default time
  • +
+
+
+

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

+
    +
  • Camptocamp
  • +
+
+ +
+

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:

+

grindtildeath

+

This module is part of the OCA/web project on GitHub.

+

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

+
+
+
+ + diff --git a/web_datetime_picker_default_time/static/src/js/datepicker.esm.js b/web_datetime_picker_default_time/static/src/js/datepicker.esm.js index c23e7280c..65f5425d6 100644 --- a/web_datetime_picker_default_time/static/src/js/datepicker.esm.js +++ b/web_datetime_picker_default_time/static/src/js/datepicker.esm.js @@ -2,7 +2,7 @@ /* Copyright 2024 Camptocamp * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) */ -import {DatePicker, DateTimePicker} from "@web/core/datepicker/datepicker"; +import {DateTimePicker} from "@web/core/datepicker/datepicker"; import {patch} from "@web/core/utils/patch"; patch(DateTimePicker.prototype, "DateTimePickerDefaultTime", { @@ -23,7 +23,7 @@ patch(DateTimePicker.prototype, "DateTimePickerDefaultTime", { }, }); -DateTimePicker.props = _.extend({}, DatePicker.props, { +DateTimePicker.props = _.extend({}, DateTimePicker.props, { defaultTime: { type: Object, shape: { diff --git a/web_datetime_picker_default_time/static/src/js/datetime_field.esm.js b/web_datetime_picker_default_time/static/src/js/datetime_field.esm.js new file mode 100644 index 000000000..58f46609f --- /dev/null +++ b/web_datetime_picker_default_time/static/src/js/datetime_field.esm.js @@ -0,0 +1,42 @@ +/** @odoo-module **/ +/* Copyright 2024 Camptocamp + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) */ + +import {DateTimeField} from "@web/views/fields/datetime/datetime_field"; +import {patch} from "@web/core/utils/patch"; + +patch(DateTimeField.prototype, "DateTimeFieldDefaultTime", { + get defaultTime() { + if (typeof this.props.defaultTime === "string") { + return this.props.record.data[this.props.defaultTime]; + } + return this.props.defaultTime; + }, +}); + +DateTimeField.props = _.extend({}, DateTimeField.props, { + defaultTime: { + type: [ + String, + { + type: Object, + shape: { + hour: Number, + minute: Number, + second: Number, + }, + optional: true, + }, + ], + optional: true, + }, +}); + +const super_extractProps = DateTimeField.extractProps; + +DateTimeField.extractProps = ({attrs}) => { + return { + ...super_extractProps({attrs}), + defaultTime: attrs.options.defaultTime, + }; +}; diff --git a/web_datetime_picker_default_time/static/src/xml/datetime_field.xml b/web_datetime_picker_default_time/static/src/xml/datetime_field.xml new file mode 100644 index 000000000..df9affad0 --- /dev/null +++ b/web_datetime_picker_default_time/static/src/xml/datetime_field.xml @@ -0,0 +1,8 @@ + + + + + defaultTime + + +