Free and open source ticket system written in python
1from django import forms
2from django.conf import settings
3from .models import PawUser
4from django.utils.translation import gettext_lazy as _
5
6class UserChangeForm(forms.Form):
7 email = forms.EmailField(required=True, widget=forms.EmailInput(
8 attrs={'class': 'input w-full'}))
9 profile_picture = forms.ImageField(required=False, widget=forms.FileInput(
10 attrs={'class': 'file-input w-full'}))
11 language = forms.ChoiceField(choices=settings.LANGUAGES, widget=forms.Select(
12 attrs={'class': 'select w-full'}))
13 telegram_username = forms.CharField(required=False, widget=forms.TextInput(attrs={
14 'class': 'grow', 'placeholder': 'Telegram Username'}))
15 use_darkmode = forms.BooleanField(
16 required=False, widget=forms.CheckboxInput(attrs={'class': 'toggle toggle-secondary'}))
17 receive_email_notifications = forms.BooleanField(
18 required=False, widget=forms.CheckboxInput(attrs={'class': 'toggle toggle-secondary'}))
19
20 class Meta:
21 model = PawUser
22 fields = ('email', 'profile_picture',
23 'language', 'telegram_username', 'use_darkmode', 'receive_email_notifications')
24
25USERNAME_REGEX_FIELD = forms.RegexField(
26 required=True,
27 label=_('Username'),
28 max_length=50,
29 regex=r'^[a-zA-Z0-9-_@]+$',
30 error_messages={
31 'required': _('Please enter a username'),
32 'invalid': _('Username can only have alphanumeric characters and underscores and dashes (a-z, 0-9, _, -, @)')
33 },
34 widget=forms.TextInput(
35 attrs={'placeholder': _('Username'), 'class': 'input w-full'}),
36)
37
38class RegisterForm(forms.Form):
39 username = USERNAME_REGEX_FIELD
40 email = forms.EmailField(required=True, widget=forms.EmailInput(
41 attrs={'class': 'input w-full'}))
42 password = forms.CharField(required=True, widget=forms.PasswordInput(
43 attrs={'class': 'input w-full'}))
44 password_confirm = forms.CharField(required=True, widget=forms.PasswordInput(
45 attrs={'class': 'input w-full'}))
46
47 class Meta:
48 model = PawUser
49 fields = ('username', 'email', 'password')
50
51 def clean(self):
52 cleaned_data = super(RegisterForm, self).clean()
53 password = cleaned_data.get("password")
54 password_confirm = cleaned_data.get("password_confirm")
55
56 if password != password_confirm:
57 raise forms.ValidationError(
58 _("Password and Confirm Password do not match.")
59 )
60 if len(password) < 10:
61 raise forms.ValidationError(
62 _("Password must be at least 10 characters long.")
63 )
64
65 if PawUser.objects.filter(username=cleaned_data.get("username")).exists():
66 raise forms.ValidationError(
67 _("An account with this username already exists.")
68 )
69 if PawUser.objects.filter(email=cleaned_data.get("email")).exists():
70 raise forms.ValidationError(
71 _("An account with this email already exists.")
72 )
73 return cleaned_data
74
75
76class AccountFinishForm(forms.Form):
77 username = USERNAME_REGEX_FIELD
78
79 class Meta:
80 model = PawUser
81 fields = ('username')
82
83 def clean(self):
84 cleaned_data = super(AccountFinishForm, self).clean()
85
86 if PawUser.objects.filter(username=cleaned_data.get("username")).exists():
87 raise forms.ValidationError(
88 _("An account with this username already exists")
89 )
90
91 return cleaned_data