| 1 |
import re |
| 2 |
from markupsafe import Markup |
| 3 |
|
| 4 |
from odoo import api, models |
| 5 |
|
| 6 |
CARD_IMAGE_URL = re.compile(r'src=".*?/web/image/card.campaign/[0-9]+/image_preview"') |
| 7 |
CARD_PREVIEW_URL = re.compile(r'href=".*?/cards/[0-9]+/preview"') |
| 8 |
|
| 9 |
|
| 10 |
class MailComposeMessage(models.TransientModel): |
| 11 |
_inherit = 'mail.compose.message' |
| 12 |
|
| 13 |
def _prepare_mail_values_dynamic(self, res_ids): |
| 14 |
"""Replace generic card urls with the specific res_id url.""" |
| 15 |
mail_values_all = super()._prepare_mail_values_dynamic(res_ids) |
| 16 |
|
| 17 |
if campaign := self.mass_mailing_id.card_campaign_id: |
| 18 |
card_from_res_id = self.env['card.card'].search_fetch( |
| 19 |
[('campaign_id', '=', campaign.id), ('res_id', 'in', res_ids)], |
| 20 |
['res_id'], |
| 21 |
).grouped('res_id') |
| 22 |
|
| 23 |
processed_bodies = self._process_generic_card_url_body([ |
| 24 |
(card_from_res_id[res_id], mail_values.get('body_html')) |
| 25 |
for res_id, mail_values in mail_values_all.items() |
| 26 |
]) |
| 27 |
for mail_values, body in zip(mail_values_all.values(), processed_bodies): |
| 28 |
if body is not None: |
| 29 |
# in a mailing these are the same |
| 30 |
mail_values['body'] = body |
| 31 |
mail_values['body_html'] = body |
| 32 |
|
| 33 |
return mail_values_all |
| 34 |
|
| 35 |
def _get_done_emails(self, mail_values_dict): |
| 36 |
"""Consider every target gets a different card, hence we don't want unique message per email address.""" |
| 37 |
if self.mass_mailing_id.card_campaign_id: |
| 38 |
return [] |
| 39 |
return super()._get_done_emails(mail_values_dict) |
| 40 |
|
| 41 |
@api.model |
| 42 |
def _process_generic_card_url_body(self, card_body_pairs: list[tuple[models.Model, str]]) -> list[str]: |
| 43 |
"""Update the bodies with the specific card url for that res_id and create a card. |
| 44 |
|
| 45 |
example: (1, "/cards/9/preview") -> (1, "/cards/9/1/abchashtoken/preview") + new card as side-effect |
| 46 |
|
| 47 |
:return: processed bodies in the order they were received |
| 48 |
""" |
| 49 |
bodies = [] |
| 50 |
for card, body in card_body_pairs: |
| 51 |
if body: |
| 52 |
def fill_card_image_url(match): |
| 53 |
return Markup('src="{}"').format(card._get_path('card.jpg')) |
| 54 |
|
| 55 |
def fill_card_preview_url(match): |
| 56 |
return Markup('href="{}"').format(card._get_path('preview')) |
| 57 |
|
| 58 |
body_is_markup = False |
| 59 |
if isinstance(body, Markup): |
| 60 |
body_is_markup = True |
| 61 |
body = re.sub(CARD_IMAGE_URL, fill_card_image_url, body) |
| 62 |
body = re.sub(CARD_PREVIEW_URL, fill_card_preview_url, body) |
| 63 |
if body_is_markup: |
| 64 |
body = Markup(body) |
| 65 |
bodies.append(body) |
| 66 |
return bodies |