"""Consent checks and unsubscribe helpers.""" from __future__ import annotations import html import re from datetime import datetime from typing import TYPE_CHECKING from urllib.parse import urlencode from django.conf import settings from django.core import signing from django.db.models import QuerySet from django.urls import reverse from django.utils import timezone from contacts.models import Channel, ConsentRecord, Contact, Suppression from messaging.models import Campaign, Message, MessageTemplate from messaging.shortener import resolve_display_url if TYPE_CHECKING: from django.contrib.auth.models import AbstractBaseUser AUDIENCE_CHANNEL = { Campaign.Audience.EMAIL_OPT_IN: Channel.EMAIL, Campaign.Audience.SMS_OPT_IN: Channel.SMS, Campaign.Audience.POSTCARD_OPT_IN: Channel.POSTCARD, Campaign.Audience.CARRIER_ROUTE: Channel.POSTCARD, } UNSUB_SALT = "monica-site-unsubscribe" UNSUB_MAX_AGE = 60 * 60 * 24 * 365 # 1 year # {{first_name}} preferred; {first_name} also accepted (composer hint legacy). _MERGE_TAG_RE = re.compile( r"\{\{\s*(first_name|last_name|email|phone|full_name)\s*\}\}" r"|\{\s*(first_name|last_name|email|phone|full_name)\s*\}", re.IGNORECASE, ) # Campaign tracked links: utm_source is always the brand; medium = channel. UTM_SOURCE = "monica" UTM_CAMPAIGN_SLUG_MAX = 80 _UTM_ANCHOR_RE = re.compile(r"(]*>)(.*?)()", re.IGNORECASE | re.DOTALL) _UTM_TEXT_URL_RE = re.compile( r"https?://[^\s<>\"]+utm_source=" + re.escape(UTM_SOURCE) + r"[^\s<>\"]*", re.IGNORECASE, ) # Minted short links in SMS (piha.li prod, beta.piha.li, local compose). _SHORT_TEXT_URL_RE = re.compile( r"https?://(?:(?:www\.)?(?:beta\.)?piha\.li|(?:127\.0\.0\.1|localhost):\d+)" r"/[a-z0-9]{4,8}", re.IGNORECASE, ) REMOVABLE_MESSAGE_STATUSES = frozenset( { Message.Status.DRAFT, Message.Status.SCHEDULED, Message.Status.FAILED, } ) def render_merge_tags(text: str, contact: Contact | None) -> str: """Replace personalization tags with contact field values.""" if not text: return text or "" first = (getattr(contact, "first_name", None) or "").strip() if contact else "" last = (getattr(contact, "last_name", None) or "").strip() if contact else "" email = (getattr(contact, "email", None) or "").strip() if contact else "" phone = (getattr(contact, "phone", None) or "").strip() if contact else "" full = f"{first} {last}".strip() values = { "first_name": first, "last_name": last, "email": email, "phone": phone, "full_name": full, } def _replace(match: re.Match[str]) -> str: key = (match.group(1) or match.group(2) or "").lower() return values.get(key, "") return _MERGE_TAG_RE.sub(_replace, text) def public_site_base_url() -> str: """Public homepage used as the UTM landing URL.""" base = (getattr(settings, "PUBLIC_SITE_URL", None) or "").rstrip("/") if not base: base = "https://mkdrealtor.com" return base def public_site_link_label() -> str: """Visible email-link text (host), with a stable label in local/dev.""" host = ( public_site_base_url() .replace("https://", "") .replace("http://", "") .split("/")[0] ) if host.startswith("127.") or host.startswith("localhost") or host.startswith("0.0.0.0"): return "MKDRealtor.com" return host or "MKDRealtor.com" def campaign_utm_slug(name: str) -> str: """Lowercase hyphenated utm_campaign from the campaign name.""" slug = re.sub(r"[^a-z0-9]+", "-", (name or "").strip().lower()) slug = re.sub(r"-{2,}", "-", slug).strip("-") return (slug or "campaign")[:UTM_CAMPAIGN_SLUG_MAX] def build_campaign_utm_url( *, name: str, medium: str, base_url: str = "", ) -> str: """Homepage URL with utm_source / utm_medium / utm_campaign.""" base = (base_url or public_site_base_url()).rstrip("/") channel = (medium or "").strip().lower() if channel not in Channel.values: channel = Channel.EMAIL query = urlencode( { "utm_source": UTM_SOURCE, "utm_medium": channel, "utm_campaign": campaign_utm_slug(name), } ) return f"{base}/?{query}" def campaign_shortener_ref(*, campaign_id=None, user_id=None, medium: str = "", name: str = "") -> str: """external_ref for the shortener (max 64). Prefer campaign UUID.""" if campaign_id: return str(campaign_id)[:64] slug = campaign_utm_slug(name)[:40] ch = (medium or "email").strip().lower()[:8] uid = "" if user_id is None else str(user_id) return f"p{uid}-{ch}-{slug}"[:64] def resolve_campaign_tracked_url( *, name: str, medium: str, campaign_id=None, user_id=None, shorten: bool = True, ) -> tuple[str, str]: """Return (long UTM URL, display URL). Display is short when mint works.""" target = build_campaign_utm_url(name=name, medium=medium) if not shorten: return target, target display = resolve_display_url( target, title=f"{(name or 'campaign').strip()} ({medium})"[:200], external_ref=campaign_shortener_ref( campaign_id=campaign_id, user_id=user_id, medium=medium, name=name ), ) return target, display def _is_our_utm_anchor(attrs: str) -> bool: lower = attrs.lower() return "data-monica-utm" in lower or f"utm_source={UTM_SOURCE}" in lower def ensure_campaign_utm_in_html(body: str, url: str, label: str) -> str: """Insert or refresh the tracked in an HTML (email) body.""" href = html.escape(url, quote=True) label_html = html.escape(label) replaced = False def _repl(match: re.Match[str]) -> str: nonlocal replaced if replaced or not _is_our_utm_anchor(match.group(1)): return match.group(0) replaced = True inner = match.group(2) if (match.group(2) or "").strip() else label_html return f'{inner}' out = _UTM_ANCHOR_RE.sub(_repl, body or "") if replaced: return out anchor = f'{label_html}' text = (body or "").rstrip() if text: return f"{text}\n
{anchor}
" return f"{anchor}
" def ensure_campaign_utm_in_text(body: str, url: str) -> str: """Insert or refresh the tracked URL in a plain-text (SMS) body.""" raw = body or "" if _UTM_TEXT_URL_RE.search(raw): return _UTM_TEXT_URL_RE.sub(url, raw, count=1) if _SHORT_TEXT_URL_RE.search(raw): return _SHORT_TEXT_URL_RE.sub(url, raw, count=1) text = raw.rstrip() return f"{text}\n\n{url}" if text else url def ensure_campaign_utm_link( body: str, *, name: str, medium: str, html: bool, campaign_id=None, shorten: bool | None = None, ) -> str: """Keep a tracked site link in the body, matching campaign name + channel.""" do_shorten = bool(campaign_id) if shorten is None else shorten _target, url = resolve_campaign_tracked_url( name=name, medium=medium, campaign_id=campaign_id, shorten=do_shorten, ) if html: return ensure_campaign_utm_in_html(body, url, public_site_link_label()) return ensure_campaign_utm_in_text(body, url) def message_is_removable(message: Message) -> bool: return message.status in REMOVABLE_MESSAGE_STATUSES def contact_may_receive(contact: Contact, channel: str) -> bool: if Suppression.objects.filter( contact=contact, channel=channel, active=True ).exists(): return False consent = ConsentRecord.objects.filter(contact=contact, channel=channel).first() if channel == Channel.POSTCARD: # Address on file defaults to postcard-eligible until explicit opt-out. if consent is None: return Contact.postal_address_has_content(contact.postal_address) return bool(consent.opted_in) return bool(consent and consent.opted_in) def channel_preferences(contact: Contact) -> dict[str, bool]: """Current opt-in flags for every channel (missing record = False). Postcard: missing consent + postal address → shown as opted in (default). """ flags = {c.value: False for c in Channel} seen: set[str] = set() for record in contact.consents.all(): flags[record.channel] = record.opted_in seen.add(record.channel) if ( Channel.POSTCARD not in seen and Contact.postal_address_has_content(contact.postal_address) ): flags[Channel.POSTCARD] = True return flags def set_channel_consent( contact: Contact, channel: str, *, opted_in: bool, reason: str = "", ) -> None: """Write ConsentRecord + Suppression for one channel.""" if channel not in Channel.values: raise ValueError(f"Unknown channel: {channel}") ConsentRecord.objects.update_or_create( contact=contact, channel=channel, defaults={"opted_in": opted_in, "reason": reason}, ) Suppression.objects.update_or_create( contact=contact, channel=channel, defaults={ "active": not opted_in, "reason": reason if not opted_in else "", }, ) def set_channel_preferences( contact: Contact, preferences: dict[str, bool], *, reason: str = "", ) -> None: """Update consent for each provided channel key.""" for channel, opted_in in preferences.items(): if channel not in Channel.values: continue set_channel_consent( contact, channel, opted_in=bool(opted_in), reason=reason ) def unsubscribe_all(contact: Contact, *, reason: str = "unsubscribe_all") -> None: for channel in Channel: set_channel_consent( contact, channel.value, opted_in=False, reason=reason ) def make_unsubscribe_token(contact_id: str, channel: str = Channel.EMAIL) -> str: return signing.dumps({"c": str(contact_id), "ch": channel}, salt=UNSUB_SALT) def parse_unsubscribe_token(token: str) -> tuple[Contact | None, str]: """Return (contact, channel) or (None, '') on bad/expired token.""" try: data = signing.loads(token, salt=UNSUB_SALT, max_age=UNSUB_MAX_AGE) except signing.BadSignature: return None, "" contact = ( Contact.objects.filter(pk=data.get("c")) .prefetch_related("consents") .first() ) if not contact: return None, "" channel = data.get("ch") or Channel.EMAIL if channel not in Channel.values: channel = Channel.EMAIL return contact, channel def process_unsubscribe_token(token: str) -> bool: """One-click opt-out for the channel encoded in the token.""" contact, channel = parse_unsubscribe_token(token) if not contact: return False set_channel_consent( contact, channel, opted_in=False, reason="unsubscribe_link" ) return True def preferences_url(contact_id: str, channel: str = Channel.EMAIL) -> str: token = make_unsubscribe_token(contact_id, channel) return reverse("public:unsubscribe", kwargs={"token": token}) def one_click_unsubscribe_url(contact_id: str, channel: str = Channel.EMAIL) -> str: token = make_unsubscribe_token(contact_id, channel) return reverse("public:unsubscribe_one_click", kwargs={"token": token}) def record_sms_stop(phone: str) -> bool: digits = "".join(ch for ch in (phone or "") if ch.isdigit()) if len(digits) < 7: return False tail = digits[-10:] contact = None for row in Contact.objects.exclude(phone="").iterator(): stored = "".join(ch for ch in row.phone if ch.isdigit()) if stored.endswith(tail) or tail.endswith(stored[-10:]): contact = row break if not contact: return False set_channel_consent( contact, Channel.SMS, opted_in=False, reason="sms_stop" ) return True def channel_for_audience(audience: str) -> str: try: return AUDIENCE_CHANNEL[audience] except KeyError as exc: raise ValueError(f"Unknown audience: {audience}") from exc def parse_route_targeting(raw: str | dict | None) -> dict: """Parse compose JSON for a carrier-route campaign.""" from messaging.providers.postcard.pcm import MAX_SELECTED_ROUTES data: dict = {} if isinstance(raw, dict): data = raw elif isinstance(raw, str) and raw.strip(): import json try: parsed = json.loads(raw) except json.JSONDecodeError as exc: raise ValueError("Carrier route selection is invalid JSON.") from exc if not isinstance(parsed, dict): raise ValueError("Carrier route selection must be an object.") data = parsed codes = [ str(code).strip() for code in (data.get("selected_codes") or data.get("routes") or []) if str(code).strip() ] # Allow routes as list of objects from the quote endpoint. if not codes and isinstance(data.get("routes"), list): codes = [ str(row.get("code") or "").strip() for row in data["routes"] if isinstance(row, dict) ] codes = [c for c in codes if c] codes = list(dict.fromkeys(codes)) if not codes: raise ValueError("Select one or two USPS carrier routes.") if len(codes) > MAX_SELECTED_ROUTES: raise ValueError("Pick at most two carrier routes.") try: list_count_id = int(data.get("list_count_id") or 0) record_count = int(data.get("record_count") or 0) except (TypeError, ValueError) as exc: raise ValueError("Route quote is missing list count.") from exc if list_count_id <= 0 or record_count <= 0: raise ValueError("Find routes and select them before saving (need a PCM quote).") estimated = data.get("estimated") if isinstance(data.get("estimated"), dict) else {} route_rows = data.get("route_rows") if isinstance(data.get("route_rows"), list) else [] return { "selected_codes": codes, "list_count_id": list_count_id, "record_count": record_count, "mail_class": str(data.get("mail_class") or "Standard"), "list_type": str(data.get("list_type") or "IRL"), "zip": str(data.get("zip") or ""), "estimated": estimated, "route_rows": route_rows, } def money_total(per_piece: float | None, quantity: int) -> float | None: if per_piece is None: return None return round(float(per_piece) * max(int(quantity or 0), 0), 2) def postcard_quote(*, action: str, payload: dict) -> dict: """Search routes or price a mailing-list / selected-route postcard.""" from messaging.providers.postcard.pcm import ( MAX_SELECTED_ROUTES, design_pricing, extract_crrt_rows, list_count_carrier_route, list_count_radius, list_count_zipcode, mail_class_for_size, pick_unit_price, ) template_id = str(payload.get("template_id") or "").strip() design_id = payload.get("design_id") size = str(payload.get("size") or "").strip() if template_id and not design_id: # Views may pass a compose value t:{notice}
' ), ) text_content = get_template("emails/marketing_email.txt").render(ctx) html_content = get_template("emails/marketing_email.html").render(ctx) email = EmailMultiAlternatives( subject=f"[TEST] {subject}", body=text_content, from_email=settings.DEFAULT_FROM_EMAIL, to=[to_email], ) email.attach_alternative(html_content, "text/html") email.send(fail_silently=False)