Skip to content

Commit b8c81c2

Browse files
committed
WIP
1 parent 518a46d commit b8c81c2

5 files changed

Lines changed: 26 additions & 234 deletions

File tree

mygpo/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,5 @@ def get_intOrNone(name, default):
385385

386386
MYGPO_AUTH_URL = os.getenv('MYGPO_AUTH_URL', None)
387387

388+
MYGPO_AUTH_REGISTER_URL = os.getenv('MYGPO_AUTH_REGISTER_URL', None)
389+

mygpo/users/checks.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.core.checks import register, Warning
22
from django.db import connection
33
from django.db.utils import OperationalError
4+
from django.conf import settings
45

56

67
SQL = """
@@ -38,3 +39,15 @@ def check_case_insensitive_users(app_configs=None, **kwargs):
3839
raise
3940

4041
return errors
42+
43+
44+
@register()
45+
def check_registration_url(app_configs=None, **kwargs):
46+
errors = []
47+
48+
if not settings.MYGPO_AUTH_REGISTER_URL:
49+
txt = 'The setting MYGPO_AUTH_REGISTER_URL is not set.'
50+
wid = 'users.W002'
51+
errors.append(Warning(txt, id=wid))
52+
53+
return errors

mygpo/users/urls.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@
1111
registration.RegistrationView.as_view(),
1212
name='register'),
1313

14-
url(r'^registration_complete/$',
15-
registration.TemplateView.as_view(
16-
template_name='registration/registration_complete.html'),
17-
name='registration-complete'),
18-
19-
url(r'^activate/(?P<activation_key>\w+)$',
20-
registration.ActivationView.as_view()),
21-
22-
url(r'^registration/resend$',
23-
registration.ResendActivationView.as_view(),
24-
name='resend-activation'),
25-
26-
url(r'^registration/resent$',
27-
registration.ResentActivationView.as_view(),
28-
name='resent-activation'),
29-
3014
url(r'^account/$',
3115
settings.account,
3216
name='account'),

mygpo/users/views/registration.py

Lines changed: 5 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -1,220 +1,13 @@
11
import re
22

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
73
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
134
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
176

18-
from mygpo.utils import random_token
19-
from mygpo.users.models import UserProxy
207

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):
709
""" 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()
21510

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)

mygpo/users/views/user.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from mygpo.constants import DEFAULT_LOGIN_REDIRECT
2727
from mygpo.web.auth import get_google_oauth_flow
2828
from mygpo.users.models import UserProxy
29-
from mygpo.users.views.registration import send_activation_email
3029
from mygpo.utils import random_token
3130

3231
import logging
@@ -83,11 +82,11 @@ def post(self, request):
8382
messages.error(request, _('Wrong username or password.'))
8483
return HttpResponseRedirect(login_page)
8584

86-
8785
if not user.is_active:
88-
send_activation_email(user, request)
86+
# send_activation_email(user, request)
8987
messages.error(request, _('Please activate your account first. '
90-
'We have just re-sent your activation email'))
88+
'We have just re-sent your activation '
89+
'email'))
9190
return HttpResponseRedirect(login_page)
9291

9392
# set up the user's session
@@ -130,9 +129,10 @@ def restore_password(request):
130129
return render(request, 'password_reset_failed.html')
131130

132131
if not user.is_active:
133-
send_activation_email(user, request)
132+
# send_activation_email(user, request)
134133
messages.error(request, _('Please activate your account first. '
135-
'We have just re-sent your activation email'))
134+
'We have just re-sent your activation '
135+
'email'))
136136
return HttpResponseRedirect(reverse('login'))
137137

138138
site = RequestSite(request)

0 commit comments

Comments
 (0)