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

[14.0][FW-PORT] helpdesk_mgmt: Simplify e-mail templates #467

Closed
Closed
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 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>
ypapouin marked this conversation as resolved.
Show resolved Hide resolved
<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>
88 changes: 79 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 @@
)
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(

Check warning on line 124 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L124

Added line #L124 was not covered by tests
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 All @@ -134,6 +156,15 @@
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 @@ -143,7 +174,13 @@
vals["team_id"] = self._prepare_team_id(vals)
if vals.get("user_id") and not vals.get("assigned_date"):
vals["assigned_date"] = fields.Datetime.now()
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 @@ -176,11 +213,6 @@
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 @@ -219,11 +251,15 @@
"partner_email": msg.get("from"),
"partner_id": msg.get("author_id"),
}
msg_from = msg.get("from", "")
if msg_from:
res = getaddresses([msg_from])

Check warning on line 256 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L256

Added line #L256 was not covered by tests
if res:
defaults["partner_name"] = res[0][0]
defaults["partner_email"] = res[0][1]

Check warning on line 259 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L258-L259

Added lines #L258 - L259 were not covered by tests
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 @@ -236,7 +272,6 @@
if p
]
ticket.message_subscribe(partner_ids)

return ticket

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

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

def _compute_access_url(self):
super(HelpdeskTicket, self)._compute_access_url()
ypapouin marked this conversation as resolved.
Show resolved Hide resolved
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

Check warning on line 376 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L376

Added line #L376 was not covered by tests
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")

Check warning on line 390 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L390

Added line #L390 was not covered by tests
2 changes: 1 addition & 1 deletion helpdesk_mgmt/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Stages
#. Edit or create a new stage.
#. Set the name for the stage.
#. Set the sequence order for the stage.
#. You can select an Email template.
#. You can select an email template that will be automatically sent when a ticket reaches this stage.
#. Mark the Unattended checkbox if the stage contains unattended tickets.
#. Mark the Closed checkbox if the stage contains closed tickets.
#. You can add a description for the stage.
Expand Down
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
3 changes: 3 additions & 0 deletions helpdesk_mgmt/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def setUpClass(cls):
groups="helpdesk_mgmt.group_helpdesk_user",
context=ctx,
)
cls.admin = cls.env.ref("base.user_admin")
cls.user_portal = cls.env.ref("base.demo_user0")
cls.user_demo = cls.env.ref("base.user_demo")
cls.stage_closed = cls.env.ref("helpdesk_mgmt.helpdesk_ticket_stage_done")
cls.team_a = helpdesk_ticket_team.create(
{"name": "Team A", "user_ids": [(6, 0, [cls.user_own.id, cls.user.id])]}
Expand Down
Loading
Loading