# utils/otp_utils.py import random from django.utils import timezone from datetime import timedelta from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from django.utils.html import strip_tags def generate_otp(): return str(random.randint(100000, 999999)) def send_otp_via_email(email, otp, user_name=None, context='registration'): try: context_data = { 'user_name': user_name or 'User', 'otp': otp, 'expiry_minutes': 10, 'company_name': 'Attune Heart Therapy', 'support_email': 'hello@attunehearttherapy.com', 'current_year': timezone.now().year, 'email_context': context } if context == 'password_reset': template_name = 'emails/password_reset_otp.html' subject = 'Password Reset Request - Verification Code' else: template_name = 'emails/otp_verification.html' subject = 'Your Verification Code - Secure Your Account' html_content = render_to_string(template_name, context_data) text_content = strip_tags(html_content) from_email = settings.DEFAULT_FROM_EMAIL to_email = [email] email_msg = EmailMultiAlternatives( subject, text_content, from_email, to_email ) email_msg.attach_alternative(html_content, "text/html") email_msg.send() return True except Exception as e: print(f"Error sending email OTP: {e}") return False def is_otp_expired(otp_expiry): if otp_expiry and timezone.now() < otp_expiry: return False return True