-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
144 lines (107 loc) · 4.79 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from django.urls import reverse_lazy, reverse
from django.views import generic, View
from django.views.generic.edit import FormView
from django.shortcuts import render, get_object_or_404
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.contrib.auth import get_user_model, login
from django.utils.safestring import mark_safe
from rest_framework.views import APIView
from rest_framework.response import Response
from webapp.settings import USE_STRIPE
from .forms import UserCreationForm, CancelSubscriptionForm, DeleteUserForm
from .models import is_profile_active, Project, create_profile_from_user
from django.dispatch import receiver
from allauth.account.signals import user_signed_up
@receiver(user_signed_up)
def user_signed_up_callback(request, user, **kwargs):
create_profile_from_user(user)
class SignUp(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy("home")
template_name = "registration/signup.html"
def get_success_url(self):
if self.request.GET.get("next"):
return self.request.GET.get("next")
return super().get_success_url()
def form_valid(self, form):
res = super().form_valid(form)
login(self.request, self.object, "django.contrib.auth.backends.ModelBackend")
return res
def post(self, request, *args, **kwargs):
return super().post(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["use_stripe"] = USE_STRIPE
return context
class UserSettings(View):
template_name = ("registration/settings.html",)
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
banner_msg = None
if getattr(request.user, "customer", None) is not None:
current_si = request.user.customer.current_plan(as_dict=False)
if (
current_si is not None
and current_si.subscription.is_trial()
and current_si.subscription.cancel_at is not None
):
banner_msg = mark_safe(
f"""
<p>Your free C/S Pro trial ends on {current_si.subscription.trial_end.date()}.</p>
<p>
<a class="btn btn-primary" href="/billing/upgrade/monthly/aftertrial/">
<strong>Upgrade to C/S Pro after trial</strong>
</a>
</p>
"""
)
return render(
request,
self.template_name,
context={"username": request.user.username, "banner_msg": banner_msg},
)
class CancelSubscription(generic.edit.UpdateView):
template_name = "registration/cancel_subscription.html"
form_class = CancelSubscriptionForm
success_url = reverse_lazy("cancel_subscription_done")
@method_decorator(login_required)
@method_decorator(user_passes_test(is_profile_active))
def get(self, request, *args, **kwargs):
return super().get(self, request, *args, **kwargs)
@method_decorator(login_required)
@method_decorator(user_passes_test(is_profile_active))
def post(self, request, *args, **kwargs):
return super().post(self, request, *args, **kwargs)
@method_decorator(login_required)
@method_decorator(user_passes_test(is_profile_active))
def put(self, request, *args, **kwargs):
return super().put(self, request, *args, **kwargs)
def get_object(self):
return self.request.user
class CancelSubscriptionDone(generic.TemplateView):
template_name = "registration/cancel_subscription_done.html"
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
class DeleteUser(generic.edit.UpdateView):
template_name = "registration/delete_user.html"
form_class = DeleteUserForm
success_url = reverse_lazy("delete_user_done")
@method_decorator(login_required)
def get(self, request, *args, **kwargs):
return super().get(self, request, *args, **kwargs)
@method_decorator(login_required)
def post(self, request, *args, **kwargs):
return super().post(self, request, *args, **kwargs)
@method_decorator(login_required)
def put(self, request, *args, **kwargs):
return super().put(self, request, *args, **kwargs)
def get_object(self):
return self.request.user
class DeleteUserDone(generic.TemplateView):
template_name = "registration/delete_user_done.html"
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)