summaryrefslogtreecommitdiff
path: root/addons/website_mail_channel/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/website_mail_channel/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/website_mail_channel/models')
-rw-r--r--addons/website_mail_channel/models/__init__.py5
-rw-r--r--addons/website_mail_channel/models/mail_channel.py64
-rw-r--r--addons/website_mail_channel/models/mail_mail.py34
-rw-r--r--addons/website_mail_channel/models/website.py14
4 files changed, 117 insertions, 0 deletions
diff --git a/addons/website_mail_channel/models/__init__.py b/addons/website_mail_channel/models/__init__.py
new file mode 100644
index 00000000..f5c2bbb5
--- /dev/null
+++ b/addons/website_mail_channel/models/__init__.py
@@ -0,0 +1,5 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+from . import mail_channel
+from . import mail_mail
+from . import website
diff --git a/addons/website_mail_channel/models/mail_channel.py b/addons/website_mail_channel/models/mail_channel.py
new file mode 100644
index 00000000..89d72cd7
--- /dev/null
+++ b/addons/website_mail_channel/models/mail_channel.py
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+import hashlib
+import hmac
+
+from werkzeug import urls
+
+from odoo import models
+from odoo.addons.http_routing.models.ir_http import slug
+
+
+class MailGroup(models.Model):
+ _inherit = 'mail.channel'
+
+ def _notify_email_header_dict(self):
+ headers = super(MailGroup, self)._notify_email_header_dict()
+ base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
+ headers['List-Archive'] = '<%s/groups/%s>' % (base_url, slug(self))
+ headers['List-Subscribe'] = '<%s/groups>' % (base_url)
+ headers['List-Unsubscribe'] = '<%s/groups?unsubscribe>' % (base_url,)
+ return headers
+
+ def _send_confirmation_email(self, partner_ids, unsubscribe=False):
+ website = self.env['website'].get_current_website()
+ base_url = website.get_base_url()
+
+ route = "/groups/%(action)s/%(channel)s/%(partner)s/%(token)s"
+ if unsubscribe:
+ template = self.env.ref('website_mail_channel.mail_template_list_unsubscribe')
+ action = 'unsubscribe'
+ else:
+ template = self.env.ref('website_mail_channel.mail_template_list_subscribe')
+ action = 'subscribe'
+
+ for partner_id in partner_ids:
+ # generate a new token per subscriber
+ token = self._generate_action_token(partner_id, action=action)
+
+ token_url = urls.url_join(base_url, route % {
+ 'action': action,
+ 'channel': self.id,
+ 'partner': partner_id,
+ 'token': token,
+ })
+ template.with_context(token_url=token_url).send_mail(
+ self.id,
+ force_send=True,
+ email_values={
+ 'recipient_ids': [(4, partner_id)],
+ 'email_from': website.company_id.email,
+ }
+ )
+
+ return True
+
+ def _generate_action_token(self, partner_id, action='unsubscribe'):
+ self.ensure_one()
+ secret = self.env['ir.config_parameter'].sudo().get_param('database.secret')
+ data = '$'.join([
+ str(self.id),
+ str(partner_id),
+ action])
+ return hmac.new(secret.encode('utf-8'), data.encode('utf-8'), hashlib.md5).hexdigest()
diff --git a/addons/website_mail_channel/models/mail_mail.py b/addons/website_mail_channel/models/mail_mail.py
new file mode 100644
index 00000000..271e245e
--- /dev/null
+++ b/addons/website_mail_channel/models/mail_mail.py
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, models, tools, _
+from odoo.addons.http_routing.models.ir_http import slug
+
+
+class MailMail(models.Model):
+ _inherit = 'mail.mail'
+
+ def _send_prepare_body(self):
+ """ Short-circuit parent method for mail groups, replace the default
+ footer with one appropriate for mailing-lists."""
+ if self.model == 'mail.channel' and self.res_id:
+ # no super() call on purpose, no private links that could be quoted!
+ channel = self.env['mail.channel'].browse(self.res_id)
+ base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
+ vals = {
+ 'maillist': _('Mailing-List'),
+ 'post_to': _('Post to'),
+ 'unsub': _('Unsubscribe'),
+ 'mailto': 'mailto:%s@%s' % (channel.alias_name, channel.alias_domain),
+ 'group_url': '%s/groups/%s' % (base_url, slug(channel)),
+ 'unsub_url': '%s/groups?unsubscribe' % (base_url,),
+ }
+ footer = """_______________________________________________
+ %(maillist)s: %(group_url)s
+ %(post_to)s: %(mailto)s
+ %(unsub)s: %(unsub_url)s
+ """ % vals
+ body = tools.append_content_to_html(self.body, footer, container_tag='div')
+ return body
+ else:
+ return super(MailMail, self)._send_prepare_body()
diff --git a/addons/website_mail_channel/models/website.py b/addons/website_mail_channel/models/website.py
new file mode 100644
index 00000000..854e99bd
--- /dev/null
+++ b/addons/website_mail_channel/models/website.py
@@ -0,0 +1,14 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import models, _
+from odoo.addons.http_routing.models.ir_http import url_for
+
+
+class Website(models.Model):
+ _inherit = "website"
+
+ def get_suggested_controllers(self):
+ suggested_controllers = super(Website, self).get_suggested_controllers()
+ suggested_controllers.append((_('Mailing Lists'), url_for('/groups'), 'website_mail_channel'))
+ return suggested_controllers