neptune/core/views/__init__.py

95 lines
3.4 KiB
Python
Raw Normal View History

2022-07-21 12:50:45 +00:00
import logging
2022-07-21 12:50:09 +00:00
import pprint
import stripe
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import JsonResponse
from django.shortcuts import redirect, render
from django.urls import reverse, reverse_lazy
from django.views import View
from django.views.generic.edit import CreateView
from core.forms import NewUserForm
from core.lib.products import assemble_plan_map
from core.models import ContentBlock, Plan, Session
2022-07-21 12:50:09 +00:00
2022-07-21 12:50:45 +00:00
logger = logging.getLogger(__name__)
2022-07-21 12:50:09 +00:00
pp = pprint.PrettyPrinter(indent=4)
# Create your views here
class Home(View):
template_name = "index.html"
def get(self, request):
2022-07-21 12:50:36 +00:00
blocks = ContentBlock.objects.filter(page="home").order_by("position")
context = {"blocks": blocks}
return render(request, self.template_name, context)
2022-07-21 12:50:09 +00:00
class Billing(LoginRequiredMixin, View):
template_name = "billing.html"
def get(self, request):
context = {"plans": Plan.objects.all(), "user_plans": request.user.plans.all()}
return render(request, self.template_name, context)
class Order(LoginRequiredMixin, View):
def get(self, request, plan_name):
plan = Plan.objects.get(name=plan_name)
try:
2022-07-21 12:50:51 +00:00
cast = {
"payment_method_types": settings.ALLOWED_PAYMENT_METHODS,
"mode": "subscription",
"customer": request.user.stripe_id,
"line_items": assemble_plan_map(product_id_filter=plan.product_id),
"success_url": request.build_absolute_uri(reverse("success")),
"cancel_url": request.build_absolute_uri(reverse("cancel")),
}
if request.user.is_superuser:
cast["discounts"] = [{"coupon": settings.STRIPE_ADMIN_COUPON}]
session = stripe.checkout.Session.create(**cast)
2022-07-21 12:50:09 +00:00
Session.objects.create(user=request.user, session=session.id)
return redirect(session.url)
# return JsonResponse({'id': session.id})
except Exception as e:
# Raise a server error
return JsonResponse({"error": str(e)}, status=500)
2022-07-21 12:50:45 +00:00
class Cancel(LoginRequiredMixin, View):
def get(self, request, plan_name):
plan = Plan.objects.get(name=plan_name)
try:
subscriptions = stripe.Subscription.list(
customer=request.user.stripe_id, price=plan.product_id
)
for subscription in subscriptions["data"]:
items = subscription["items"]["data"]
for item in items:
stripe.Subscription.delete(item["subscription"])
return render(request, "subscriptioncancel.html", {"plan": plan})
# return JsonResponse({'id': session.id})
except Exception as e:
# Raise a server error
logging.error(f"Error cancelling subscription for user: {e}")
return JsonResponse({"error": "True"}, status=500)
2022-07-21 12:50:09 +00:00
class Signup(CreateView):
form_class = NewUserForm
success_url = reverse_lazy("login")
template_name = "registration/signup.html"
class Portal(LoginRequiredMixin, View):
def get(self, request):
session = stripe.billing_portal.Session.create(
customer=request.user.stripe_id,
return_url=request.build_absolute_uri(reverse("billing")),
)
return redirect(session.url)