diff options
author | Matthew Lemon <y@yulqen.org> | 2024-12-05 17:42:08 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-12-05 17:42:08 +0000 |
commit | 30101453a75aaa635e3458d509dec164a6a68ac1 (patch) | |
tree | ceb332bad155a7abe037acc9c123a0bf42dd9ec7 /alphabetlearning/payments/views.py | |
parent | 7cd2fd2b75bc5597e9b2a128bf61201910be6df2 (diff) |
Add unique constraint to EmailVerification email field
This update enhances the EmailVerification model by ensuring the email field is unique, preventing duplicate records. A new form, EmailVerificationForm, was also introduced to handle email cleaning and validation, which enhances user input handling. Additionally, existing views and templates have been updated to integrate this form, improving the user experience and error feedback.
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r-- | alphabetlearning/payments/views.py | 147 |
1 files changed, 89 insertions, 58 deletions
diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py index 4b6f79f..dda7900 100644 --- a/alphabetlearning/payments/views.py +++ b/alphabetlearning/payments/views.py @@ -18,7 +18,7 @@ from alphabetlearning.payments.models import EmailSignup from alphabetlearning.payments.models import EmailVerification from alphabetlearning.resources.models import Resource from alphabetlearning.users.models import User - +from .forms import EmailVerificationForm from .models import CartItem from .models import Price from .models import ShoppingCart @@ -56,70 +56,101 @@ class SuccessEmailSignupView(TemplateView): def email_signup_verification(request): if request.method == "POST": - email = request.POST.get("email") - - # Create pending verification - pending_verification = EmailVerification.objects.create( - email=email, - ) - - # Generate verification URL - verification_url = request.build_absolute_uri( - reverse('payments:verify_email', args=[str(pending_verification.verification_token)]) - ) - - # Send verification email - 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> - <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> - <p>Best regards,</p> - <p>The Alphabet Learning Team</p> - </body> - </html> + form = EmailVerificationForm(request.POST) + if form.is_valid(): + # Create pending verification + pending_verification = EmailVerification.objects.create( + 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)]) + ) + email = process_verification_emails(verification_url, form.cleaned_data.get("email"), pending_verification, request) + 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 + }) + 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): + + if warn is False: + 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> ''' - message = f''' - Hi!, - - You recently requested to sign up to the Alphabet Learning contact list. - + 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. - - Best regards, - The Alphabet Learning Team ''' - send_mail( - subject, - message, - settings.DEFAULT_FROM_EMAIL, - [email], - fail_silently=False, - html_message=html_message - ) - admin_message = f''' - Joanna/Matthew, - - {email} has just signed up to the Alphabet Learning contact list. They are awaiting verification. - - I will email again if they follow through with the verification. - - Best regards, - The Alphabet Learning Server + admin_warn = "They are not already subscribed." + else: + html_warning_message = f''' + <p>You are already subscribed to our list - no further action is required.</p> ''' + 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''' + <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} + <p>Best regards,</p> + <p>The Alphabet Learning Team</p> + </body> + </html> + ''' + message = f''' + Hi!, + + You recently requested to sign up to the Alphabet Learning contact list. + + {warning_message} + + Best regards, + The Alphabet Learning Team + ''' + send_mail( + subject, + message, + settings.DEFAULT_FROM_EMAIL, + [email], + fail_silently=False, + html_message=html_message + ) + admin_message = f''' + Joanna/Matthew, + + {email} has just signed up to the Alphabet Learning contact list. They are awaiting verification. + + {admin_warn} + + I will email again if they follow through with the verification. + + Best regards, + The Alphabet Learning Server + ''' + mail_admins(subject, admin_message, fail_silently=False) + return email + - mail_admins(subject, admin_message, fail_silently=False) - return render(request, 'payments/verification_sent.html', { - 'email': email - }) - return render(request, "pages/home.html") # Adjust as necessary def verify_email(request, token): try: pending = EmailVerification.objects.get( |