from fansifter_common.adapters.ows_account import OwsAccountClient from fansifter_common.adapters.sendgrid import SendGridClient from fansifter_common.adapters.sendgrid.models import apply_fallback_merge_tags from fansifter_common.core.enums import Brand from fansifter_common.legal_info.services import LegalInfoService from fansifter_common.translation.services import TranslationService from email_campaigns.emails.models import BaseEmail def apply_preview_substitutions( html: str, *, email: BaseEmail, user_email: str, sendgrid_client: SendGridClient, legal_info_service: LegalInfoService, translation_service: TranslationService, ows_account_client: OwsAccountClient, ) -> str: vendor = ows_account_client.get_vendor(email.vendor_id) brand = email.email_domain.brand if email.email_domain else Brand.SME fan_country_iso2 = legal_info_service.default_country_code legal_entity = legal_info_service.get_legal_entity(fan_country_iso2) translations = translation_service.get_translations_by_country_code( fan_country_iso2 ) footer_html = sendgrid_client.get_user_privacy_footer_block( email=user_email, privacy_links_block=legal_info_service.prepare_privacy_links_for_country( fan_country_iso2 ), opt_in_info=translations["en"].optInInfo, opt_in_info_external=translations["en"].optInInfoExternal, unsubscribe_text=translations["en"].unsubscribe, unsubscribe_url="#", vendor_id=vendor.vendor_id, vendor_name=vendor.name, legal_entity=legal_entity, brand=brand, preview=True, ) html = html.replace("-privacyFooterBlock-", footer_html) html = html.replace("-doubleOptInUrl-", "#") return apply_fallback_merge_tags(html)