From a1c0d8fa15714494a0d04a78ba681e65dacfb8e5 Mon Sep 17 00:00:00 2001 From: zoro2002 Date: Mon, 28 Aug 2023 12:27:44 +0200 Subject: [PATCH] added dropdown and valid_until --- .../cron/update_cron.xml | 18 ++++++-- .../models/res_partner_category_extension.py | 20 +++++++-- .../tests/test_smart_tagger.py | 43 +++++++++++++++++++ .../res_partner_category_view_extension.xml | 4 +- 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/partner_tag_smart_assignation/cron/update_cron.xml b/partner_tag_smart_assignation/cron/update_cron.xml index d3886f6e75a..fbd7fe2ef5c 100644 --- a/partner_tag_smart_assignation/cron/update_cron.xml +++ b/partner_tag_smart_assignation/cron/update_cron.xml @@ -1,14 +1,26 @@ 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/res_partner_category_extension.py b/partner_tag_smart_assignation/models/res_partner_category_extension.py index c465a2264da..3118c2c705d 100644 --- a/partner_tag_smart_assignation/models/res_partner_category_extension.py +++ b/partner_tag_smart_assignation/models/res_partner_category_extension.py @@ -1,10 +1,22 @@ -from odoo import fields, models +from odoo import fields, models, api class ResPartnerCategoryExtension(models.Model): _inherit = "res.partner.category" - author_of_the_tag = fields.Char(string="Author") - department_that_uses_the_tag = fields.Char(string="Department") + 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") - is_unlimited_tag = fields.Boolean(string="Unlimited") + 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 fdf0fd5fbfe..a68badf70fa 100644 --- a/partner_tag_smart_assignation/tests/test_smart_tagger.py +++ b/partner_tag_smart_assignation/tests/test_smart_tagger.py @@ -1,6 +1,9 @@ import logging +from odoo import fields from odoo.tests.common import SavepointCase +from datetime import timedelta + logger = logging.getLogger(__name__) @@ -119,3 +122,43 @@ def test_smart_tag_sql(self): for partner in smart_tag.partner_ids: self.assertTrue("o" in partner.name) + + def test_check_validity_dates(self): + """ + Test if the valid_until functionality works correctly + """ + + # 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, + } + ) + + # Create a new tag with a 'valid_until' date set to tomorrow + tomorrow = fields.Date.to_string(fields.Date.today() + timedelta(days=1)) + + active_tag = self.env["res.partner.category"].create( + { + "name": "Active Tag", + "active": True, + "valid_until": tomorrow, + } + ) + + # 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 index c8c12f4f883..2cb4a7bff69 100644 --- a/partner_tag_smart_assignation/views/res_partner_category_view_extension.xml +++ b/partner_tag_smart_assignation/views/res_partner_category_view_extension.xml @@ -8,7 +8,7 @@ - + @@ -22,7 +22,7 @@ - +