/
forms.py
100 lines (80 loc) · 3.19 KB
/
forms.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
import os
import stripe
from django.contrib.auth import get_user_model, forms as authforms
from django import forms
from django.contrib.contenttypes.models import ContentType
from django.core.mail import send_mail
from django.utils.translation import gettext_lazy as _
from .models import Profile, Project, create_profile_from_user
User = get_user_model()
stripe.api_key = os.environ.get("STRIPE_SECRET")
class UserCreationForm(authforms.UserCreationForm):
error_messages = {
"password_mismatch": _("The two password fields didn’t match."),
"duplicate_email": _("A user is already registered with this e-mail address."),
}
def save(self, commit=False):
user = super().save()
create_profile_from_user(user)
return user
class Meta(authforms.UserCreationForm.Meta):
model = User
fields = ("username", "email")
class UserChangeForm(authforms.UserChangeForm):
class Meta(authforms.UserChangeForm.Meta):
model = User
fields = ("username", "email")
class ConfirmUsernameForm(forms.ModelForm):
confirm_username = authforms.UsernameField(
widget=forms.TextInput(attrs={"autofocus": True})
)
class Meta:
model = User
fields = ("confirm_username",)
def clean(self):
cleaned_data = super().clean()
confirm_username = cleaned_data.get("confirm_username")
if confirm_username != self.instance.username:
self.add_error("confirm_username", "Username does not match.")
class CancelSubscriptionForm(ConfirmUsernameForm):
def save(self, commit=True):
user = super().save(commit=False)
if hasattr(user, "customer"):
user.customer.cancel_subscriptions()
user.profile.is_active = False
if commit:
user.profile.save()
user.save()
send_mail(
"You have unsubscribed from Compute Studio",
(
f"Hello {user.username}, you have recently unsubscribed "
f"from Compute Studio. We value your feedback. Please let us know why "
f"you unsubscribed and how we can win you back in the future."
),
"admin@compute.studio",
set([user.email, "matt@compute.studio"]),
fail_silently=True,
)
return user
class DeleteUserForm(CancelSubscriptionForm):
def save(self, commit=True):
user = super().save(commit=True)
username = user.username
email = user.email
user.delete()
send_mail(
"You have deleted your account",
(
f"Hello {user.username}, you have recently deleted your "
f"account. You have up to 5 days to change your mind and still "
f"recover your data. If this is a mistake, please contact us at "
f"admin@compute.studio as soon as possible. If this was not a "
f"mistake, please let us know why you deleted your account "
f"and how we can win you back in the future."
),
"admin@compute.studio",
set([user.email, "hank@compute.studio"]),
fail_silently=True,
)
return user