Skip to content

Commit

Permalink
[PR OCA#467] [IMP] helpdesk_mgmt: Use email layout to simplify all te…
Browse files Browse the repository at this point in the history
…mplates

- Avoid email_cc (error "message without recipients")
- Use coherent partner/user reference
- Access button from e-mail notifications

Semi-forward port of 98e5ef7

Please note that the commit a5041c2 (An e-mail is sent to the ticket after each create/edit) has not been forward ported because the assignment notify is done internally by odoo in `_message_auto_subscribe_followers` from `mail.thread`
  • Loading branch information
eLBati authored and renda-dev committed Apr 23, 2024
1 parent 48a22dc commit 7e936a9
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 115 deletions.
6 changes: 5 additions & 1 deletion helpdesk_mgmt/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Helpdesk Management
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:357a54501fd1c1b608a3bf56d0ecaf11c1e7e0891f1e615a94e9298cec37580b
!! source digest: sha256:9512cd33b07790d26d4669f420a5a7191d15dc3fe83683b82307c952449e1dee
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -211,6 +211,10 @@ Contributors
* Marcel Savegnago
* Eduardo Aparício

* `TAKOBI <https://takobi.online>`_:

* Lorenzo Battistini

* `Obertix <https://www.obertix.net>`_:

* Vicent Cubells
Expand Down
1 change: 1 addition & 0 deletions helpdesk_mgmt/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"depends": ["mail", "portal"],
"data": [
"data/helpdesk_data.xml",
"data/ir_config_parameter.xml",
"security/helpdesk_security.xml",
"security/ir.model.access.csv",
"views/res_partner_views.xml",
Expand Down
1 change: 1 addition & 0 deletions helpdesk_mgmt/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def _prepare_submit_ticket_vals(self, **kw):
"description": plaintext2html(kw.get("description")),
"name": kw.get("subject"),
"attachment_ids": False,
"stage_id": request.env.ref("helpdesk_mgmt.helpdesk_ticket_stage_new").id,
"channel_id": request.env["helpdesk.ticket.channel"]
.sudo()
.search([("name", "=", "Web")])
Expand Down
147 changes: 43 additions & 104 deletions helpdesk_mgmt/data/helpdesk_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,68 +14,54 @@
<field name="model_id" ref="helpdesk_mgmt.model_helpdesk_ticket" />
<field name="email_from">${object.company_id.partner_id.email}</field>
<field
name="email_cc"
name="email_to"
>${not object.partner_id and object.partner_email or ''|safe},</field>
<field name="subject">The ticket ${object.number} has been closed.</field>
<field name="partner_to">${object.partner_id.id}</field>
<field name="auto_delete" eval="False" />
<field name="lang">${object.partner_id.lang}</field>
<field
name="body_html"
><![CDATA[<html>
<head></head>
<body style="margin: 0; padding: 0;">
<table border="0" width="100%" cellpadding="0" bgcolor="#ededed" style="padding: 20px; background-color: #ededed; border-collapse:separate;">
<tbody>
<!-- HEADER -->
<tr>
<td align="center" style="min-width: 590px;">
<table width="590" border="0" cellpadding="0" bgcolor="#875A7B" style="min-width: 590px; background-color: rgb(135,90,123); padding: 20px; border-collapse:separate;">
<tr>
<td valign="middle" align="right">
<img src="/logo.png?company=${object.company_id.id}" style="padding: 0px; margin: 0px; height: auto; width: 80px;" alt="${object.company_id.name}">
</td>
</tr>
</table>
</td>
</tr>
<!-- CONTENT -->
<tr>
<td align="center" style="min-width: 590px;">
<table width="590" border="0" cellpadding="0" bgcolor="#ffffff" style="min-width: 590px; background-color: rgb(255, 255, 255); padding: 20px; border-collapse:separate;">
<tbody>
<td valign="top" style="font-family:Arial,Helvetica,sans-serif; color: #555; font-size: 14px;">
<p>Hello ${object.partner_id.name or object.partner_name or ''},</p>
<p>The ticket "${object.display_name}" has been closed.</p>
</td>
</tbody>
</table>
</td>
</tr>
<!-- FOOTER -->
<tr>
<td align="center" style="min-width: 590px;">
<table width="590" border="0" cellpadding="0" bgcolor="#875A7B" style="min-width: 590px; background-color: rgb(135,90,123); padding: 20px; border-collapse:separate;">
<tr>
<td valign="middle" align="left" style="color: #fff; padding-top: 10px; padding-bottom: 10px; font-size: 12px;">
${object.company_id.phone}
</td>
<td valign="middle" align="left" style="color: #fff; padding-top: 10px; padding-bottom: 10px; font-size: 12px;">
${object.company_id.email}
</td>
<td valign="middle" align="left" style="color: #fff; padding-top: 10px; padding-bottom: 10px; font-size: 12px;">
${object.company_id.website}
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
><![CDATA[
<div>
<p>Hello ${object.partner_id.name or object.partner_name or ''},</p>
<p>The ticket "${object.display_name or 'n/a'}" has been closed.</p>
% if object.partner_can_access():
<div style="margin: 16px 0px 16px 0px;">
<a href="${object.get_access_link()}" style="background-color: #875A7B; padding: 8px 16px 8px 16px; text-decoration: none; color: #fff; border-radius: 5px; font-size:13px;">
View Ticket
</a>
</div>
% endif
Thank you,<br/><br/>
${user.signature | safe}<br/>
</div>
]]></field>
</record>
<record id="created_ticket_template" model="mail.template">
<field name="name">Helpdesk Created Ticket Notification Email</field>
<field name="model_id" ref="helpdesk_mgmt.model_helpdesk_ticket" />
<field name="email_from">${object.company_id.partner_id.email}</field>
<field
name="email_to"
>${('' if object.partner_id.email and object.partner_id.email == object.partner_email else object.email) | safe},</field>
<field name="subject">The ticket ${object.number} has been created.</field>
<field
name="partner_to"
>${object.partner_id.id if object.partner_id.email and object.partner_id.email == object.partner_email else False}</field>
<field name="auto_delete" eval="False" />
<field name="lang">${object.partner_id.lang}</field>
<field
name="body_html"
><![CDATA[<html>
<div>
<p>Hello ${object.partner_id.name or object.partner_name or ''},</p>
<p>The ticket "${object.display_name or 'n/a'}" has been created.</p>
<p>You can reply to this email to add information to the ticket.</p>
</div>
]]>
</field>
</record>
<record id="changed_stage_template" model="mail.template">
<field name="name">Helpdesk Changed Stage notification Email</field>
<field name="model_id" ref="helpdesk_mgmt.model_helpdesk_ticket" />
Expand All @@ -90,57 +76,10 @@
<field
name="body_html"
><![CDATA[<html>
<head></head>
<body style="margin: 0; padding: 0;">
<table border="0" width="100%" cellpadding="0" bgcolor="#ededed" style="padding: 20px; background-color: #ededed; border-collapse:separate;">
<tbody>
<!-- HEADER -->
<tr>
<td align="center" style="min-width: 590px;">
<table width="590" border="0" cellpadding="0" bgcolor="#875A7B" style="min-width: 590px; background-color: rgb(135,90,123); padding: 20px; border-collapse:separate;">
<tr>
<td valign="middle" align="right">
<img src="/logo.png?company=${object.company_id.id}" style="padding: 0px; margin: 0px; height: auto; width: 80px;">
</td>
</tr>
</table>
</td>
</tr>
<!-- CONTENT -->
<tr>
<td align="center" style="min-width: 590px;">
<table width="590" border="0" cellpadding="0" bgcolor="#ffffff" style="min-width: 590px; background-color: rgb(255, 255, 255); padding: 20px; border-collapse:separate;">
<tbody>
<td valign="top" style="font-family:Arial,Helvetica,sans-serif; color: #555; font-size: 14px;">
<p>Hello ${object.partner_id.name or object.partner_name or ''},</p>
<p>The ticket "${object.display_name}" stage has changed to ${object.stage_id.name}.</p>
</td>
</tbody>
</table>
</td>
</tr>
<!-- FOOTER -->
<tr>
<td align="center" style="min-width: 590px;">
<table width="590" border="0" cellpadding="0" bgcolor="#875A7B" style="min-width: 590px; background-color: rgb(135,90,123); padding: 20px; border-collapse:separate;">
<tr>
<td valign="middle" align="left" style="color: #fff; padding-top: 10px; padding-bottom: 10px; font-size: 12px;">
${object.company_id.phone}
</td>
<td valign="middle" align="left" style="color: #fff; padding-top: 10px; padding-bottom: 10px; font-size: 12px;">
${object.company_id.email}
</td>
<td valign="middle" align="left" style="color: #fff; padding-top: 10px; padding-bottom: 10px; font-size: 12px;">
${object.company_id.website}
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
<div>
<p>Hello ${object.partner_id.name or object.partner_name or ''},</p>
<p>The ticket "${object.display_name}" stage has changed to ${object.stage_id.name}.</p>
</div>
]]>
</field>
</record>
Expand Down
9 changes: 9 additions & 0 deletions helpdesk_mgmt/data/ir_config_parameter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">

<record model="ir.config_parameter" id="created_ticket_template_name">
<field name="key">helpdesk_mgmt.created_ticket_template_name</field>
<field name="value">created_ticket_template</field>
</record>

</odoo>
92 changes: 83 additions & 9 deletions helpdesk_mgmt/models/helpdesk_ticket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from email.utils import getaddresses

from odoo import _, api, fields, models, tools
from odoo.exceptions import AccessError

Expand Down Expand Up @@ -111,6 +113,26 @@ def _read_group_stage_ids(self, stages, domain, order):
)
active = fields.Boolean(default=True)

def _send_mail_to_partner(self, template_name):
"""Send a feedback message to the contact linked to this newly created ticket
(whatever it is a real partner or just its e-mail).
Note: The purpose of this function is to replace the stage's `mail_template_id`
role (that just doesn't work when the ticket is created from the portal or when
`partner_id` is set)"""
created_ticket_template = self.env.ref(template_name, raise_if_not_found=False)
if created_ticket_template:
created_ticket_template.send_mail(
self.id, force_send=True, notif_layout="mail.mail_notification_light"
)

@api.model
def _get_created_ticket_template_name(self):
return (
self.env["ir.config_parameter"]
.sudo()
.get_param("helpdesk_mgmt.created_ticket_template_name")
)

def name_get(self):
res = []
for rec in self:
Expand Down Expand Up @@ -140,6 +162,15 @@ def _get_default_email_channel(self):
def _creation_subtype(self):
return self.env.ref("helpdesk_mgmt.hlp_tck_created")

def _message_auto_subscribe_followers(self, updated_values, default_subtype_ids):
"""Auto-subscribe ticket partner."""
result = super()._message_auto_subscribe_followers(
updated_values, default_subtype_ids
)
if updated_values.get("partner_id"):
result.append((self.partner_id.id, default_subtype_ids, False))
return result

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
Expand All @@ -157,7 +188,14 @@ def create(self, vals_list):
channel_email_id = self._get_default_email_channel()
if channel_email_id:
vals["channel_id"] = channel_email_id.id
return super().create(vals_list)

res = super().create(vals_list)
created_ticket_template_name = self._get_created_ticket_template_name()
if created_ticket_template_name:
for rec in res:
if rec.partner_id or rec.partner_email:
rec._send_mail_to_partner(created_ticket_template_name)
return res

def copy(self, default=None):
self.ensure_one()
Expand Down Expand Up @@ -190,11 +228,6 @@ def _prepare_ticket_number(self, values):
seq = seq.with_company(values["company_id"])
return seq.next_by_code("helpdesk.ticket.sequence") or "/"

def _compute_access_url(self):
super()._compute_access_url()
for item in self:
item.access_url = "/my/ticket/%s" % (item.id)

def _prepare_team_id(self, values):
category = self.env["helpdesk.ticket.category"].browse(values["category_id"])
if category.default_team_id:
Expand Down Expand Up @@ -236,13 +269,20 @@ def message_new(self, msg, custom_values=None):
"partner_email": partner_email,
"partner_id": msg.get("author_id"),
}

if not msg.get("author_id"):
defaults["partner_name"] = partner_name
defaults.update(custom_values)

msg_from = msg.get("from", "")
if msg_from:
res = getaddresses([msg_from])
if res:
defaults["partner_name"] = res[0][0]
defaults["partner_email"] = res[0][1]

defaults.update(custom_values)
# Write default values coming from msg
ticket = super().message_new(msg, custom_values=defaults)

# Use mail gateway tools to search for partners to subscribe
email_list = tools.email_split(
(msg.get("to") or "") + "," + (msg.get("cc") or "")
Expand All @@ -255,7 +295,6 @@ def message_new(self, msg, custom_values=None):
if p
]
ticket.message_subscribe(partner_ids)

return ticket

def message_update(self, msg, update_vals=None):
Expand Down Expand Up @@ -337,3 +376,38 @@ def action_do_send_email(self):
"target": "new",
"context": ctx,
}

# ---------------------------------------------------
# Portal
# ---------------------------------------------------

def _compute_access_url(self):
super(HelpdeskTicket, self)._compute_access_url()
for ticket in self:
ticket.access_url = "/my/ticket/%s" % (ticket.id)

def _notify_get_groups(self, msg_vals=None):
groups = super(HelpdeskTicket, self)._notify_get_groups(msg_vals=msg_vals)
self.ensure_one()
for group_name, _group_method, group_data in groups:
if group_name == "portal":
group_data["has_button_access"] = True
return groups

def partner_can_access(self):
if not self.partner_id:
return False
user = (
self.env["res.users"]
.sudo()
.search([("partner_id", "=", self.partner_id.id)])
)
if not user or not self.with_user(user).check_access_rights(
"read", raise_exception=False
):
return False
return True

def get_access_link(self):
# _notify_get_action_link is not callable from email template
return self._notify_get_action_link("view")
4 changes: 4 additions & 0 deletions helpdesk_mgmt/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
* Marcel Savegnago
* Eduardo Aparício

* `TAKOBI <https://takobi.online>`_:

* Lorenzo Battistini

* `Obertix <https://www.obertix.net>`_:

* Vicent Cubells
Expand Down
7 changes: 6 additions & 1 deletion helpdesk_mgmt/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -366,7 +367,7 @@ <h1 class="title">Helpdesk Management</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:357a54501fd1c1b608a3bf56d0ecaf11c1e7e0891f1e615a94e9298cec37580b
!! source digest: sha256:9512cd33b07790d26d4669f420a5a7191d15dc3fe83683b82307c952449e1dee
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/helpdesk/tree/14.0/helpdesk_mgmt"><img alt="OCA/helpdesk" src="https://img.shields.io/badge/github-OCA%2Fhelpdesk-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/helpdesk-14-0/helpdesk-14-0-helpdesk_mgmt"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/helpdesk&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds Helpdesk functionality in Odoo.</p>
Expand Down Expand Up @@ -563,6 +564,10 @@ <h2><a class="toc-backref" href="#toc-entry-12">Contributors</a></h2>
<li>Eduardo Aparício</li>
</ul>
</li>
<li><a class="reference external" href="https://takobi.online">TAKOBI</a>:<ul>
<li>Lorenzo Battistini</li>
</ul>
</li>
<li><a class="reference external" href="https://www.obertix.net">Obertix</a>:<ul>
<li>Vicent Cubells</li>
</ul>
Expand Down

0 comments on commit 7e936a9

Please sign in to comment.