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

[16.0][IMP] mail_gateway_whatsapp: add variables #1

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
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 mail_gateway_whatsapp/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Contributors
* `Tecnativa <https://www.tecnativa.com>`_:

* Carlos Lopez
* Frank Cespedes <[email protected]>

Other credits
~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions mail_gateway_whatsapp/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"security/ir.model.access.csv",
"wizards/whatsapp_composer.xml",
"wizards/mail_compose_gateway_message.xml",
"views/mail_whatsapp_template_variable_views.xml",
"views/mail_whatsapp_template_views.xml",
"views/mail_gateway.xml",
],
Expand Down
1 change: 1 addition & 0 deletions mail_gateway_whatsapp/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import mail_channel
from . import res_partner
from . import mail_whatsapp_template
from . import mail_whatsapp_template_variable
1 change: 1 addition & 0 deletions mail_gateway_whatsapp/models/mail_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def button_import_whatsapp_template(self):
for template_data in meta_info.get("data", []):
ws_template = templates_by_id.get(template_data["id"])
if ws_template:
ws_template.variable_ids.unlink()
ws_template.write(
WhatsappTemplate._prepare_values_to_import(self, template_data)
)
Expand Down
19 changes: 19 additions & 0 deletions mail_gateway_whatsapp/models/mail_gateway_whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,34 @@ def _send_payload(
"to": channel.gateway_channel_token,
}
if whatsapp_template:
variables = whatsapp_template.get_variable_values()
payload.update(
{
"type": "template",
"template": {
"name": whatsapp_template.template_name,
"language": {"code": whatsapp_template.language},
"components": [],
},
}
)
body_variables = variables.get("body")
if body_variables:
parameters = []
if variables["type"] == "number":
for value in body_variables.values():
parameters.append(
{
"type": "text",
"text": value,
}
)
elif variables["type"] == "name":
# TODO: Implement name type
pass
payload["template"]["components"].append(
{"type": "body", "parameters": parameters}
)
else:
payload.update(
{
Expand Down
42 changes: 42 additions & 0 deletions mail_gateway_whatsapp/models/mail_whatsapp_template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2024 Tecnativa - Carlos López
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import re

Expand All @@ -21,6 +22,10 @@ class MailWhatsAppTemplate(models.Model):

name = fields.Char(required=True)
body = fields.Text(required=True)
variable_ids = fields.One2many(
comodel_name="mail.whatsapp.template.variable", inverse_name="template_id"
)
variable_type = fields.Selection(selection=[("number", "Number"), ("name", "Name")])
header = fields.Char()
footer = fields.Char()
template_name = fields.Char(
Expand Down Expand Up @@ -82,6 +87,25 @@ def _compute_template_name(self):
r"\W+", "_", slugify(template.name or "")
)

def get_variable_values(self):
values = {"type": self.variable_type}
body_variables = self.variable_ids.filtered(lambda x: x.section == "body")
if body_variables:
body = {}
for variable in body_variables:
body.update({variable.name: variable.default_value})
values.update({"body": body})
return values

def get_body(self):
values = self.get_variable_values()
body_variables = values.get("body")
body = self.body
if body_variables:
for key, value in body_variables.items():
body = body.replace(f"{{{{{key}}}}}", value)
return body

def button_back2draft(self):
self.write({"state": "draft"})

Expand Down Expand Up @@ -159,6 +183,7 @@ def button_sync_template(self):
)
response.raise_for_status()
json_data = response.json()
self.variable_ids.unlink()
vals = self._prepare_values_to_import(gateway, json_data)
self.write(vals)
except Exception as err:
Expand All @@ -185,6 +210,23 @@ def _prepare_values_to_import(self, gateway, json_data):
vals["header"] = component["text"]
elif component["type"] == "BODY":
vals["body"] = component["text"]
for key, value in component.get("example", {}).items():
if key == "body_text":
vals["variable_type"] = "number"
for var_list in value:
for variable, var_value in enumerate(var_list, start=1):
vals.setdefault("variable_ids", []).append(
(
0,
0,
{
"section": "body",
"name": variable,
"parameter_type": "text",
"default_value": var_value,
},
)
)
elif component["type"] == "FOOTER":
vals["footer"] = component["text"]
else:
Expand Down
18 changes: 18 additions & 0 deletions mail_gateway_whatsapp/models/mail_whatsapp_template_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class MailWhatsAppTemplateButton(models.Model):
_name = "mail.whatsapp.template.button"
_description = "Mail WhatsApp template button"

template_id = fields.Many2one(comodel_name="mail.whatsapp.template")
template_state = fields.Selection(related="template_id.state", readonly=True)
text = fields.Char()
url = fields.Char()
url_type = fields.Selection(
selection=[("static", "Static"), ("dynamic", "Dynamic")],
string="URL Type",
)
22 changes: 22 additions & 0 deletions mail_gateway_whatsapp/models/mail_whatsapp_template_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from odoo import fields, models


class MailWhatsAppTemplateVariable(models.Model):
_name = "mail.whatsapp.template.variable"
_description = "Mail WhatsApp template variable"
_order = "name"

template_id = fields.Many2one(comodel_name="mail.whatsapp.template")
template_state = fields.Selection(related="template_id.state", readonly=True)
section = fields.Selection(
selection=[("body", "Body")],
)
name = fields.Char()
parameter_type = fields.Selection(
selection=[("text", "Text")],
)
default_value = fields.Char()
3 changes: 2 additions & 1 deletion mail_gateway_whatsapp/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
* Enric Tobella <[email protected]>
* `Tecnativa <https://www.tecnativa.com>`_:

* Carlos Lopez
* Carlos Lopez
* Frank Cespedes <[email protected]>
2 changes: 2 additions & 0 deletions mail_gateway_whatsapp/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_whatsapp_composer,access.whatsapp.composer,model_whatsapp_composer,base.group_user,1,1,1,0
access_mail_whatsapp_template_group_system,mail_whatsapp_template_group_system,model_mail_whatsapp_template,base.group_system,1,1,1,1
access_mail_whatsapp_template_group_user,mail_whatsapp_template_group_user,model_mail_whatsapp_template,base.group_user,1,0,0,0
access_mail_whatsapp_template_variable_group_system,mail_whatsapp_template_variable_group_system,model_mail_whatsapp_template_variable,base.group_system,1,1,1,1
access_mail_whatsapp_template_variable_group_user,mail_whatsapp_template_variable_group_user,model_mail_whatsapp_template_variable,base.group_user,1,0,0,0
13 changes: 6 additions & 7 deletions mail_gateway_whatsapp/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -476,6 +475,8 @@ <h2><a class="toc-backref" href="#toc-entry-9">Contributors</a></h2>
</ul>
</blockquote>
</li>
<li><p class="first">Frank Cespedes &lt;<a class="reference external" href="mailto:fcespedes&#64;nuobit.es">fcespedes&#64;nuobit.es</a>&gt;</p>
</li>
</ul>
</div>
<div class="section" id="other-credits">
Expand All @@ -485,9 +486,7 @@ <h2><a class="toc-backref" href="#toc-entry-10">Other credits</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>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.</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).-->
<odoo>
<record id="mail_whatsapp_template_variable_view_tree_readonly" model="ir.ui.view">
<field name="name">mail.whatsapp.template.variable.view.tree.readonly</field>
<field name="model">mail.whatsapp.template.variable</field>
<field name="arch" type="xml">
<tree create="0" delete="0" editable="top">
<field name="template_state" invisible="1" />
<field
name="name"
string="Variable"
attrs="{'readonly': [('template_state', '!=', 'draft')]}"
/>
<field
name="parameter_type"
attrs="{'readonly': [('template_state', '!=', 'draft')]}"
/>
<field name="default_value" />
</tree>
</field>
</record>
</odoo>
7 changes: 7 additions & 0 deletions mail_gateway_whatsapp/views/mail_whatsapp_template_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
>variables</strong> or <strong>buttons</strong>.
</div>
<sheet>
<div name="button_box" />
<div class="oe_title">
<label for="name" />
<h1>
Expand Down Expand Up @@ -119,6 +120,12 @@
name="body"
attrs="{'readonly': [('state', '!=', 'draft')]}"
/>
<field
name="variable_ids"
attrs="{'invisible': [('state', '!=', 'draft'), ('variable_ids', '=', [])]}"
domain="[('section', '=', 'body')]"
context="{'tree_view_ref': 'mail_gateway_whatsapp.mail_whatsapp_template_variable_view_tree_readonly'}"
/>
</page>
</notebook>
</sheet>
Expand Down