Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T0424 FEAT: Add field to the tag to help identify them #7

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion partner_tag_smart_assignation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"author": "Compassion CH, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/partner-contact",
"depends": ["base"],
"depends": ["base", "hr"],
"data": ["cron/update_cron.xml", "views/smart_tagger_view.xml"],
"installable": True,
"auto_install": False,
Expand Down
1 change: 1 addition & 0 deletions partner_tag_smart_assignation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import res_partner_category
from . import hr_department
9 changes: 9 additions & 0 deletions partner_tag_smart_assignation/models/hr_department.py
Original file line number Diff line number Diff line change
@@ -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")
18 changes: 18 additions & 0 deletions partner_tag_smart_assignation/models/res_partner_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
40 changes: 40 additions & 0 deletions partner_tag_smart_assignation/tests/test_smart_tagger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
from datetime import timedelta

from odoo import fields
from odoo.tests.common import SavepointCase

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -52,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,
}
Expand Down Expand Up @@ -119,3 +125,37 @@ 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.create_tag()
expired_tag.update({"valid_until": yesterday})
expired_tag.update_partner_tags()

# Reload the tags from the database
expired_tag.invalidate_cache()

# 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
active_tag.invalidate_cache()

# Check that the active tag is still active
self.assertTrue(active_tag.active)
14 changes: 14 additions & 0 deletions partner_tag_smart_assignation/views/smart_tagger_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<field name="arch" type="xml">
<field name="display_name" position="after">
<field name="smart" />
<field name="author_id" />
<field name="department_ids" />
<field name="description" />
<field
name="valid_until"
attrs="{'invisible': [('smart', '=', False)],}"
/>
</field>
</field>
</record>
Expand Down Expand Up @@ -34,6 +41,13 @@
</xpath>
<field name="parent_id" position="after">
<field name="smart" />
<field name="author_id" />
<field name="department_ids" widget="many2many_tags" />
<field name="description" />
<field
name="valid_until"
attrs="{'invisible': [('smart', '=', False)],}"
/>
<field
name="tag_filter_join_operator"
attrs="{'invisible': [('smart', '=', False)],}"
Expand Down
Loading