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

[MIG] RR/17.0 partner_smart_tag_assignation #9

Merged
merged 4 commits into from
Dec 3, 2024
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
81 changes: 81 additions & 0 deletions partner_tag_smart_assignation/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
============
Smart Tagger
============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:4cc44e04885361c3f282fc3aa7cfd2136a3df53e41bfc1ed8904a81e767c7b96
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fpartner--contact-lightgray.png?logo=github
:target: https://github.com/OCA/partner-contact/tree/17.0/partner_tag_smart_assignation
:alt: OCA/partner-contact
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/partner-contact-17-0/partner-contact-17-0-partner_tag_smart_assignation
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/partner-contact&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

The Smart Tagger Module update the partner category to add tags in a
dynamic way. Give the possibility to update all the partner linked to
the tags with one button. A cron update all the smart tagger when you
want it.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/partner-contact/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/partner-contact/issues/new?body=module:%20partner_tag_smart_assignation%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Compassion CH

Contributors
------------

- Nathan Flückiger <[email protected]>
- Emanuel Cino <[email protected]>
- Cyril Favre <[email protected]>

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/partner-contact <https://github.com/OCA/partner-contact/tree/17.0/partner_tag_smart_assignation>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions partner_tag_smart_assignation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import models
15 changes: 15 additions & 0 deletions partner_tag_smart_assignation/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (C) 2019-2023 Compassion CH (http://www.compassion.ch)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Smart Tagger",
"summary": "Smart tagger, module to have smart tags who " "update themselves alone",
"version": "17.0.1.0.0",
"category": "Other",
"author": "Compassion CH, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/partner-contact",
"depends": ["base", "hr"],
"data": ["cron/update_cron.xml", "views/smart_tagger_view.xml"],
"installable": True,
"auto_install": False,
}
14 changes: 14 additions & 0 deletions partner_tag_smart_assignation/cron/update_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +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="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="state">code</field>
<field name="code">model.update_all_smart_tags()</field>
</record>
</odoo>
3 changes: 3 additions & 0 deletions partner_tag_smart_assignation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +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")
169 changes: 169 additions & 0 deletions partner_tag_smart_assignation/models/res_partner_category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Copyright (C) 2019 Compassion CH (http://www.compassion.ch)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from datetime import datetime

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.safe_eval import safe_eval


class ResPartnerCategory(models.Model):
_inherit = "res.partner.category"

tag_filter_condition_id = fields.Many2one("ir.filters", "Domain filter")
smart = fields.Boolean(
help="Enable this to automatically assign the category on partners "
"matching a given filter domain or SQL query."
)
tag_filter_partner_field = fields.Char(
default="partner_id",
help="Relational field used on the filter object to find the partners.",
)
tag_filter_sql_query = fields.Text(
"SQL query",
help="Can be used instead of the filter for finding the relevant "
"partners. The given SQL query should only return partner ids "
"rows.",
)
tag_filter_join_operator = fields.Selection(
[
("and", "AND (must satisfy both SQL and domain filter)"),
("or", "OR (can satisfy either SQL or domain filter)"),
],
"JOIN operator",
default="or",
required=True,
)
partner_ids = fields.Many2many(
"res.partner",
"res_partner_res_partner_category_rel",
"category_id",
"partner_id",
)

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)
record.update_partner_tags()
return record

def write(self, vals):
res = super().write(vals)
if "tag_filter_condition_id" in vals or "model" in vals:
self.update_partner_tags()
return res

@api.constrains(
"tag_filter_condition_id",
"tag_filter_condition_id.model_id",
"tag_filter_condition_id.domain",
"tag_filter_partner_field",
)
def check_condition(self):
for me in self.filtered("tag_filter_condition_id"):
if me.tag_filter_condition_id.model_id != "res.partner":
model_link = self.env[me.tag_filter_condition_id.model_id]
if me.tag_filter_partner_field not in model_link:
raise ValidationError(
_("The chosen model has no field %s")
% me.tag_filter_partner_field
)

@api.constrains("tag_filter_sql_query")
def check_sql_query(self):
for me in self.filtered("tag_filter_sql_query"):
self.env.cr.execute(me.tag_filter_sql_query)
rows = self.env.cr.fetchall()
for row in rows:
if (
len(row) > 1
or not isinstance(row[0], int)
or not self.env["res.partner"].browse(row[0]).exists()
):
raise ValidationError(
_("The SQL query should only return partner ids")
)

def update_partner_tags(self):
for tagger in self.filtered("smart"):
sql_partners = tagger.get_partners_from_sql()
filter_partners = tagger.get_partners_from_ir_filter()
if tagger.tag_filter_join_operator == "and":
partners = sql_partners & filter_partners
else:
partners = sql_partners | filter_partners
if partners:
tagger.write({"partner_ids": [(6, 0, partners.ids)]})
else:
tagger.write({"partner_ids": [(5, 0, 0)]})
return True

def get_partners_from_ir_filter(self):
self.ensure_one()
if not self.tag_filter_condition_id.domain:
return self.env["res.partner"]
domain = safe_eval(
self.tag_filter_condition_id.domain,
locals_dict={"datetime": datetime},
locals_builtins=True,
)
model = self.tag_filter_condition_id.model_id
matching_records = self.env[model].search(domain)
if matching_records:
if model == "res.partner":
partners = matching_records
else:
partners = matching_records.mapped(self.tag_filter_partner_field)
return partners
else:
return self.env["res.partner"]

def get_partners_from_sql(self):
self.ensure_one()
partner_obj = self.env["res.partner"]
if not self.tag_filter_sql_query:
return partner_obj
self.env.cr.execute(self.tag_filter_sql_query)
rows = self.env.cr.fetchall()
if rows:
return partner_obj.browse([r[0] for r in rows])
else:
return partner_obj

@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:
category.tagged_partner_count = len(category.partner_ids)

def open_tags(self):
return {
"type": "ir.actions.act_window",
"res_model": "res.partner",
"view_mode": "list,form",
"name": "Partners",
"domain": [["id", "in", self.mapped("partner_ids").ids]],
}
3 changes: 3 additions & 0 deletions partner_tag_smart_assignation/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
3 changes: 3 additions & 0 deletions partner_tag_smart_assignation/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Nathan Flückiger \<<[email protected]>\>
- Emanuel Cino \<<[email protected]>\>
- Cyril Favre \<<[email protected]>\>
4 changes: 4 additions & 0 deletions partner_tag_smart_assignation/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The Smart Tagger Module update the partner category to add tags in a
dynamic way. Give the possibility to update all the partner linked to
the tags with one button. A cron update all the smart tagger when you
want it.
Loading
Loading