1# Copyright 2013 The Distro Tracker Developers
2# See the COPYRIGHT file at the top-level directory of this distribution and
3# at https://deb.li/DTAuthors
4#
5# This file is part of Distro Tracker. It is subject to the license terms
6# in the LICENSE file found in the top-level directory of this
7# distribution and at https://deb.li/DTLicense. No part of Distro Tracker,
8# including this file, may be copied, modified, propagated, or distributed
9# except according to the terms contained in the LICENSE file.
10"""Django forms for django_email_accounts."""
12from django import forms
13from django.conf import settings
14from django.contrib.auth import get_user_model
15from django.contrib.auth.forms import \
16 AuthenticationForm as ContribAuthenticationForm
18from django_email_accounts import run_hook
19from django_email_accounts.models import UserEmail
21User = get_user_model()
24class AuthenticationForm(ContribAuthenticationForm):
25 def clean(self):
26 run_hook('pre-login', self)
27 cleaned_data = super(AuthenticationForm, self).clean()
29 return cleaned_data
32class UserCreationForm(forms.ModelForm):
33 """
34 A form for creating a user. It uses the user model returned by
35 django.contrib.auth.get_user_model(). That model must have
36 the main_email, first_name and last_name fields.
38 The created user has no privileges and its account is inactive until
39 a confirmation link is followed.
40 """
42 # Optional captcha
43 if getattr(settings, 'DJANGO_EMAIL_ACCOUNTS_USE_CAPTCHA', False): 43 ↛ 44line 43 didn't jump to line 44, because the condition on line 43 was never true
44 import captcha.fields
45 captcha = captcha.fields.CaptchaField()
47 class Meta:
48 model = User
49 fields = (
50 'main_email',
51 'first_name',
52 'last_name',
53 )
55 def clean_main_email(self):
56 main_email = self.cleaned_data['main_email']
57 # Check whether a different user is already associated with this
58 # email address.
59 try:
60 user_email = UserEmail.objects.get(email__iexact=main_email)
61 if user_email.user is not None: 61 ↛ 67line 61 didn't jump to line 67, because the condition on line 61 was never false
62 raise forms.ValidationError(
63 'The email address is already in use')
64 except UserEmail.DoesNotExist:
65 pass
67 if User.objects.filter(main_email__iexact=main_email).exists(): 67 ↛ 68line 67 didn't jump to line 68, because the condition on line 67 was never true
68 raise forms.ValidationError('The email address is already in use')
70 return main_email
72 def save(self, *args, **kwargs):
73 user = super(UserCreationForm, self).save(commit=True)
74 email, _ = UserEmail.objects.get_or_create(
75 email__iexact=user.main_email,
76 defaults={'email': user.main_email}
77 )
78 user.emails.add(email)
79 user.save()
81 return user
84class ResetPasswordForm(forms.Form):
85 """
86 A form for resetting a user's password.
88 The user must provide a new password and confirm the new password in
89 a separate field.
90 """
91 password1 = forms.CharField(
92 label='Password',
93 widget=forms.PasswordInput())
95 password2 = forms.CharField(
96 label='Repeat password',
97 widget=forms.PasswordInput())
99 def clean_password2(self):
100 password1 = self.cleaned_data['password1']
101 password2 = self.cleaned_data['password2']
103 if password1 != password2:
104 raise forms.ValidationError("The two passwords do not match.")
106 return password2
109class ChangePersonalInfoForm(forms.ModelForm):
110 """
111 A form providing a way for the user to change their account's
112 personal info.
113 """
114 class Meta:
115 model = User
116 fields = ['first_name', 'last_name']
119class ForgotPasswordForm(forms.Form):
120 email = forms.EmailField()
122 def clean_email(self):
123 email = self.cleaned_data['email']
124 if User.objects.filter(emails__email__iexact=email).count() == 0:
125 raise forms.ValidationError(
126 "No user with the given email is registered")
128 return email
131class AddEmailToAccountForm(forms.Form):
132 email = forms.EmailField()