Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('campaigns', '0009_campaign_cached_counters'),
('campaigns', '0009_campaignlead_bounce_metadata'),
]

operations = []
26 changes: 13 additions & 13 deletions backend/campaigns/tasks.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import logging from datetime
import timedelta from celery
import shared_task from django.conf
import settings as django_settings from django.utils
import timezone from .ai
import _apply_merge_tags, personalize_email from .gmail_service
import build_unsubscribe_url, check_for_replies, send_gmail from .sms_service
import send_sms, initiate_call from .models
import CampaignLead, SequenceStep from leads.models
import BlockedDomain, normalize_domain


import logging
import urllib.parse
from datetime import timedelta

from bs4 import BeautifulSoup
from celery import shared_task
from django.conf import settings as django_settings
from django.core.signing import Signer
from django.utils import timezone

from .ai import _apply_merge_tags, personalize_email
from .gmail_service import build_unsubscribe_url, check_for_replies, send_gmail
from .models import CampaignLead, SequenceStep
from .sms_service import send_sms, initiate_call
from leads.models import BlockedDomain, normalize_domain


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -692,4 +692,4 @@ def poll_gmail_for_replies():
logger.info(f"Reply detected for {clead.lead.email} in campaign {clead.campaign.name}")
_maybe_mark_campaign_completed(clead.campaign)

return f"Detected {total_replies} new replies."
return f"Detected {total_replies} new replies."
21 changes: 21 additions & 0 deletions backend/campaigns/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,27 @@ def test_unsubscribe_get_shows_confirmation_without_updating_lead(self):
lead.refresh_from_db()
self.assertFalse(lead.global_unsubscribe)

def test_unsubscribe_get_uses_organization_branding_when_available(self):
self.organization.unsubscribe_title = 'Stay in touch'
self.organization.unsubscribe_message = 'We will miss you, but you can leave anytime.'
self.organization.brand_logo_url = 'https://cdn.example.test/logo.png'
self.organization.save(update_fields=['unsubscribe_title', 'unsubscribe_message', 'brand_logo_url'])

lead = Lead.objects.create(
organization=self.organization,
email='branded@acme.test',
)
token = generate_unsubscribe_token(lead.id)

response = self.client.get(f'/api/v1/unsubscribe/{lead.id}/{token}/')

self.assertEqual(response.status_code, status.HTTP_200_OK)
html = response.content.decode('utf-8')
self.assertIn('Stay in touch', html)
self.assertIn('We will miss you, but you can leave anytime.', html)
self.assertIn('https://cdn.example.test/logo.png', html)
self.assertIn('brand-logo', html)

def test_unsubscribe_post_marks_lead_unsubscribed(self):
lead = Lead.objects.create(
organization=self.organization,
Expand Down
37 changes: 26 additions & 11 deletions backend/campaigns/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import urllib.parse
from html import escape
from django.core.signing import Signer, BadSignature
from django.http import HttpResponseRedirect, HttpResponseBadRequest
# ------------------------------------------
Expand Down Expand Up @@ -719,20 +720,23 @@ def _build_fallback_content(self, request):


def _unsubscribe_page(title, message, extra_html=''):
safe_title = escape(title)
safe_message = escape(message)
return (
'<!DOCTYPE html>'
'<html lang="en">'
'<head>'
'<meta charset="utf-8">'
'<meta name="viewport" content="width=device-width,initial-scale=1">'
f'<title>{title} | LeadOrbit</title>'
f'<title>{safe_title} | LeadOrbit</title>'
'<style>body{margin:0;font-family:Inter,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Ubuntu,sans-serif;background:#f8fafc;color:#111827;}'
'.container{max-width:720px;margin:72px auto;padding:32px;background:#ffffff;border:1px solid #e5e7eb;border-radius:24px;box-shadow:0 20px 80px rgba(15,23,42,.08);}'
'h1{margin-top:0;font-size:2rem;color:#0f172a;}p{font-size:1rem;line-height:1.7;color:#475569;}'
'button{margin-top:12px;border:0;border-radius:999px;background:#1d4ed8;color:#fff;font-weight:700;padding:12px 20px;cursor:pointer;}'
'.brand-logo{display:block;max-height:72px;max-width:220px;object-fit:contain;margin:0 0 18px 0;}'
'</style>'
'</head>'
f'<body><div class="container"><h1>{title}</h1><p>{message}</p>{extra_html}</div></body>'
f'<body><div class="container">{extra_html}<h1>{safe_title}</h1><p>{safe_message}</p></div></body>'
'</html>'
)

Expand All @@ -755,6 +759,20 @@ def unsubscribe_view(request, lead_id, token):
status=404,
)

organization = lead.organization
custom_title = organization.unsubscribe_title or 'Confirm unsubscribe'
custom_message = (
organization.unsubscribe_message
or 'Please confirm that you want to unsubscribe from future emails sent through LeadOrbit.'
)
brand_logo_url = (organization.brand_logo_url or '').strip()
brand_logo_html = ''
if brand_logo_url:
brand_logo_html = (
f'<img class="brand-logo" src="{escape(brand_logo_url)}" '
f'alt="{escape(organization.name)} logo">'
)

if request.method != 'POST':
csrf_token = get_token(request)
form = (
Expand All @@ -763,20 +781,17 @@ def unsubscribe_view(request, lead_id, token):
'<button type="submit">Confirm unsubscribe</button>'
'</form>'
)
html = _unsubscribe_page(
'Confirm unsubscribe',
'Please confirm that you want to unsubscribe from future emails sent through LeadOrbit.',
form,
)
html = _unsubscribe_page(custom_title, custom_message, brand_logo_html + form)
return HttpResponse(html, content_type='text/html')

lead.global_unsubscribe = True
lead.save(update_fields=["global_unsubscribe"])

html = _unsubscribe_page(
'Unsubscribed',
'You have been unsubscribed from all future emails sent through LeadOrbit.',
'<p>If you received this link by mistake, no further action is needed.</p>',
organization.unsubscribe_title or 'Unsubscribed',
organization.unsubscribe_message
or 'You have been unsubscribed from all future emails sent through LeadOrbit.',
brand_logo_html + '<p>If you received this link by mistake, no further action is needed.</p>',
)

return HttpResponse(html, content_type='text/html')
Expand Down Expand Up @@ -820,4 +835,4 @@ def get(self, request, *args, **kwargs):
# Original Destination par redirect karna
decoded_dest = urllib.parse.unquote(dest_url)
return HttpResponseRedirect(decoded_dest)
# ------------------------------------------
# ------------------------------------------
28 changes: 28 additions & 0 deletions backend/tenants/migrations/0003_add_unsubscribe_branding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 4.2.30 on 2026-06-22 06:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('tenants', '0002_organization_enable_ai_personalization_and_more'),
]

operations = [
migrations.AddField(
model_name='organization',
name='brand_logo_url',
field=models.URLField(blank=True, null=True),
),
migrations.AddField(
model_name='organization',
name='unsubscribe_message',
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name='organization',
name='unsubscribe_title',
field=models.CharField(blank=True, max_length=120, null=True),
),
]
3 changes: 3 additions & 0 deletions backend/tenants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Organization(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
gemini_api_key = models.CharField(max_length=255, blank=True, null=True)
enable_ai_personalization = models.BooleanField(default=True)
unsubscribe_title = models.CharField(max_length=120, blank=True, null=True)
unsubscribe_message = models.TextField(blank=True, null=True)
brand_logo_url = models.URLField(blank=True, null=True)

def __str__(self):
return self.name
Expand Down