|
1 | 1 | import re |
2 | 2 |
|
3 | | -from django import forms |
4 | | -from django.core.validators import RegexValidator |
5 | | -from django.core.exceptions import ValidationError |
6 | | -from django.db import IntegrityError, transaction |
7 | 3 | from django.http import HttpResponseRedirect |
8 | | -from django.views.generic.edit import FormView |
9 | | -from django.utils.translation import ugettext as _ |
10 | | -from django.template.loader import render_to_string |
11 | | -from django.urls import reverse, reverse_lazy |
12 | | -from django.views.generic import TemplateView |
13 | 4 | from django.views import View |
14 | | -from django.contrib import messages |
15 | | -from django.contrib.auth import get_user_model |
16 | | -from django.contrib.sites.requests import RequestSite |
| 5 | +from django.conf import settings |
17 | 6 |
|
18 | | -from mygpo.utils import random_token |
19 | | -from mygpo.users.models import UserProxy |
20 | 7 |
|
21 | | - |
22 | | -USERNAME_MAXLEN = get_user_model()._meta.get_field('username').max_length |
23 | | - |
24 | | - |
25 | | -class DuplicateUsername(ValidationError): |
26 | | - """ The username is already in use """ |
27 | | - |
28 | | - def __init__(self, username): |
29 | | - self.username = username |
30 | | - super().__init__('The username {0} is already in use.' |
31 | | - .format(username)) |
32 | | - |
33 | | - |
34 | | -class DuplicateEmail(ValidationError): |
35 | | - """ The email address is already in use """ |
36 | | - |
37 | | - def __init__(self, email): |
38 | | - self.email = email |
39 | | - super().__init__('The email address {0} is already in use.' |
40 | | - .format(email)) |
41 | | - |
42 | | - |
43 | | -class UsernameValidator(RegexValidator): |
44 | | - """ Validates that a username uses only allowed characters """ |
45 | | - regex = r'^\w[\w.+-]*$' |
46 | | - message = 'Invalid Username' |
47 | | - code = 'invalid-username' |
48 | | - flags = re.ASCII |
49 | | - |
50 | | - |
51 | | -class RegistrationForm(forms.Form): |
52 | | - """ Form that is used to register a new user """ |
53 | | - username = forms.CharField(max_length=USERNAME_MAXLEN, |
54 | | - validators=[UsernameValidator()], |
55 | | - ) |
56 | | - email = forms.EmailField() |
57 | | - password1 = forms.CharField(widget=forms.PasswordInput()) |
58 | | - password2 = forms.CharField(widget=forms.PasswordInput()) |
59 | | - |
60 | | - def clean(self): |
61 | | - cleaned_data = super(RegistrationForm, self).clean() |
62 | | - password1 = cleaned_data.get('password1') |
63 | | - password2 = cleaned_data.get('password2') |
64 | | - |
65 | | - if not password1 or password1 != password2: |
66 | | - raise forms.ValidationError('Passwords do not match') |
67 | | - |
68 | | - |
69 | | -class RegistrationView(FormView): |
| 8 | +class RegistrationView(View): |
70 | 9 | """ View to register a new user """ |
71 | | - template_name = 'registration/registration_form.html' |
72 | | - form_class = RegistrationForm |
73 | | - success_url = reverse_lazy('registration-complete') |
74 | | - |
75 | | - def form_valid(self, form): |
76 | | - """ called whene the form was POSTed and its contents were valid """ |
77 | | - |
78 | | - try: |
79 | | - user = self.create_user(form) |
80 | | - |
81 | | - except ValidationError as e: |
82 | | - messages.error(self.request, '; '.join(e.messages)) |
83 | | - return HttpResponseRedirect(reverse('register')) |
84 | | - |
85 | | - except IntegrityError: |
86 | | - messages.error(self.request, |
87 | | - _('Username or email address already in use')) |
88 | | - return HttpResponseRedirect(reverse('register')) |
89 | | - |
90 | | - send_activation_email(user, self.request) |
91 | | - return super(RegistrationView, self).form_valid(form) |
92 | | - |
93 | | - @transaction.atomic |
94 | | - def create_user(self, form): |
95 | | - User = get_user_model() |
96 | | - user = User() |
97 | | - username = form.cleaned_data['username'] |
98 | | - |
99 | | - self._check_username(username) |
100 | | - user.username = username |
101 | | - |
102 | | - email_addr = form.cleaned_data['email'] |
103 | | - user.email = email_addr |
104 | | - |
105 | | - user.set_password(form.cleaned_data['password1']) |
106 | | - user.is_active = False |
107 | | - user.full_clean() |
108 | | - |
109 | | - try: |
110 | | - user.save() |
111 | | - |
112 | | - except IntegrityError as e: |
113 | | - if 'django_auth_unique_email' in str(e): |
114 | | - # this was not caught by the form validation, but now validates |
115 | | - # the DB's unique constraint |
116 | | - raise DuplicateEmail(email_addr) from e |
117 | | - else: |
118 | | - raise |
119 | | - |
120 | | - user.profile.activation_key = random_token() |
121 | | - user.profile.save() |
122 | | - |
123 | | - return user |
124 | | - |
125 | | - def _check_username(self, username): |
126 | | - """ Check if the username is already in use |
127 | | -
|
128 | | - Until there is a case-insensitive constraint on usernames, it is |
129 | | - necessary to check for existing usernames manually. This is not a |
130 | | - perfect solution, but the chance that two people sign up with the same |
131 | | - username at the same time is low enough. """ |
132 | | - UserModel = get_user_model() |
133 | | - users = UserModel.objects.filter(username__iexact=username) |
134 | | - if users.exists(): |
135 | | - raise DuplicateUsername(username) |
136 | | - |
137 | | - |
138 | | -class ActivationView(TemplateView): |
139 | | - """ Activates an already registered user """ |
140 | | - |
141 | | - template_name = 'registration/activation_failed.html' |
142 | | - |
143 | | - def get(self, request, activation_key): |
144 | | - User = get_user_model() |
145 | | - |
146 | | - try: |
147 | | - user = UserProxy.objects.get( |
148 | | - profile__activation_key=activation_key, |
149 | | - is_active=False, |
150 | | - ) |
151 | | - except UserProxy.DoesNotExist: |
152 | | - messages.error(request, _('The activation link is either not ' |
153 | | - 'valid or has already expired.')) |
154 | | - return super(ActivationView, self).get(request, activation_key) |
155 | | - |
156 | | - user.activate() |
157 | | - messages.success(request, _('Your user has been activated. ' |
158 | | - 'You can log in now.')) |
159 | | - return HttpResponseRedirect(reverse('login')) |
160 | | - |
161 | | - |
162 | | -class ResendActivationForm(forms.Form): |
163 | | - """ Form for resending the activation email """ |
164 | | - |
165 | | - username = forms.CharField(max_length=USERNAME_MAXLEN, required=False) |
166 | | - email = forms.EmailField(required=False) |
167 | | - |
168 | | - def clean(self): |
169 | | - cleaned_data = super(ResendActivationForm, self).clean() |
170 | | - username = cleaned_data.get('username') |
171 | | - email = cleaned_data.get('email') |
172 | | - |
173 | | - if not username and not email: |
174 | | - raise forms.ValidationError(_('Either username or email address ' |
175 | | - 'are required.')) |
176 | | - |
177 | | - |
178 | | -class ResendActivationView(FormView): |
179 | | - """ View to resend the activation email """ |
180 | | - template_name = 'registration/resend_activation.html' |
181 | | - form_class = ResendActivationForm |
182 | | - success_url = reverse_lazy('resent-activation') |
183 | | - |
184 | | - def form_valid(self, form): |
185 | | - """ called whene the form was POSTed and its contents were valid """ |
186 | | - |
187 | | - try: |
188 | | - user = UserProxy.objects.all().by_username_or_email( |
189 | | - form.cleaned_data['username'], |
190 | | - form.cleaned_data['email'], |
191 | | - ) |
192 | | - |
193 | | - except UserProxy.DoesNotExist: |
194 | | - messages.error(self.request, _('User does not exist.')) |
195 | | - return HttpResponseRedirect(reverse('resend-activation')) |
196 | | - |
197 | | - if user.profile.activation_key is None: |
198 | | - messages.success(self.request, _('Your account already has been ' |
199 | | - 'activated. Go ahead and log in.')) |
200 | | - |
201 | | - send_activation_email(user, self.request) |
202 | | - return super(ResendActivationView, self).form_valid(form) |
203 | | - |
204 | | - |
205 | | -class ResentActivationView(TemplateView): |
206 | | - template_name = 'registration/resent_activation.html' |
207 | | - |
208 | | - |
209 | | -def send_activation_email(user, request): |
210 | | - """ Sends the activation email for the given user """ |
211 | | - |
212 | | - subj = render_to_string('registration/activation_email_subject.txt') |
213 | | - # remove trailing newline added by render_to_string |
214 | | - subj = subj.strip() |
215 | 10 |
|
216 | | - msg = render_to_string('registration/activation_email.txt', { |
217 | | - 'site': RequestSite(request), |
218 | | - 'activation_key': user.profile.activation_key, |
219 | | - }) |
220 | | - user.email_user(subj, msg) |
| 11 | + def get(self, request): |
| 12 | + url = settings.MYGPO_AUTH_REGISTER_URL |
| 13 | + return HttpResponseRedirect(url) |
0 commit comments