diff options
Diffstat (limited to 'alphabetlearning/payments/views.py')
-rw-r--r-- | alphabetlearning/payments/views.py | 156 |
1 files changed, 25 insertions, 131 deletions
diff --git a/alphabetlearning/payments/views.py b/alphabetlearning/payments/views.py index 7682c35..cf442d7 100644 --- a/alphabetlearning/payments/views.py +++ b/alphabetlearning/payments/views.py @@ -1,31 +1,19 @@ -import requests import stripe +from alphabetlearning.payments.models import EmailSignup, EmailVerification +from alphabetlearning.resources.models import Resource +from alphabetlearning.users.models import User from django.conf import settings from django.contrib.auth.decorators import login_required -from django.core.mail import mail_admins -from django.core.mail import send_mail -from django.http import HttpResponse -from django.http import HttpResponseBadRequest -from django.shortcuts import get_object_or_404 -from django.shortcuts import redirect -from django.shortcuts import render -from django.urls import reverse -from django.urls import reverse_lazy +from django.core.mail import mail_admins, send_mail +from django.http import HttpResponse, HttpResponseBadRequest +from django.shortcuts import get_object_or_404, redirect, render +from django.urls import reverse, reverse_lazy from django.views import View from django.views.decorators.csrf import csrf_exempt -from django.views.generic import DeleteView -from django.views.generic import TemplateView -from django_ratelimit.decorators import ratelimit - -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 django.views.generic import DeleteView, TemplateView from .forms import EmailVerificationForm -from .models import CartItem -from .models import Price -from .models import ShoppingCart +from .models import CartItem, Price, ShoppingCart # TODO get the cart integrated with Stripe # Steps to convert our Cart into something that can be used with Stripe: @@ -58,11 +46,8 @@ class SuccessEmailSignupView(TemplateView): return context -@ratelimit(key="ip", rate="2/m", block=True) def email_signup_verification(request): if request.method == "POST": - if getattr(request, "limited", False): - return render(request, "payments/rate_limited.html", status=429) form = EmailVerificationForm(request.POST) if form.is_valid(): # Create pending verification @@ -75,41 +60,27 @@ def email_signup_verification(request): "payments:verify_email", args=[str(pending_verification.verification_token)] ) ) - # 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) + email = send_verification_email(form.cleaned_data.get("email"), verification_url) return render(request, "payments/verification_sent.html", {"email": email}) else: - # email = process_verification_emails(email=request.POST.get("email"), warn=True) - email = send_mailgun_verification(email=request.POST.get("email"), warn=True) + email = send_verification_email(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 send_mailgun_verification( - email: str, - verification_url: str = None, - warn=False, -): +def send_verification_email(email: str, verification_url: str = None, warn=False): if warn: warning_message = """ You are already subscribed to our list - no further action is required. """ - requests.post( - f"{settings.MAILGUN_API_URL}/{settings.MAILGUN_SENDER_DOMAIN}/messages", - auth=("api", settings.MAILGUN_API_KEY), - data={ - "from": f"No reply - Alphabet Learning <noreply@{settings.MAILGUN_SENDER_DOMAIN}>", - "to": [ - email, - ], - "subject": "Alphabet Learning - Email Verification", - "text": f"Thanks, {email} for signing up.\n\n{warning_message}\n\nBest regards,\n\nThe Alphabet Learning Team", - }, + send_mail( + subject="Alphabet Learning - Email Verification", + message=f"Thanks, {email} for signing up.\n\n{warning_message}\n\nBest regards,\n\nThe Alphabet Learning Team", + from_email=settings.DEFAULT_FROM_EMAIL, + recipient_list=[email], + fail_silently=False, ) else: warning_message = f""" @@ -117,94 +88,16 @@ def send_mailgun_verification( {verification_url} If you didn't request this, please ignore this email. """ - requests.post( - f"{settings.MAILGUN_API_URL}/{settings.MAILGUN_SENDER_DOMAIN}/messages", - auth=("api", settings.MAILGUN_API_KEY), - data={ - "from": f"No reply - Alphabet Learning <noreply@{settings.MAILGUN_SENDER_DOMAIN}>", - "to": [ - email, - ], - "subject": "Alphabet Learning - Email Verification", - "text": f"Thanks, {email} for signing up.\n\n{warning_message}\n\nBest regards,\n\nThe Alphabet Learning Team", - }, + send_mail( + subject="Alphabet Learning - Email Verification", + message=f"Thanks, {email} for signing up.\n\n{warning_message}\n\nBest regards,\n\nThe Alphabet Learning Team", + from_email=settings.DEFAULT_FROM_EMAIL, + recipient_list=[email], + fail_silently=False, ) return email -def process_verification_emails( - email: str, - verification_url: str = None, - 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> - """ - 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 = """ - <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> - <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 - - def verify_email(request, token): try: pending = EmailVerification.objects.get(verification_token=token, is_verified=False) @@ -280,6 +173,7 @@ class ProductLandingPageView(TemplateView): @login_required def add_to_cart(request, resource_id): + breakpoint() resource = get_object_or_404(Resource, id=resource_id) if not resource.price_obj.first(): return HttpResponseBadRequest( |