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

[17.0][ADD] crm_lead_to_task: Add relationship and smart buttons #592

Merged
merged 4 commits into from
Aug 7, 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
1 change: 1 addition & 0 deletions crm_lead_to_task/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# License LGPL-3 - See https://www.gnu.org/licenses/lgpl-3.0.html
from . import models
from . import wizard
1 change: 1 addition & 0 deletions crm_lead_to_task/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
"security/ir.model.access.csv",
"wizard/crm_lead_convert2task_views.xml",
"views/crm_lead_views.xml",
"views/project_task_views.xml",
],
}
2 changes: 2 additions & 0 deletions crm_lead_to_task/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import crm_lead
from . import project_task
27 changes: 27 additions & 0 deletions crm_lead_to_task/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models


class CrmLead(models.Model):
_inherit = "crm.lead"

task_ids = fields.One2many("project.task", "lead_id")
task_count = fields.Integer(compute="_compute_task_count")

@api.depends("task_ids")
def _compute_task_count(self):
for lead in self:
lead.task_count = len(lead.task_ids)

def action_view_tasks(self):
self.ensure_one()
return {
"type": "ir.actions.act_window",
"res_model": "project.task",
"view_mode": "tree,form",
"domain": [("lead_id", "=", self.id)],
"context": {"default_search_lead_id": self.id},
"name": _("Tasks from crm lead %s") % self.name,
}
21 changes: 21 additions & 0 deletions crm_lead_to_task/models/project_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, fields, models


class ProjectTask(models.Model):
_inherit = "project.task"

lead_id = fields.Many2one("crm.lead")

def action_view_lead(self):
self.ensure_one()
return {
"type": "ir.actions.act_window",
"res_model": "crm.lead",
"view_mode": "form",
"res_id": self.lead_id.id,
"target": "current",
"name": _("Lead: %s") % self.lead_id.name,
}
1 change: 1 addition & 0 deletions crm_lead_to_task/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# License LGPL-3 - See https://www.gnu.org/licenses/lgpl-3.0.html
from . import test_crm_project
from . import test_crm_lead_task
59 changes: 59 additions & 0 deletions crm_lead_to_task/tests/test_crm_lead_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (C) 2024 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _
from odoo.tests.common import TransactionCase


class TestCrmLeadTask(TransactionCase):
def setUp(self):
super().setUp()

# Create a CRM lead
self.lead = self.env["crm.lead"].create(
{
"name": "Test Lead",
"description": "Description",
}
)

# Create tasks related to the lead
self.task1 = self.env["project.task"].create(
{
"name": "Test Task 1",
"lead_id": self.lead.id,
}
)
self.task2 = self.env["project.task"].create(
{
"name": "Test Task 2",
"lead_id": self.lead.id,
}
)

def test_task_count_computation(self):
"""Test that the task_count field correctly reflects the number of tasks"""
lead = self.env["crm.lead"].browse(self.lead.id)
self.assertEqual(lead.task_count, 2, "Task count should be 2.")

def test_action_view_tasks(self):
"""Test that action_view_tasks returns the correct action"""
action = self.lead.action_view_tasks()

expected_domain = [("lead_id", "=", self.lead.id)]
self.assertEqual(action["type"], "ir.actions.act_window")
self.assertEqual(action["res_model"], "project.task")
self.assertEqual(action["view_mode"], "tree,form")
self.assertEqual(action["domain"], expected_domain)
self.assertEqual(action["context"]["default_search_lead_id"], self.lead.id)
self.assertEqual(action["name"], _("Tasks from crm lead %s") % self.lead.name)

def test_action_view_leads(self):
"""Test that action_view_lead returns the correct action"""
action = self.task1.action_view_lead()

self.assertEqual(action["type"], "ir.actions.act_window")
self.assertEqual(action["res_model"], "crm.lead")
self.assertEqual(action["view_mode"], "form")
self.assertEqual(action["res_id"], self.lead.id)
self.assertEqual(action["name"], _("Lead: %s") % self.lead.name)
23 changes: 23 additions & 0 deletions crm_lead_to_task/views/crm_lead_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@
groups="project.group_project_user"
/>
</field>
<!-- Add a smart button to the tasks created from this lead -->
<xpath expr="//div[@name='button_box']" position="inside">
<field name="task_count" invisible="1" />
<button
name="action_view_tasks"
type="object"
class="oe_stat_button"
icon="fa-pencil"
invisible="task_count &lt; 1"
>
<div class="o_stat_info">
<field name="task_count" class="o_stat_value" />
<span
class="o_stat_text"
invisible="task_count &lt; 2"
>Tasks</span>
<span
class="o_stat_text"
invisible="task_count &gt; 1"
>Task</span>
</div>
</button>
</xpath>
</field>
</record>
</odoo>
25 changes: 25 additions & 0 deletions crm_lead_to_task/views/project_task_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<odoo>

<record id="view_task_form2" model="ir.ui.view">
<field name="name">project.task.view.form.inherit.crm.lead.to.task</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_form2" />
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']" position="inside">
<field name="lead_id" invisible="1" />
<button
name="action_view_lead"
type="object"
class="oe_stat_button"
icon="fa-star"
invisible="not lead_id"
>
<div class="o_stat_info">
<span class="o_stat_text">CRM Lead</span>
</div>
</button>
</xpath>
</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions crm_lead_to_task/wizard/crm_lead_convert2task.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def action_lead_to_project_task(self):
"project_id": self.project_id.id,
"partner_id": partner.id,
"email_cc": lead.email_cc,
"lead_id": lead.id,
}
task = self.env["project.task"].create(vals)
# move the mail thread
Expand Down
Loading