Commit f366f7bc authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

transmitting next url parameter all along the user creation process, including with activation link

parent 33188c4a
Pipeline #48327 failed with stage
in 1 minute and 35 seconds
from django import forms
from django.contrib.auth import get_user_model, forms as auth_forms
from django.db.models import Q
from django.forms import ModelForm
from django.forms import widgets
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
......@@ -38,6 +39,8 @@ class UserCreationFormWithMore(CleanUsernameAndSuggestReset, auth_forms.UserCrea
fields = ("username", "email", "first_name", "last_name")
field_classes = {'username': auth_forms.UsernameField}
next = forms.CharField(widget=widgets.HiddenInput(), required=False)
def __init__(self, *args, **kwargs):
super(UserCreationFormWithMore, self).__init__(*args, **kwargs)
self.fields['email'].required = True
......@@ -79,7 +82,7 @@ class MyUserChangeForm(CleanUsernameAndSuggestReset, auth_forms.UserChangeForm):
return user
class UserDeleteForm(ModelForm):
class UserDeleteForm(forms.ModelForm):
class Meta:
model = get_user_model()
fields = ()
......
......@@ -15,7 +15,7 @@
</form>
<br/>
<hr/>
<a href="{% url 'basetheme_bootstrap:signup' %}" role="button" class="btn btn-default btn-outline-primary full-width">{%trans "Create account"%}</a>
<a href="{% url 'basetheme_bootstrap:signup' %}{%if next%}?next={{next}}{%endif%}" role="button" class="btn btn-default btn-outline-primary full-width">{%trans "Create account"%}</a>
<a href="{% url 'basetheme_bootstrap:password_reset' %}" role="button" class="btn btn-default btn-outline-primary full-width pull-right float-right">{%trans "Reset my password"%}</a>
</div>
{% endblock %}
\ No newline at end of file
......@@ -66,6 +66,7 @@ def signup(request):
form = UserCreationFormWithMore(request.POST)
if form.is_valid():
auto_active = not is_validating_email()
next_page = form.cleaned_data["next"]
user = form.save()
if get_user_model().objects.filter(pk__gt=1).count() == 0:
user.is_superuser = True
......@@ -79,7 +80,7 @@ def signup(request):
user.is_active = auto_active
user.save()
send_account_created(request, user, auto_active=auto_active)
send_account_created(request, user, auto_active=auto_active, next_page=next_page)
if not auto_active and not user.is_superuser:
return account_is_pending_view(request, email=request.POST['email'])
......@@ -90,7 +91,7 @@ def signup(request):
request.user = user
login(request, user)
return redirect('home')
return redirect(next_page if next_page else 'home')
else:
user = get_user_model().objects.filter(groups__name=__PENDING_ACCOUNT_USER,
email=request.POST['email']).first()
......@@ -100,7 +101,11 @@ def signup(request):
else:
if not request.user.is_anonymous:
return HttpResponseForbidden()
form = UserCreationFormWithMore()
try:
initial = dict(next=request.GET["next"])
except KeyError:
initial = None
form = UserCreationFormWithMore(initial=initial)
return render(request, 'registration/signup.html', {'form': form})
......@@ -113,13 +118,15 @@ def account_is_pending_view(request, email):
})
def send_account_created(request, user, auto_active=False):
def send_account_created(request, user, auto_active=False, next_page=None):
try:
activation_link = request.scheme + "://" + request.get_host()
activation_link += reverse('basetheme_bootstrap:activate', kwargs={
'uidb64': urlsafe_base64_encode(force_bytes(user.pk)),
'token': tokens.account_activation_token.make_token(user)
})
if next_page:
activation_link += f"?next={next_page}"
if auto_active:
message = ugettext(
'Dear %(first_name)s %(last_name)s\n\n'
......@@ -250,12 +257,17 @@ def activate(request, uidb64, token):
user.groups.remove(user.groups.get(name=__PENDING_ACCOUNT_USER))
user.save()
login(request, user)
message = ugettext('Thank you for your email confirmation, you account have been activated '
'and you are now logged in.')
next_page = request.GET.get("next", '')
if next_page:
messages.success(request, message)
return redirect(next_page)
# return redirect('home')
# return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
return render(request, 'basetheme_bootstrap/simple_message_page.html', {
'page_title': ugettext('Account activated'),
'message': ugettext('Thank you for your email confirmation, you account have been activated '
'and you are now logged in.'),
'message': message,
})
else:
send_account_created(request, user)
......
......@@ -7,7 +7,7 @@ readme = open('README.rst').read()
setup(
name='django-basetheme-bootstrap',
version='0.2.53',
version='0.2.54',
description='Django Basetheme Bootstrap',
long_description=readme,
author='Bryan Brancotte',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment