Files
odoo_source/addons/website_mass_mailing/controllers/main.py
T
Thibault Delavallée b897571b9f [REF] mass_mailing: rename subscription model
PURPOSE

Overall cleaning of subscription and exclusion management code from portal.
This code comes mainly from v12 and can now benefit from cleaning and update.

SPECIFICATIONS

In this commit we rename ``mailing.contact.subscription`` model into the
shorter ``mailing.subscription``. This is sufficient to explain the model
purpose. As we plan to add an opt-out model this also allows to have sub
models with suffixes without being too long.

We also rename ``subscription_list_ids`` field on contact model to
``subscription_ids`` as this is shorter and clearer.

Finally the ``mailing_contact_list_rel`` table name that comes from old
implementations (simple m2m table) is renamed to ``mailing_subscription``
to match the model name.

Task-2669037 (Mass Mailing: Refactor js/portal for subscription)

Part-of: odoo/odoo#86084
2023-10-06 06:13:53 +00:00

67 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import tools, _
from odoo.http import route, request
from odoo.addons.mass_mailing.controllers import main
class MassMailController(main.MassMailController):
@route('/website_mass_mailing/is_subscriber', type='json', website=True, auth='public')
def is_subscriber(self, list_id, subscription_type, **post):
value = self._get_value(subscription_type)
fname = self._get_fname(subscription_type)
is_subscriber = False
if value and fname:
contacts_count = request.env['mailing.subscription'].sudo().search_count(
[('list_id', 'in', [int(list_id)]), (f'contact_id.{fname}', '=', value), ('opt_out', '=', False)])
is_subscriber = contacts_count > 0
return {'is_subscriber': is_subscriber, 'value': value}
def _get_value(self, subscription_type):
value = None
if subscription_type == 'email':
if not request.env.user._is_public():
value = request.env.user.email
elif request.session.get('mass_mailing_email'):
value = request.session['mass_mailing_email']
return value
def _get_fname(self, subscription_type):
return 'email' if subscription_type == 'email' else ''
@route('/website_mass_mailing/subscribe', type='json', website=True, auth='public')
def subscribe(self, list_id, value, subscription_type, **post):
if not request.env['ir.http']._verify_request_recaptcha_token('website_mass_mailing_subscribe'):
return {
'toast_type': 'danger',
'toast_content': _("Suspicious activity detected by Google reCaptcha."),
}
ContactSubscription = request.env['mailing.subscription'].sudo()
Contacts = request.env['mailing.contact'].sudo()
if subscription_type == 'email':
name, value = tools.parse_contact_from_email(value)
elif subscription_type == 'mobile':
name = value
fname = self._get_fname(subscription_type)
subscription = ContactSubscription.search(
[('list_id', '=', int(list_id)), (f'contact_id.{fname}', '=', value)], limit=1)
if not subscription:
# inline add_to_list as we've already called half of it
contact_id = Contacts.search([(fname, '=', value)], limit=1)
if not contact_id:
contact_id = Contacts.create({'name': name, fname: value})
ContactSubscription.create({'contact_id': contact_id.id, 'list_id': int(list_id)})
elif subscription.opt_out:
subscription.opt_out = False
# add email to session
request.session[f'mass_mailing_{fname}'] = value
return {
'toast_type': 'success',
'toast_content': _("Thanks for subscribing!"),
}