From 4299a841c21f19267d5d3e033268a0e5d180664f Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 21 Jul 2022 13:50:09 +0100 Subject: [PATCH] Move views to module --- core/views.py | 69 ------------------------------------------ core/views/__init__.py | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 69 deletions(-) delete mode 100644 core/views.py diff --git a/core/views.py b/core/views.py deleted file mode 100644 index 85b592b..0000000 --- a/core/views.py +++ /dev/null @@ -1,69 +0,0 @@ -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 Plan, Session - -pp = pprint.PrettyPrinter(indent=4) - -# Create your views here - - -class Home(View): - template_name = "index.html" - - def get(self, request): - return render(request, self.template_name) - - -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: - session = stripe.checkout.Session.create( - 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")), - ) - 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) - - -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) diff --git a/core/views/__init__.py b/core/views/__init__.py index e69de29..85b592b 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -0,0 +1,69 @@ +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 Plan, Session + +pp = pprint.PrettyPrinter(indent=4) + +# Create your views here + + +class Home(View): + template_name = "index.html" + + def get(self, request): + return render(request, self.template_name) + + +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: + session = stripe.checkout.Session.create( + 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")), + ) + 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) + + +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)