Skip to content

Commit

Permalink
T0424 FEAT: Add field to the tag to help identify them
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nfluckiger committed Nov 14, 2023
1 parent a1c0d8f commit bca29a4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 92 deletions.
4 changes: 2 additions & 2 deletions partner_tag_smart_assignation/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
18 changes: 3 additions & 15 deletions partner_tag_smart_assignation/cron/update_cron.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
<odoo>
<record id="smart_tags_updater" model="ir.cron">
<field name="name">Smart Tags Updater</field>
<field name="active" eval="True"/>
<field name="user_id" ref="base.user_root"/>
<field name="active" eval="True" />
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">weeks</field>
<field name="numbercall">-1</field>
<field name="doall">1</field>
<field name="model_id" ref="model_res_partner_category"/>
<field name="model_id" ref="model_res_partner_category" />
<field name="state">code</field>
<field name="code">model.update_all_smart_tags()</field>
</record>

<record id="ir_cron_check_validity_dates" model="ir.cron">
<field name="name">Check Validity Dates</field>
<field name="active" eval="True"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="model_id" ref="model_res_partner_category"/>
<field name="state">code</field>
<field name="code">model._check_validity_dates()</field>
</record>
</odoo>
3 changes: 1 addition & 2 deletions partner_tag_smart_assignation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
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

This file was deleted.

41 changes: 19 additions & 22 deletions partner_tag_smart_assignation/tests/test_smart_tagger.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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)

This file was deleted.

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

0 comments on commit bca29a4

Please sign in to comment.