aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/payments/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-12-08 15:30:51 +0000
committerMatthew Lemon <y@yulqen.org>2024-12-08 15:30:51 +0000
commit9950234b1aa07fceed0d220297d158be6a067cd2 (patch)
tree23708f889cfc11f84dbc288508a29719a18ac53d /alphabetlearning/payments/views.py
parent30101453a75aaa635e3458d509dec164a6a68ac1 (diff)
wip: sorting out the email sending
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r--alphabetlearning/payments/views.py99
1 files changed, 61 insertions, 38 deletions
diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py
index dda7900..6290e08 100644
--- a/alphabetlearning/payments/views.py
+++ b/alphabetlearning/payments/views.py
@@ -1,3 +1,4 @@
+import requests
import stripe
from django.conf import settings
from django.contrib.auth.decorators import login_required
@@ -63,52 +64,77 @@ def email_signup_verification(request):
email=form.cleaned_data.get("email"),
)
# Generate verification URL
- verification_url:str = request.build_absolute_uri(
- reverse('payments:verify_email', args=[str(pending_verification.verification_token)])
+ verification_url: str = request.build_absolute_uri(
+ reverse(
+ "payments:verify_email", args=[str(pending_verification.verification_token)]
+ )
)
- email = process_verification_emails(verification_url, form.cleaned_data.get("email"), pending_verification, request)
- return render(request, 'payments/verification_sent.html', {
- 'email': email
- })
+ email = process_verification_emails(
+ email=form.cleaned_data.get("email"),
+ verification_url=verification_url,
+ )
+ # email = send_mailgun_verification(
+ # form.cleaned_data.get("email"), verification_url, pending_verification
+ # )
+ return render(request, "payments/verification_sent.html", {"email": email})
else:
- email = process_verification_emails(request.POST.get("email"), warn=True)
- return render(request, 'payments/verification_sent.html', {
- 'email': email
- })
+ email = process_verification_emails(email=request.POST.get("email"), warn=True)
+ return render(request, "payments/verification_sent.html", {"email": email})
else:
form = EmailVerificationForm()
return render(request, "pages/home.html", {"form": form}) # Adjust as necessary
-def process_verification_emails(email: str, verification_url: str=False, email_verification_obj: EmailVerification=False, warn=False):
+def send_mailgun_verification(
+ email: str,
+ verification_url: str,
+ email_verification_obj: EmailVerification = False,
+ warn=False,
+):
+ requests.post(
+ "https://api.mailgun.net/v3/sandbox98fd3a1c6501446aa57a9c8b76e56e15.mailgun.org/messages",
+ auth=("api", "7535a010b822cd503dc5a11670a64194-f55d7446-76d3865a"),
+ data={
+ "from": "Excited User <mailgun@sandbox98fd3a1c6501446aa57a9c8b76e56e15.mailgun.org>",
+ "to": [email, email],
+ "subject": "Hello - Test from Mailgun",
+ "text": f"Thanks, {email} for signing up.\n\nTesting some Mailgun awesomeness",
+ },
+ )
+ return email
+
+def process_verification_emails(
+ email: str,
+ verification_url: str = None,
+ warn=False,
+):
if warn is False:
- html_warning_message = f'''
+ html_warning_message = f"""
<p>Please click the following link to verify your email address within 24 hours:</p>
<p><a href="{verification_url}">{verification_url}</a></p>
<p>If you didn't request this, please ignore this email.</p>
- '''
- warning_message = f'''
+ """
+ warning_message = f"""
Please click the following link to verify your email address within 24 hours:
{verification_url}
If you didn't request this, please ignore this email.
- '''
+ """
admin_warn = "They are not already subscribed."
else:
- html_warning_message = f'''
+ html_warning_message = """
<p>You are already subscribed to our list - no further action is required.</p>
- '''
- warning_message = '''
+ """
+ warning_message = """
You are already subscribed to our list - no further action is required.
- '''
+ """
admin_warn = "They have already subscribed so have been told of this fact in their verification email. No further action required."
# Send verification email
- subject = 'Alphabet Learning - Email Verification'
- html_message = f'''
+ subject = "Alphabet Learning - Email Verification"
+ html_message = f"""
<html>
<body>
- <img src="http://localhost:8000/static/images/AL_long_logo_black_grey.png" alt="Alphabet Learning Logo" style="max-width: 200px; margin-bottom: 20px;">
<p>Hi!</p>
<p>You recently requested to sign up to the Alphabet Learning contact list.</p>
{html_warning_message}
@@ -116,8 +142,8 @@ def process_verification_emails(email: str, verification_url: str=False, email_v
<p>The Alphabet Learning Team</p>
</body>
</html>
- '''
- message = f'''
+ """
+ message = f"""
Hi!,
You recently requested to sign up to the Alphabet Learning contact list.
@@ -126,16 +152,16 @@ def process_verification_emails(email: str, verification_url: str=False, email_v
Best regards,
The Alphabet Learning Team
- '''
+ """
send_mail(
subject,
message,
settings.DEFAULT_FROM_EMAIL,
[email],
fail_silently=False,
- html_message=html_message
+ html_message=html_message,
)
- admin_message = f'''
+ admin_message = f"""
Joanna/Matthew,
{email} has just signed up to the Alphabet Learning contact list. They are awaiting verification.
@@ -146,20 +172,17 @@ def process_verification_emails(email: str, verification_url: str=False, email_v
Best regards,
The Alphabet Learning Server
- '''
+ """
mail_admins(subject, admin_message, fail_silently=False)
return email
def verify_email(request, token):
try:
- pending = EmailVerification.objects.get(
- verification_token=token,
- is_verified=False
- )
+ pending = EmailVerification.objects.get(verification_token=token, is_verified=False)
if pending.is_expired:
- return render(request, 'payments/verification_failed.html')
+ return render(request, "payments/verification_failed.html")
# Create the subscriber
EmailSignup.objects.create(
@@ -171,16 +194,16 @@ def verify_email(request, token):
pending.save()
mail_admins(
- subject=f'{pending.email} has just signed up to the Alphabet Learning contact list.',
- message=f'{pending.email} has just signed up to the Alphabet Learning contact list. Their verification was successful.\n\n\n'
- f'Best regards,\n\nThe Alphabet Learning Server',
+ subject=f"{pending.email} has just signed up to the Alphabet Learning contact list.",
+ message=f"{pending.email} has just signed up to the Alphabet Learning contact list. Their verification was successful.\n\n\n"
+ f"Best regards,\n\nThe Alphabet Learning Server",
fail_silently=False,
)
- return render(request, 'payments/success_email_signup.html')
+ return render(request, "payments/success_email_signup.html")
except EmailVerification.DoesNotExist:
- return render(request, 'payments/verification_failed.html')
+ return render(request, "payments/verification_failed.html")
def create_line_items(resources):