Merge pull request #420 from nephila/hotfix/author_view

Return 404 in AuthorEntriesView if author does not exists
This commit is contained in:
Iacopo Spalletti 2018-01-19 10:39:32 +01:00 committed by GitHub
commit 5ba20ed133
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View file

@ -9,6 +9,7 @@ from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import Http404 from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import get_language from django.utils.translation import get_language
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
@ -166,7 +167,10 @@ class AuthorEntriesView(BaseBlogListView, ListView):
return self.optimize(qs) return self.optimize(qs)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs['author'] = User.objects.get(**{User.USERNAME_FIELD: self.kwargs.get('username')}) kwargs['author'] = get_object_or_404(
User,
**{User.USERNAME_FIELD: self.kwargs.get('username')}
)
context = super(AuthorEntriesView, self).get_context_data(**kwargs) context = super(AuthorEntriesView, self).get_context_data(**kwargs)
return context return context

View file

@ -494,4 +494,15 @@ class ViewTest(BaseTest):
view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.namespace, view_obj.config = get_app_instance(request)
with self.assertRaises(Http404): with self.assertRaises(Http404):
view_obj.kwargs = {'category': 'unknown-category'} view_obj.kwargs = {'category': 'unknown-category'}
category_obj = view_obj.get_queryset() view_obj.get_queryset()
def test_non_existing_author_should_raise_404(self):
pages = self.get_pages()
with smart_override('en'):
request = self.get_request(pages[1], 'en', AnonymousUser())
view_obj = AuthorEntriesView()
view_obj.request = request
view_obj.namespace, view_obj.config = get_app_instance(request)
with self.assertRaises(Http404):
view_obj.kwargs = {'username': 'unknown-author'}
view_obj.get_context_data()