Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from django import forms 

from django.contrib.auth import password_validation 

from django.contrib.auth.forms import UserChangeForm, UserCreationForm 

from django.forms.widgets import DateInput 

from django_countries.fields import CountryField 

from django_countries.widgets import CountrySelectWidget 

 

from .models import CustomUser 

from .validators import date_of_birth_validator 

 

 

class CustomUserCreationForm(UserCreationForm): 

 

class Meta(UserCreationForm): 

model = CustomUser 

fields = ('name_surname', 'username', 'email', 'country', 

'date_of_birth') 

widgets = {'date_of_birth': DateInput(attrs={'type': 'date'})} 

 

 

class CustomUserChangeForm(UserChangeForm): 

 

class Meta: 

model = CustomUser 

fields = UserChangeForm.Meta.fields 

 

 

class EditUser(forms.Form): 

name_surname = forms.CharField(label="Full Name", max_length=180) 

email = forms.EmailField(label="Email") 

password = forms.CharField( 

label="New password", widget=forms.PasswordInput(), required=False) 

country = CountryField(blank_label='Country').formfield() 

date_of_birth = forms.DateField(label="Birthday", validators=[ 

date_of_birth_validator], 

widget=DateInput(attrs={'type': 'date'})) 

GENDER_OPTIONS = ( 

('m', 'Male'), 

('f', 'Female') 

) 

gender = forms.ChoiceField( 

choices=GENDER_OPTIONS, required=False) 

website = forms.CharField(label="My website", required=False) 

about_me = forms.CharField(label="About Me", 

required=False, widget=forms.Textarea( 

attrs={'rows': 5}), 

max_length=500) 

 

def clean_password(self): 

password = self.cleaned_data.get('password') 

if password: 

try: 

password_validation.validate_password(password) 

except forms.ValidationError as error: 

self.add_error('password', error) 

return password