|
1 | 1 | from django import forms
|
2 | 2 | from django.forms.widgets import PasswordInput
|
| 3 | +from ajax_select.fields import AutoCompleteField |
3 | 4 |
|
4 |
| -from recommender_webapp.models import User, Profile |
5 |
| - |
6 |
| - |
7 |
| -class UserForm(forms.ModelForm): |
8 |
| - class Meta: |
9 |
| - model = User |
10 |
| - fields = ('email', 'password', 'first_name', 'last_name') |
| 5 | +from recommender_webapp.models import User, Profile, Comune |
11 | 6 |
|
12 | 7 |
|
13 | 8 | class ProfileForm(forms.ModelForm):
|
14 | 9 | class Meta:
|
15 | 10 | model = Profile
|
16 | 11 | fields = ('location',)
|
17 | 12 |
|
| 13 | + location = AutoCompleteField('cities') |
| 14 | + |
| 15 | + def clean(self, *args, **kwargs): |
| 16 | + location = self.cleaned_data.get('location') |
| 17 | + location_found = Comune.objects.filter(nome__iexact=location) |
| 18 | + if not location_found.exists(): |
| 19 | + raise forms.ValidationError("Location not found") |
| 20 | + |
18 | 21 |
|
19 | 22 | class UserRegisterForm(forms.ModelForm):
|
20 | 23 | email = forms.EmailField(label='Email address')
|
21 | 24 | email2 = forms.EmailField(label='Confirm Email')
|
22 | 25 | password = forms.CharField(widget=PasswordInput)
|
| 26 | + password2 = forms.CharField(widget=PasswordInput, label='Confirm Password') |
23 | 27 |
|
24 | 28 | class Meta:
|
25 | 29 | model = User
|
26 | 30 | fields = [
|
27 | 31 | 'email',
|
28 | 32 | 'email2',
|
29 | 33 | 'password',
|
| 34 | + 'password2', |
30 | 35 | 'first_name',
|
31 | 36 | 'last_name'
|
32 | 37 | ]
|
33 | 38 |
|
34 | 39 | def clean(self, *args, **kwargs):
|
35 | 40 | email = self.cleaned_data.get('email')
|
36 | 41 | email2 = self.cleaned_data.get('email2')
|
| 42 | + password = self.cleaned_data.get('password') |
| 43 | + password2 = self.cleaned_data.get('password2') |
37 | 44 | if email != email2:
|
38 | 45 | raise forms.ValidationError("Emails must match")
|
| 46 | + if password != password2: |
| 47 | + raise forms.ValidationError("Passwords must match") |
39 | 48 |
|
40 | 49 | email_qs = User.objects.filter(email=email)
|
41 | 50 | if email_qs.exists():
|
|
0 commit comments