backoffice: support next url after user creation (#26652)

This commit is contained in:
Paul Marillonnet 2018-10-31 12:00:30 +01:00
parent d8c6ba0976
commit ceb683ee3c
4 changed files with 52 additions and 1 deletions

View File

@ -91,5 +91,7 @@
}
})
</script>
{% block hidden_inputs %}
{% endblock %}
</form>
{% endblock %}

View File

@ -5,6 +5,11 @@
{% trans "Add an user" %}
{% endblock %}
{% block hidden_inputs %}
{{ block.super }}
{% if next_url %}<input type="hidden" name="next_url" value="{{ next_url }}">{% endif %}
{% endblock %}
{% block breadcrumb %}
{{ block.super }}
<a href="{% url 'a2-manager-users' %}{% if multiple_ou and ou %}?search-ou={{ ou.pk }}{% endif %}">{% trans 'Users' %}{% if multiple_ou and ou %}&nbsp;: {{ ou }}{% endif %}</a>

View File

@ -129,12 +129,15 @@ class UserAddView(BaseAddView):
return fields
def get_success_url(self):
return reverse('a2-manager-user-detail', kwargs={'pk': self.object.pk})
return self.request.POST.get('next_url') or \
reverse('a2-manager-user-detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(UserAddView, self).get_context_data(**kwargs)
context['cancel_url'] = '../..'
context['ou'] = self.ou
if hasattr(self.request, 'GET') and 'next_url' in self.request.GET:
context['next_url'] = self.request.GET['next_url']
return context
def form_valid(self, form):

View File

@ -692,3 +692,44 @@ def test_return_on_logout(superuser, app):
manager_home_page = login(app, superuser, reverse('a2-manager-homepage'))
response = manager_home_page.click('Logout').maybe_follow()
assert response.request.query_string == 'next=/manage/'
def test_manager_create_user_next_url(superuser_or_admin, app, ou1):
next_url = u'https://example.nowhere.null/'
url = u'/manage/users/%s/add/?next_url=%s' % (ou1.pk, next_url)
login(app, superuser_or_admin, '/manage/')
response = app.get(url)
form = response.form
form.set('first_name', 'John')
form.set('last_name', 'Doe')
form.set('email', 'john.doe@gmail.com')
form.set('password1', 'ABcd1234')
form.set('password2', 'ABcd1234')
assert form.submit().location == next_url
def test_manager_create_user_next_url_form_cancelation(superuser_or_admin, app, ou1):
next_url = u'https://example.nowhere.null/'
url = u'/manage/users/%s/add/?next_url=%s' % (ou1.pk, next_url)
login(app, superuser_or_admin, '/manage/')
response = app.get(url)
form = response.form
form.set('first_name', 'John')
form.set('last_name', 'Doe')
form.set('email', 'john.doe@gmail.com')
form.set('password1', 'ABcd1234')
form.set('password2', 'ABcd1234')
assert form.submit('cancel').location == next_url
def test_manager_create_user_next_url_form_error(superuser_or_admin, app, ou1):
next_url = u'https://example.nowhere.null/'
url = u'/manage/users/%s/add/?next_url=%s' % (ou1.pk, next_url)
login(app, superuser_or_admin, '/manage/')
response = app.get(url)
form = response.form
form.set('first_name', 'John')
form.set('last_name', 'Doe')
form.set('email', 'jd') # erroneous
form.set('password1', 'notvalid') # erroneous
assert '<input type="hidden" name="next_url" value="%s">' % next_url in form.submit().body