From c5d68e8f26b5e226d4f1da74e9b905dedf3450d5 Mon Sep 17 00:00:00 2001 From: nfluckiger Date: Fri, 3 Nov 2023 10:10:10 +0100 Subject: [PATCH] T0424 FEAT: Add field to the tag to help identify them We are adding some fields to give more information to the tag. The main 3 are the author, the department who use the tag and a description. We add those to all tag and show them in the list view. To add the department we need to add a new dependence the hr base module. We add a new value to the smart part of the tag. This new value give the possibility to give a end date to a tag. We had a cron who archive tag who are older than their valid date. --- partner_tag_smart_assignation/__manifest__.py | 4 +- .../cron/update_cron.xml | 12 ------ .../models/__init__.py | 2 +- .../models/hr_department.py | 12 ++++++ .../models/res_partner_category.py | 17 ++++++++ .../models/res_partner_category_extension.py | 22 ----------- .../tests/test_smart_tagger.py | 39 +++++++++---------- .../res_partner_category_view_extension.xml | 29 -------------- .../views/smart_tagger_view.xml | 10 +++++ 9 files changed, 61 insertions(+), 86 deletions(-) create mode 100644 partner_tag_smart_assignation/models/hr_department.py delete mode 100644 partner_tag_smart_assignation/models/res_partner_category_extension.py delete mode 100644 partner_tag_smart_assignation/views/res_partner_category_view_extension.xml diff --git a/partner_tag_smart_assignation/__manifest__.py b/partner_tag_smart_assignation/__manifest__.py index a89f90e3234b..831c1977316b 100644 --- a/partner_tag_smart_assignation/__manifest__.py +++ b/partner_tag_smart_assignation/__manifest__.py @@ -8,8 +8,8 @@ "author": "Compassion CH, Odoo Community Association (OCA)", "license": "AGPL-3", "website": "https://github.com/OCA/partner-contact", - "depends": ["base"], - "data": ["cron/update_cron.xml", "views/smart_tagger_view.xml", "views/res_partner_category_view_extension.xml"], + "depends": ["base", "hr"], + "data": ["cron/update_cron.xml", "views/smart_tagger_view.xml"], "installable": True, "auto_install": False, } diff --git a/partner_tag_smart_assignation/cron/update_cron.xml b/partner_tag_smart_assignation/cron/update_cron.xml index fbd7fe2ef5cf..222d61eaa21f 100644 --- a/partner_tag_smart_assignation/cron/update_cron.xml +++ b/partner_tag_smart_assignation/cron/update_cron.xml @@ -11,16 +11,4 @@ code model.update_all_smart_tags() - - - Check Validity Dates - - - 1 - days - -1 - - code - model._check_validity_dates() - diff --git a/partner_tag_smart_assignation/models/__init__.py b/partner_tag_smart_assignation/models/__init__.py index b8a18026177f..3bf4f053c285 100644 --- a/partner_tag_smart_assignation/models/__init__.py +++ b/partner_tag_smart_assignation/models/__init__.py @@ -1,4 +1,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). from . import res_partner_category -from . import res_partner_category_extension +from . import hr_department diff --git a/partner_tag_smart_assignation/models/hr_department.py b/partner_tag_smart_assignation/models/hr_department.py new file mode 100644 index 000000000000..83334e0ad249 --- /dev/null +++ b/partner_tag_smart_assignation/models/hr_department.py @@ -0,0 +1,12 @@ +# Copyright (C) 2019 Compassion CH (http://www.compassion.ch) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from odoo import fields, models, api + + +class ResPartnerCategoryExtension(models.Model): + _inherit = "hr.department" + + tag_ids = fields.Many2many("res.partner.category") + + + diff --git a/partner_tag_smart_assignation/models/res_partner_category.py b/partner_tag_smart_assignation/models/res_partner_category.py index 2c0946f877b8..4e3772926808 100644 --- a/partner_tag_smart_assignation/models/res_partner_category.py +++ b/partner_tag_smart_assignation/models/res_partner_category.py @@ -43,6 +43,11 @@ class ResPartnerCategory(models.Model): tagged_partner_count = fields.Integer(compute="_compute_number_tags", stored=True) + author_id = fields.Many2one('res.users', string="Author", default=lambda x: x.env.user) + department_ids = fields.Many2many('hr.department', string="Department") + description = fields.Text() + valid_until = fields.Date() + @api.model def create(self, vals): record = super().create(vals) @@ -134,8 +139,20 @@ def get_partners_from_sql(self): @api.model def update_all_smart_tags(self): + self._check_validity_dates() return self.search([("smart", "=", True)]).update_partner_tags() + @api.model + def _check_validity_dates(self): + """ Scheduled method to deactivate records past their validity date """ + today = fields.Date.today() + records_to_deactivate = self.search([('valid_until', '<', today), ('active', '=', True), ('smart', '=', True)]) + # Archive the tag and unlink the partner + records_to_deactivate.write({ + 'partner_ids': [(5, 0, 0)], + 'active': False + }) + @api.depends("partner_ids") def _compute_number_tags(self): for category in self: diff --git a/partner_tag_smart_assignation/models/res_partner_category_extension.py b/partner_tag_smart_assignation/models/res_partner_category_extension.py deleted file mode 100644 index 3118c2c705da..000000000000 --- a/partner_tag_smart_assignation/models/res_partner_category_extension.py +++ /dev/null @@ -1,22 +0,0 @@ -from odoo import fields, models, api - - -class ResPartnerCategoryExtension(models.Model): - _inherit = "res.partner.category" - - author_of_the_tag = fields.Many2one('res.users', string="Author") - department_that_uses_the_tag = fields.Many2one('hr.department', string="Department") - description_of_the_tag = fields.Text(string="Description") - valid_until = fields.Date(string="Valid until") - - @api.model - def _check_validity_dates(self): - """ Scheduled method to deactivate records past their validity date """ - today = fields.Date.today() - records_to_deactivate = self.search([('valid_until', '<', today), ('active', '=', True)]) - records_to_deactivate.write({'active': False}) - - records_to_activate = self.search([('valid_until', '>', today), ('active', '=', False)]) - records_to_activate.write({'active': True}) - - diff --git a/partner_tag_smart_assignation/tests/test_smart_tagger.py b/partner_tag_smart_assignation/tests/test_smart_tagger.py index a68badf70faf..5a5df67ae624 100644 --- a/partner_tag_smart_assignation/tests/test_smart_tagger.py +++ b/partner_tag_smart_assignation/tests/test_smart_tagger.py @@ -55,6 +55,10 @@ def create_tag(self): "active": True, "smart": True, "parent_id": False, + "author_id": False, + "department_ids": False, + "description": "", + "valid_until": fields.Date.today(), "tag_filter_partner_field": "partner_id", "tag_filter_condition_id": self.create_condition().id, } @@ -131,34 +135,29 @@ def test_check_validity_dates(self): # Create a new tag with a 'valid_until' date set to yesterday yesterday = fields.Date.to_string(fields.Date.today() - timedelta(days=1)) - expired_tag = self.env["res.partner.category"].create( - { - "name": "Expired Tag", - "active": True, - "valid_until": yesterday, - } - ) + expired_tag = self.create_tag() + expired_tag.update({"valid_until": yesterday}) + expired_tag.update_partner_tags() - # Create a new tag with a 'valid_until' date set to tomorrow - tomorrow = fields.Date.to_string(fields.Date.today() + timedelta(days=1)) + # Reload the tags from the database + expired_tag.invalidate_cache() - active_tag = self.env["res.partner.category"].create( - { - "name": "Active Tag", - "active": True, - "valid_until": tomorrow, - } - ) + # Check that the expired tag is now inactive + self.assertFalse(expired_tag.active) + # Check that the partner aren't tagged + self.assertFalse(self.david_simpson in expired_tag.partner_ids) + + # Modify the tag with a 'valid_until' date set to tomorrow + tomorrow = fields.Date.to_string(fields.Date.today() + timedelta(days=1)) + active_tag = expired_tag + active_tag.update({"valid_until": tomorrow, + "active": True}) # Run the method which is supposed to deactivate expired tags self.env["res.partner.category"]._check_validity_dates() # Reload the tags from the database - expired_tag.invalidate_cache() active_tag.invalidate_cache() - # Check that the expired tag is now inactive - self.assertFalse(expired_tag.active) - # Check that the active tag is still active self.assertTrue(active_tag.active) diff --git a/partner_tag_smart_assignation/views/res_partner_category_view_extension.xml b/partner_tag_smart_assignation/views/res_partner_category_view_extension.xml deleted file mode 100644 index 2cb4a7bff69e..000000000000 --- a/partner_tag_smart_assignation/views/res_partner_category_view_extension.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - res.partner.category.form.inherited - res.partner.category - - - - - - - - - - - - - res.partner.category.list.inherited - res.partner.category - - - - - - - - - - - diff --git a/partner_tag_smart_assignation/views/smart_tagger_view.xml b/partner_tag_smart_assignation/views/smart_tagger_view.xml index 4465da71dd70..7df4e198463e 100644 --- a/partner_tag_smart_assignation/views/smart_tagger_view.xml +++ b/partner_tag_smart_assignation/views/smart_tagger_view.xml @@ -6,6 +6,10 @@ + + + + @@ -34,6 +38,12 @@ + + + +