diff --git a/date_range/README.rst b/date_range/README.rst index 0966a06431..34a79a9673 100644 --- a/date_range/README.rst +++ b/date_range/README.rst @@ -29,7 +29,7 @@ Date Range |badge1| |badge2| |badge3| |badge4| |badge5| This module lets you define global date ranges that can be used to -filter your values in tree views. +filter your values in list views. It also provides a mixin model for developers that extends the model's search view so that date ranges can be search as any relational field. @@ -79,50 +79,49 @@ Usage To configure this module, you need to: -- Go to Settings > Technical > Date ranges > Date Range Types where you - can create types of date ranges. +- Go to Settings > Technical > Date ranges > Date Range Types where you + can create types of date ranges. - |image1| + |image1| -- Go to Settings > Technical > Date ranges > Date Ranges where you can - create date ranges. +- Go to Settings > Technical > Date ranges > Date Ranges where you can + create date ranges. - |image2| + |image2| - It's also possible to launch a wizard from the 'Generate Date Ranges' - menu. + It's also possible to launch a wizard from the 'Generate Date Ranges' + menu. - |image3| + |image3| - The wizard is useful to generate recurring periods. Set an end date - or enter the number of ranges to create. + The wizard is useful to generate recurring periods. Set an end date or + enter the number of ranges to create. - |image4| + |image4| -- Your date ranges are now available in the search filter for any date - or datetime fields +- Your date ranges are now available in the search filter for any date + or datetime fields - Date range types are proposed as a filter operator + Date range types are proposed as a filter operator - |image5| + |image5| - Once a type is selected, date ranges of this type are porposed as a - filter value + Once a type is selected, date ranges of this type are porposed as a + filter value - |image6| + |image6| - And the dates specified into the date range are used to filter your - result. + And the dates specified into the date range are used to filter your + result. - |image7| + |image7| -- You can configure date range types with default values for the - generation wizard on the Generation tab. In the same tab you can also - configure date range types for auto-generation. New ranges for types - configured for this are generated by a scheduled task that runs - daily. +- You can configure date range types with default values for the + generation wizard on the Generation tab. In the same tab you can also + configure date range types for auto-generation. New ranges for types + configured for this are generated by a scheduled task that runs daily. - |image8| + |image8| .. |image1| image:: https://raw.githubusercontent.com/OCA/server-ux/18.0/date_range/static/description/date_range_type_create.png .. |image2| image:: https://raw.githubusercontent.com/OCA/server-ux/18.0/date_range/static/description/date_range_create.png @@ -154,14 +153,16 @@ Authors Contributors ------------ -- Laurent Mignon -- Alexis de Lattre -- Miquel Raïch -- Andrea Stirpe -- Stefan Rijnhart -- David Ramia <<@ramiadavid>> -- Son Ho -- Bert Van Groenendael +- Laurent Mignon +- Alexis de Lattre +- Miquel Raïch +- Andrea Stirpe +- Stefan Rijnhart +- David Ramia <<@ramiadavid>> +- Son Ho +- Bert Van Groenendael +- Andrii Kompaniiets +- Rafael Blasco Maintainers ----------- diff --git a/date_range/__manifest__.py b/date_range/__manifest__.py index 5262af0e43..008027fd5c 100644 --- a/date_range/__manifest__.py +++ b/date_range/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Date Range", "summary": "Manage all kind of date range", - "version": "18.0.1.0.0", + "version": "18.0.2.0.0", "category": "Uncategorized", "website": "https://github.com/OCA/server-ux", "author": "ACSONE SA/NV, Odoo Community Association (OCA)", diff --git a/date_range/migrations/18.0.2.0.0/post-migration.py b/date_range/migrations/18.0.2.0.0/post-migration.py new file mode 100644 index 0000000000..a5d3f56762 --- /dev/null +++ b/date_range/migrations/18.0.2.0.0/post-migration.py @@ -0,0 +1,18 @@ +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.delete_sql_constraint_safely( + env, "date_range", "date_range", "date_range_uniq" + ) + openupgrade.m2o_to_x2m( + env.cr, env["date.range"], "date_range", "company_ids", "company_id" + ) + env.cr.execute( + "UPDATE ir_rule SET domain_force = %s WHERE name = %s", + ( + "['|',('company_ids', 'in', company_ids),('company_ids','=',False)]", + "Date Range multi-company", + ), + ) diff --git a/date_range/models/date_range.py b/date_range/models/date_range.py index 8520b66600..a217fc5cf1 100644 --- a/date_range/models/date_range.py +++ b/date_range/models/date_range.py @@ -2,7 +2,7 @@ # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). from odoo import api, fields, models -from odoo.exceptions import ValidationError +from odoo.exceptions import UserError, ValidationError class DateRange(models.Model): @@ -26,10 +26,9 @@ def _default_company(self): ondelete="restrict", check_company=True, ) - company_id = fields.Many2one( - comodel_name="res.company", - string="Company", - index=True, + company_ids = fields.Many2many( + "res.company", + string="Companies", default=_default_company, ) active = fields.Boolean( @@ -40,14 +39,6 @@ def _default_company(self): store=True, ) - _sql_constraints = [ - ( - "date_range_uniq", - "unique (name,type_id, company_id)", - "A date range must be unique per company !", - ) - ] - @api.depends("type_id.active") def _compute_active(self): for date in self: @@ -56,7 +47,7 @@ def _compute_active(self): else: date.active = False - @api.constrains("type_id", "date_start", "date_end", "company_id") + @api.constrains("type_id", "date_start", "date_end", "company_ids") def _validate_range(self): for this in self: if this.date_start > this.date_end: @@ -73,6 +64,18 @@ def _validate_range(self): ) if this.type_id.allow_overlap: continue + if bool( + this.type_id.company_id + and this.type_id.company_id not in this.company_ids + ): + raise UserError( + self.env._( + "The Date Range Type %(drt_name)s assigned to other " + "company, you can't change the company in " + "Date Range %(dt_name)s" + ) + % {"drt_name": this.type_id.name, "dt_name": this.name} + ) # here we use a plain SQL query to benefit of the daterange # function available in PostgresSQL # (http://www.postgresql.org/docs/current/static/rangetypes.html) @@ -81,12 +84,14 @@ def _validate_range(self): id FROM date_range dt + INNER JOIN date_range_res_company_rel as rc + ON dt.id = rc.date_range_id WHERE DATERANGE(dt.date_start, dt.date_end, '[]') && DATERANGE(%s::date, %s::date, '[]') AND dt.id != %s AND dt.active - AND dt.company_id = %s + AND rc.res_company_id IN %s AND dt.type_id=%s;""" self.env.cr.execute( SQL, @@ -94,7 +99,7 @@ def _validate_range(self): this.date_start, this.date_end, this.id, - this.company_id.id or None, + tuple(this.company_ids.ids) or (None,), this.type_id.id, ), ) diff --git a/date_range/models/date_range_type.py b/date_range/models/date_range_type.py index 5f068d0c6f..7560b639a1 100644 --- a/date_range/models/date_range_type.py +++ b/date_range/models/date_range_type.py @@ -84,8 +84,8 @@ def _check_company_id(self): continue if bool( rec.date_range_ids.filtered( - lambda r, drt=rec: r.company_id - and r.company_id != drt.company_id + lambda r, drt=rec: r.company_ids + and drt.company_id not in r.company_ids ) ): raise ValidationError( diff --git a/date_range/readme/CONTRIBUTORS.md b/date_range/readme/CONTRIBUTORS.md index 96629c1413..14a65ac2a0 100644 --- a/date_range/readme/CONTRIBUTORS.md +++ b/date_range/readme/CONTRIBUTORS.md @@ -6,3 +6,5 @@ - David Ramia \<<@ramiadavid>\> - Son Ho \<\> - Bert Van Groenendael \<\> +- Andrii Kompaniiets \<\> +- Rafael Blasco \<\> diff --git a/date_range/readme/DESCRIPTION.md b/date_range/readme/DESCRIPTION.md index d8e60bf0e0..b9f5d36de5 100644 --- a/date_range/readme/DESCRIPTION.md +++ b/date_range/readme/DESCRIPTION.md @@ -1,5 +1,5 @@ This module lets you define global date ranges that can be used to -filter your values in tree views. +filter your values in list views. It also provides a mixin model for developers that extends the model's search view so that date ranges can be search as any relational field. diff --git a/date_range/security/date_range_security.xml b/date_range/security/date_range_security.xml index 0b98402ca3..55871103d2 100644 --- a/date_range/security/date_range_security.xml +++ b/date_range/security/date_range_security.xml @@ -11,7 +11,7 @@ Date Range multi-company - ['|',('company_id', 'in', company_ids),('company_id','=',False)] + ['|',('company_ids', 'in', company_ids),('company_ids','=',False)] diff --git a/date_range/static/description/index.html b/date_range/static/description/index.html index fb7b36332b..6f9090bf70 100644 --- a/date_range/static/description/index.html +++ b/date_range/static/description/index.html @@ -371,7 +371,7 @@

Date Range

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Mature License: LGPL-3 OCA/server-ux Translate me on Weblate Try me on Runboat

This module lets you define global date ranges that can be used to -filter your values in tree views.

+filter your values in list views.

It also provides a mixin model for developers that extends the model’s search view so that date ranges can be search as any relational field.

Table of contents

@@ -429,8 +429,8 @@

Usage

It’s also possible to launch a wizard from the ‘Generate Date Ranges’ menu.

image3

-

The wizard is useful to generate recurring periods. Set an end date -or enter the number of ranges to create.

+

The wizard is useful to generate recurring periods. Set an end date or +enter the number of ranges to create.

image4

  • Your date ranges are now available in the search filter for any date @@ -447,8 +447,7 @@

    Usage

  • You can configure date range types with default values for the generation wizard on the Generation tab. In the same tab you can also configure date range types for auto-generation. New ranges for types -configured for this are generated by a scheduled task that runs -daily.

    +configured for this are generated by a scheduled task that runs daily.

    image8

  • @@ -480,6 +479,8 @@

    Contributors

  • David Ramia <<@ramiadavid>>
  • Son Ho <sonhd@trobz.com>
  • Bert Van Groenendael <bert.vangroenendael@dynapps.eu>
  • +
  • Andrii Kompaniiets <andrii@moduon.team>
  • +
  • Rafael Blasco <rblasco@moduon.team>
  • diff --git a/date_range/tests/test_date_range.py b/date_range/tests/test_date_range.py index bfa5606d99..da6dbc8747 100644 --- a/date_range/tests/test_date_range.py +++ b/date_range/tests/test_date_range.py @@ -3,6 +3,7 @@ import datetime +from odoo import Command from odoo.exceptions import UserError, ValidationError from odoo.tests.common import TransactionCase @@ -36,7 +37,7 @@ def test_default_company(self): "type_id": self.type.id, } ) - self.assertTrue(dr.company_id) + self.assertTrue(dr.company_ids) # you can specify company_id to False dr = self.date_range.create( { @@ -44,10 +45,10 @@ def test_default_company(self): "date_start": "2015-01-01", "date_end": "2016-12-31", "type_id": self.type.id, - "company_id": False, + "company_ids": False, } ) - self.assertFalse(dr.company_id) + self.assertFalse(dr.company_ids) def test_empty_company(self): dr = self.date_range.create( @@ -56,7 +57,7 @@ def test_empty_company(self): "date_start": "2015-01-01", "date_end": "2016-12-31", "type_id": self.type.id, - "company_id": None, + "company_ids": None, } ) self.assertEqual(dr.name, "FS2016") @@ -135,6 +136,6 @@ def test_date_range_multicompany_1(self): "date_start": "2015-01-01", "date_end": "2016-12-31", "type_id": self.typeB.id, - "company_id": self.company_2.id, + "company_ids": [Command.set(self.company_2.ids)], } ) diff --git a/date_range/tests/test_date_range_type.py b/date_range/tests/test_date_range_type.py index 5903125c62..39399fe543 100644 --- a/date_range/tests/test_date_range_type.py +++ b/date_range/tests/test_date_range_type.py @@ -5,7 +5,7 @@ from dateutil.rrule import MONTHLY, YEARLY from psycopg2 import IntegrityError -from odoo import fields +from odoo import Command, fields from odoo.exceptions import UserError from odoo.tests.common import TransactionCase from odoo.tools import mute_logger @@ -55,12 +55,12 @@ def test_type_multicompany(self): "date_start": "2015-01-01", "date_end": "2016-12-31", "type_id": drt.id, - "company_id": self.company.id, + "company_ids": [Command.set(self.company.ids)], } ) drt.company_id = self.company.id with self.assertRaises(UserError): - dr.company_id = self.company_2 + dr.company_ids = self.company_2.ids def test_autogeneration(self): """Ranges are autogenerated for types configured for that""" diff --git a/date_range/views/date_range_view.xml b/date_range/views/date_range_view.xml index a2c8c7cd5b..a298c7b5e0 100644 --- a/date_range/views/date_range_view.xml +++ b/date_range/views/date_range_view.xml @@ -10,11 +10,12 @@ - + @@ -37,11 +38,12 @@ - + @@ -77,7 +79,7 @@ diff --git a/date_range/wizard/date_range_generator.py b/date_range/wizard/date_range_generator.py index a17d6a55c4..33a5d3f1c0 100644 --- a/date_range/wizard/date_range_generator.py +++ b/date_range/wizard/date_range_generator.py @@ -6,7 +6,7 @@ from dateutil.relativedelta import relativedelta from dateutil.rrule import DAILY, MONTHLY, WEEKLY, YEARLY, rrule -from odoo import api, fields, models +from odoo import Command, api, fields, models from odoo.exceptions import UserError, ValidationError from odoo.tools.safe_eval import safe_eval @@ -232,7 +232,7 @@ def _generate_date_ranges(self, batch=False): "date_start": date_start, "date_end": date_end, "type_id": self.type_id.id, - "company_id": self.company_id.id, + "company_ids": [Command.set(self.company_id.ids)], } ) return date_ranges