|
| 1 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from odoo import _ |
| 6 | +from odoo.http import Controller, request, route |
| 7 | + |
| 8 | +from odoo.addons.mass_mailing.controllers.main import MassMailController |
| 9 | + |
| 10 | +_logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class MassMailController(MassMailController): |
| 14 | + @route("/website_mass_mailing/subscribe", type="json", website=True, auth="public") |
| 15 | + def subscribe(self, list_id, value, subscription_type, **post): |
| 16 | + if not request.env["ir.http"]._verify_request_recaptcha_token( |
| 17 | + "website_mass_mailing_subscribe" |
| 18 | + ): |
| 19 | + return { |
| 20 | + "toast_type": "danger", |
| 21 | + "toast_content": _("Suspicious activity detected by Google reCaptcha."), |
| 22 | + } |
| 23 | + |
| 24 | + fname = self._get_fname(subscription_type) |
| 25 | + # Customisation Start |
| 26 | + if subscription_type == "email": |
| 27 | + subscription = request.env["mailing.subscription"].sudo() |
| 28 | + # add email to session |
| 29 | + request.session["mass_mailing_email"] = ( |
| 30 | + subscription.double_opt_in_subscribe( |
| 31 | + list_id, |
| 32 | + value, |
| 33 | + language=post.get("language") or request.lang.code, |
| 34 | + ) |
| 35 | + ) |
| 36 | + else: |
| 37 | + self.subscribe_to_newsletter(subscription_type, value, list_id, fname) |
| 38 | + # Customisation End |
| 39 | + return { |
| 40 | + "toast_type": "success", |
| 41 | + "toast_content": _("Thanks for subscribing!"), |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +class ConsentController(Controller): |
| 46 | + def consent_success(self): |
| 47 | + """Successful consent to redirect to different sides if required""" |
| 48 | + base_url = request.httprequest.host_url.rstrip("/") |
| 49 | + redirect_url = base_url + "/subscribed" |
| 50 | + return request.redirect(redirect_url) |
| 51 | + |
| 52 | + def consent_failure(self): |
| 53 | + """Redirect to a public invalid page""" |
| 54 | + return request.redirect("/newsletter/invalid") |
| 55 | + |
| 56 | + @route("/newsletter/invalid", type="http", auth="public", website=True) |
| 57 | + def invalid_page(self, **kwargs): |
| 58 | + """Public route for invalid page (uses website layout)""" |
| 59 | + return request.render( |
| 60 | + "website_mass_mailing_double_opt_in.invalid_subscription_confirmation_template" |
| 61 | + ) |
| 62 | + |
| 63 | + @route("/subscribed", type="http", auth="public", website=True) |
| 64 | + def subscribed(self, **kwargs): |
| 65 | + return request.render("website_mass_mailing_double_opt_in.subscribe_newsletter") |
| 66 | + |
| 67 | + def _prepare_mail_content(self, mailing_list_contact, language): |
| 68 | + """Newsletter Subscribed email template content""" |
| 69 | + if language == "de_DE": |
| 70 | + # Get translated strings |
| 71 | + greeting = "Hallo," |
| 72 | + subject = "Sie haben den Newsletter abonniert" |
| 73 | + thank_you = "vielen Dank für die Anmeldung zum Newsletter." |
| 74 | + best_regards = "Viele Grüße," |
| 75 | + team = "Ihr Team" |
| 76 | + elif language == "en_US": |
| 77 | + greeting = "Hi!" |
| 78 | + subject = "You Have Subscribed to the Newsletter" |
| 79 | + thank_you = "Thank you for subscribing the newsletter." |
| 80 | + best_regards = "Best regards," |
| 81 | + team = "your team" |
| 82 | + else: |
| 83 | + # Get translated strings |
| 84 | + greeting = _("Hi!") |
| 85 | + subject = _("You Have Subscribed to the Newsletter") |
| 86 | + thank_you = _("Thank you for subscribing the newsletter.") |
| 87 | + best_regards = _("Best regards,") |
| 88 | + team = _("your team") |
| 89 | + mail_values = { |
| 90 | + "subject": subject, |
| 91 | + "body_html": f""" |
| 92 | + <div> |
| 93 | + <p>{greeting}</p> |
| 94 | + <p>{thank_you}</p> |
| 95 | + <br /> |
| 96 | + <p>{best_regards}<br />{team}</p> |
| 97 | + </div> |
| 98 | + """, |
| 99 | + "email_from": request.env.user.partner_id.email, |
| 100 | + "email_to": mailing_list_contact.contact_id.email, |
| 101 | + "state": "outgoing", |
| 102 | + } |
| 103 | + return mail_values |
| 104 | + |
| 105 | + @route( |
| 106 | + "/newsletter/confirmation/<access_token>", |
| 107 | + type="http", |
| 108 | + auth="none", |
| 109 | + website=True, |
| 110 | + ) |
| 111 | + def consent(self, access_token, **kwargs): |
| 112 | + try: |
| 113 | + mailing_list_contact = ( |
| 114 | + request.env["mailing.subscription"] |
| 115 | + .sudo() |
| 116 | + .search([("access_token", "=", access_token)], limit=1) |
| 117 | + ) |
| 118 | + if not mailing_list_contact: |
| 119 | + _logger.warning( |
| 120 | + "No mailing subscription found for access token: %s", access_token |
| 121 | + ) |
| 122 | + return self.consent_failure() |
| 123 | + |
| 124 | + mailing_list_contact.write({"opt_out": False}) |
| 125 | + |
| 126 | + # Send email with explicit user context for template rendering |
| 127 | + language = ( |
| 128 | + mailing_list_contact.mail_language or request.lang.code or "en_US" |
| 129 | + ) |
| 130 | + if not request.env.user: |
| 131 | + request.env.user = mailing_list_contact.create_uid |
| 132 | + try: |
| 133 | + mail_values = self._prepare_mail_content(mailing_list_contact, language) |
| 134 | + mail = request.env["mail.mail"].sudo().create(mail_values) |
| 135 | + mail.with_context(**{"lang": language}).sudo().send() |
| 136 | + except Exception as e: |
| 137 | + _logger.warning("Send mail issue: %s", e) |
| 138 | + return self.consent_success() |
| 139 | + |
| 140 | + except Exception as e: |
| 141 | + _logger.error("Error processing newsletter confirmation: %s", str(e)) |
| 142 | + return self.consent_failure() |
0 commit comments