Skip to content

Commit

Permalink
added dropdown and valid_until
Browse files Browse the repository at this point in the history
  • Loading branch information
zoro2002 committed Aug 28, 2023
1 parent f6f6c59 commit a1c0d8f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
18 changes: 15 additions & 3 deletions partner_tag_smart_assignation/cron/update_cron.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<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>
Original file line number Diff line number Diff line change
@@ -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})


43 changes: 43 additions & 0 deletions partner_tag_smart_assignation/tests/test_smart_tagger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging

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


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<field name="author_of_the_tag"/>
<field name="department_that_uses_the_tag"/>
<field name="description_of_the_tag"/>
<field name="is_unlimited_tag"/>
<field name="valid_until"/>
</xpath>
</field>
</record>
Expand All @@ -22,7 +22,7 @@
<field name="author_of_the_tag"/>
<field name="department_that_uses_the_tag"/>
<field name="description_of_the_tag"/>
<field name="is_unlimited_tag"/>
<field name="valid_until"/>
</xpath>
</field>
</record>
Expand Down

0 comments on commit a1c0d8f

Please sign in to comment.