Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Also preserve BaseContentDetailView params on login redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
mparent61 committed Aug 3, 2016
1 parent d53e30c commit 249d4cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bulbs/content/views.py
Expand Up @@ -181,7 +181,8 @@ def get(self, request, *args, **kwargs):
if not request.user.is_staff and not allow_anonymous:
response = redirect_unpublished_to_login_or_404(
request=request,
next_url=self.object.get_absolute_url())
next_url=self.object.get_absolute_url(),
next_params=request.GET)

# Never cache unpublished articles
add_never_cache_headers(response)
Expand Down
4 changes: 3 additions & 1 deletion bulbs/utils/methods.py
Expand Up @@ -110,9 +110,11 @@ def get_video_object_from_videohub_id(videohub_id):
return get_object_or_404(video_model, videohub_ref_id=int(videohub_id))


def redirect_unpublished_to_login_or_404(request, next_url):
def redirect_unpublished_to_login_or_404(request, next_url, next_params=None):
redirect_unpublished = getattr(settings, "REDIRECT_UNPUBLISHED_TO_LOGIN", True)
if not request.user.is_authenticated() and redirect_unpublished:
if next_params:
next_url += '?' + urlencode(next_params)
return HttpResponseRedirect("{}?{}".format(settings.LOGIN_URL,
urlencode({'next': next_url})))
else:
Expand Down
12 changes: 12 additions & 0 deletions tests/content/test_content_views.py
Expand Up @@ -27,12 +27,24 @@ def test_unpublished_article(self):
content = TestContentObj.objects.create(title="Testing Content")
response = self.client.get(reverse("published", kwargs={"pk": content.id}))
self.assertEqual(response.status_code, 302)
self.assertEqual(
response["Location"],
"http://testserver/accounts/login/?next=%2Fdetail%2F{}%2F".format(content.id))

# But this should work when we login
self.client.login(username="admin", password="secret")
response = self.client.get(reverse("published", kwargs={"pk": content.id}))
self.assertEqual(response.status_code, 200)

def test_login_redirect_preserves_query_params(self):
content = TestContentObj.objects.create(title="Testing Content")
response = self.client.get(reverse("published", kwargs={"pk": content.id}), {'extra': True})
self.assertEqual(response.status_code, 302)
self.assertEqual(
response["Location"],
"http://testserver/accounts/login/?next=%2Fdetail%2F{}%2F%3Fextra%3DTrue".format(
content.id))

def published_article(self):
content = make_content(published=timezone.now() - timedelta(hours=2))
response = self.client.get(reverse("published", kwargs={"pk": content.id}))
Expand Down

0 comments on commit 249d4cc

Please sign in to comment.