-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathforms.py
43 lines (32 loc) · 1.33 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
import re
from django import forms
from .models import DiscordRegisteredUser
discord_tag_regex = re.compile(r".{2,32}#[0-9]{4}")
class DiscordRegisterForm(forms.Form):
email = forms.EmailField(
)
discord_tag = forms.CharField(
label="Discord Tag",
widget=forms.TextInput(attrs={"placeholder": "pnunez#1337"}),
)
def clean(self):
cleaned_data = super().clean()
errors = []
email = cleaned_data.get("email")
if not email.endswith("@berkeley.edu"):
errors.append(
"You must use your @berkeley.edu email. "
"If you do not yet have one, please contact "
)
discord_tag = cleaned_data.get("discord_tag")
if not discord_tag_regex.fullmatch(discord_tag) or discord_tag.count("#") != 1:
errors.append("Invalid Discord tag")
if DiscordRegisteredUser.objects.filter(email=email).exists():
errors.append("Email already registered")
if DiscordRegisteredUser.objects.filter(discord_tag=discord_tag).exists():
errors.append("Discord Tag already registered")
if errors:
raise forms.ValidationError(errors)
return cleaned_data