HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/unlimited-leads/Unlimited-Leads-Be/services/email/email_service.py
import logging
from django.core.mail import EmailMultiAlternatives
from django.conf import settings
from django.template.loader import get_template

from utils.utils import format_count_to_short

from authorization.models import UnlimitedLeadUser


class EmailService:
    def __init__(self, user_id) -> None:
        self.user_id = user_id
        self.from_email = settings.DEFAULT_FROM_EMAIL
        self.server_host = settings.SERVER_HOST
        self.log = logging.getLogger(__name__)

    def get_user(self):
        return UnlimitedLeadUser.objects.get(id=self.user_id)

    def create_email_html(self, subject, content, to_email, attachments=[]):
        email = EmailMultiAlternatives(
            subject, from_email=self.from_email, to=[to_email]
        )
        email.attach_alternative(content, "text/html")

        for attachment in attachments:
            email.attach(attachment["filename"], attachment["content"])

        return email


class AuthEmailService(EmailService):

    def send_forget_password(self, redirect_link):
        user = self.get_user()
        subject = "Your Password Reset Link Inside"
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
            "redirect_link": redirect_link,
        }
        template = get_template("forgot_password_email_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.exception("sending email for forget password failed")
            raise exp

    def send_email_verification(self, otp):
        user = self.get_user()
        subject = "Your OTP Code for Verification"
        context = {"base_url": self.server_host, "otp": otp}
        template = get_template("email_verify_email_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.exception("sending email for signup confirmation failed")
            raise exp
        
class AdminEmailService(EmailService):

    def send_confirmation_email(self, subject, message):
        user = self.get_user()
        context = {"base_url": self.server_host, "message": message}
        template = get_template("upload_completion_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.debug("sending email for upload completion failed")
            raise exp


class UserLeadsEmailService(EmailService):
    
    def send_user_lead_download_link(self, download_link,subject):
        user = self.get_user()
        subject = subject
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
            "download_link": download_link,
        }
        template = get_template("user_lead_download_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.exception("sending email for user lead download failed")
            raise exp
    def send_user_validation_link(self, download_link,subject):
        user = self.get_user()
        subject = subject
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
            "download_link": download_link,
        }
        template = get_template("validation.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.exception("sending email for user lead download failed")
            raise exp
    
    def send_user_scrapping_link(self, download_link,subject):
        user = self.get_user()
        subject = subject
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
            "download_link": download_link,
        }
        template = get_template("web_scrapping.html")
        
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.exception("sending email for user lead download failed")
            raise exp
    def send_saved_search_new_records_email(self, count):
        user = self.get_user()
        subject = "New Leads Found That Match Your Saved Filter!"
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
            "redirect_link": f"{settings.PROJECT_USER_HOST}/search-leads",
            "new_records_count": format_count_to_short(count),
        }
        template = get_template("saved_search_new_records_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.exception("sending email for user lead download failed")
            raise exp
        
class PaymentEmailService(EmailService):

    def send_payment_success_email(self, amount, plan, receipt_url, transaction_id, is_subscription):
        user = self.get_user()
        subject = "Payment Successful - Thank You!"
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
            "amount": amount,
            "plan": plan,
            "receipt_url": receipt_url,
            "transaction_id": transaction_id,
            "is_subscription": is_subscription,
        }
        template = get_template("payment_success_email_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.debug("Failed to send payment success email")
            raise exp

    def send_renewal_payment_email(self, payment_link):
        user = self.get_user()
        subject = "Your Subscription Has Expired - Renew to Continue!"
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
            "payment_link": payment_link,
            # "plan": plan,
            # "renewal_date": renewal_date.strftime("%B %d, %Y"),
        }
        template = get_template("renewal_payment_email_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.debug("Failed to send renewal payment email")
            raise exp

    def send_cancel_subscription_email(self):
        user = self.get_user()
        subject = "Subscription Cancelled - We're Sorry to See You Go"
        context = {
            "base_url": self.server_host,
            "name": f"{user.first_name} {user.last_name}",
        }
        template = get_template("cancel_subscription_email_template.html")
        content = template.render(context)
        email = self.create_email_html(subject, content, user.email)
        try:
            email.send()
        except Exception as exp:
            self.log.debug("Failed to send cancel subscription email")
            raise exp