Skip to content

Commit

Permalink
Corrige la redirection au login
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Jul 19, 2022
1 parent 97ef875 commit 67e2408
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 4 additions & 4 deletions zds/member/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ class LoginForm(AuthenticationForm):
"banned": _("Vous n’êtes pas autorisé à vous connecter sur le site, vous avez été banni par un modérateur."),
}

def __init__(self, request=None, *args, **kwargs):
def __init__(self, request=None, next="", *args, **kwargs):
super().__init__(request, *args, **kwargs)
self.helper = self.get_helper()
self.helper = self.get_helper(next)
# Errors are displayed using info bars (see LoginView) instead of the form built-in error rendering
self.helper.form_show_errors = False

def get_helper(self):
def get_helper(self, next):
"""Return the FormHelper expected by crispy."""
helper = FormHelper()
helper.form_action = reverse("member-login")
helper.form_action = reverse("member-login") + f"?next={next}"
helper.form_method = "post"
helper.form_class = "content-wrapper"
helper.layout = Layout(
Expand Down
9 changes: 8 additions & 1 deletion zds/member/tests/views/tests_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def setUp(self):
self.assertNotEqual(self.test_ip, ProfileFactory.last_ip_address)
settings.SESSION_COOKIE_AGE = 1337

def form_action_redirect(self):
"""The form shall have the 'next' parameter in the action url of the form."""
next_fragment = ("?next=" + reverse("member-detail", args=[self.correct_username]),)
full_url = self.login_url + next_fragment
result = self.client.get(full_url, follow=False)
self.assertContains(result, next_fragment)

def test_nominal_and_remember_me(self):
"""
Nominal case: existing username, correct password, activated user, 'remember me' checked.
Expand Down Expand Up @@ -69,7 +76,7 @@ def test_nonascii(self):
Expected: successful login and redirect to profile.
"""
user = NonAsciiProfileFactory()
result = self.client.get(
result = self.client.post(
self.login_url + "?next=" + reverse("member-detail", args=[user.user.username]),
follow=False,
)
Expand Down
5 changes: 5 additions & 0 deletions zds/member/views/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def form_valid(self, form):
form.user_cache.profile.save()
return super().form_valid(form)

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["next"] = self.get_success_url()
return kwargs

def get_success_url(self):
"""In case of success, redirect to homepage for some special 'next' targets or non-existing pages."""
url = self.get_redirect_url()
Expand Down

0 comments on commit 67e2408

Please sign in to comment.