diff --git a/partner_tag_smart_assignation/__manifest__.py b/partner_tag_smart_assignation/__manifest__.py index a89f90e3234..831c1977316 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 fbd7fe2ef5c..d3886f6e75a 100644 --- a/partner_tag_smart_assignation/cron/update_cron.xml +++ b/partner_tag_smart_assignation/cron/update_cron.xml @@ -1,26 +1,14 @@ Smart Tags Updater - - + + 1 weeks -1 1 - + 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 b8a18026177..e87be07028d 100644 --- a/partner_tag_smart_assignation/models/__init__.py +++ b/partner_tag_smart_assignation/models/__init__.py @@ -1,4 +1,3 @@ # 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 00000000000..2b20a5244df --- /dev/null +++ b/partner_tag_smart_assignation/models/hr_department.py @@ -0,0 +1,9 @@ +# 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 + + +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 2c0946f877b..a3565ae5ff0 100644 --- a/partner_tag_smart_assignation/models/res_partner_category.py +++ b/partner_tag_smart_assignation/models/res_partner_category.py @@ -43,6 +43,13 @@ 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 +141,19 @@ 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 3118c2c705d..00000000000 --- 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 a68badf70fa..d3fada1de28 100644 --- a/partner_tag_smart_assignation/tests/test_smart_tagger.py +++ b/partner_tag_smart_assignation/tests/test_smart_tagger.py @@ -1,9 +1,8 @@ import logging +from datetime import timedelta from odoo import fields from odoo.tests.common import SavepointCase -from datetime import timedelta - logger = logging.getLogger(__name__) @@ -55,6 +54,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 +134,28 @@ 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 2cb4a7bff69..00000000000 --- 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 4465da71dd7..ab9d2333ca7 100644 --- a/partner_tag_smart_assignation/views/smart_tagger_view.xml +++ b/partner_tag_smart_assignation/views/smart_tagger_view.xml @@ -6,6 +6,13 @@ + + + + @@ -34,6 +41,13 @@ + + + +