aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/payments/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-12-03 17:10:23 +0000
committerMatthew Lemon <y@yulqen.org>2024-12-03 17:10:23 +0000
commit3bcd728b0bd37c95865205a75ffbddfb2e086f90 (patch)
tree181a79c8e8b156509bfe03c34fd8ceff57382b20 /alphabetlearning/payments/views.py
parent38f7a88a15f1278df6a44242e2e22814914beddb (diff)
First cut at email verification
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r--alphabetlearning/payments/views.py95
1 files changed, 69 insertions, 26 deletions
diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py
index d0172d0..9691a9f 100644
--- a/alphabetlearning/payments/views.py
+++ b/alphabetlearning/payments/views.py
@@ -15,6 +15,7 @@ from django.views.generic import DeleteView
from django.views.generic import TemplateView
from alphabetlearning.payments.models import EmailSignup
+from alphabetlearning.payments.models import PendingEmailVerification
from alphabetlearning.resources.models import Resource
from alphabetlearning.users.models import User
@@ -43,38 +44,78 @@ from .models import ShoppingCart
stripe.api_key = settings.STRIPE_SECRET_KEY
+
class SuccessEmailSignupView(TemplateView):
template_name = "payments/success_email_signup.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
- context['email'] = self.request.POST.get('email')
+ context["email"] = self.request.POST.get("email")
return context
-def email_signup(request):
- if request.method == 'POST':
- email = request.POST.get('email')
- if email:
- EmailSignup.objects.get_or_create(email=email)
- # Send email to user
- send_mail(
- "Thank you for signing up",
- "You have successfully signed up for our newsletter.",
- settings.DEFAULT_FROM_EMAIL,
- [email],
- fail_silently=False,
- )
- # Send email to admin
- admin_email = "admin@example.com" # Replace with actual admin email
- send_mail(
- "New Email Signup",
- f"A new user has signed up with the email: {email}",
- settings.DEFAULT_FROM_EMAIL,
- [admin_email],
- fail_silently=False,
- )
- return redirect(reverse('payments:success_email_signup')) # Redirect to a success page or similar
- return render(request, 'pages/home.html') # Adjust as necessary
+
+def email_signup_verification(request):
+ if request.method == "POST":
+ email = request.POST.get("email")
+
+ # Create pending verification
+ pending_verification = PendingEmailVerification.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'
+ message = f'''
+ Hello!,
+
+ You recently requested to sign up to the Alpabet Learning contract list.
+
+ Please click the following link to verify your email address:
+ {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,
+ )
+ 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 = PendingEmailVerification.objects.get(
+ verification_token=token,
+ is_verified=False
+ )
+
+ # Create the subscriber
+ EmailSignup.objects.create(
+ email=pending.email,
+ )
+
+ # Mark as verified
+ pending.is_verified = True
+ pending.save()
+
+ return render(request, 'payments/verification_success.html')
+
+ except PendingEmailVerification.DoesNotExist:
+ return render(request, 'payments/verification_failed.html')
+
def create_line_items(resources):
price_objs = [r.price_obj.first() for r in resources]
@@ -205,6 +246,7 @@ def stripe_webhook(request):
)
return HttpResponse(status=200)
+
class DeleteCartItem(DeleteView):
model = CartItem
success_url = reverse_lazy("payments:cart_detail")
@@ -219,5 +261,6 @@ class DeleteCartItem(DeleteView):
request.user.shoppingcart.delete()
return redirect("resources:resource_list")
+
def privacy_policy(request):
- return render(request, 'pages/privacy_policy.html')
+ return render(request, "pages/privacy_policy.html")