From c4836628858077304fff0967dc9b01ddd0e152c5 Mon Sep 17 00:00:00 2001 From: Felipe Farias Date: Sun, 15 Oct 2017 20:03:52 -0300 Subject: [PATCH 1/4] Not found author now returns a 404 error. --- djangocms_blog/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index c72afe6..ed77e45 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -16,6 +16,7 @@ from parler.views import TranslatableSlugMixin, ViewUrlMixin from .models import BlogCategory, Post from .settings import get_setting +from django.shortcuts import get_object_or_404 User = get_user_model() @@ -166,7 +167,7 @@ class AuthorEntriesView(BaseBlogListView, ListView): return self.optimize(qs) 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) return context From 6205c62709d6565106e76b31d64bcc06c4514806 Mon Sep 17 00:00:00 2001 From: Felipe Farias Date: Sun, 15 Oct 2017 20:17:00 -0300 Subject: [PATCH 2/4] fixing E501 flake8 error --- djangocms_blog/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index ed77e45..864d509 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -167,7 +167,8 @@ class AuthorEntriesView(BaseBlogListView, ListView): return self.optimize(qs) def get_context_data(self, **kwargs): - kwargs['author'] = get_object_or_404(User, **{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) return context From f5b37806001d6fc7076a8ea678b3499055f9ffef Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Thu, 18 Jan 2018 23:00:00 +0100 Subject: [PATCH 3/4] Cleanup code style --- djangocms_blog/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index 864d509..f2ee5eb 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -9,6 +9,7 @@ from django.contrib.auth import get_user_model from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse from django.http import Http404 +from django.shortcuts import get_object_or_404 from django.utils.timezone import now from django.utils.translation import get_language from django.views.generic import DetailView, ListView @@ -16,7 +17,6 @@ from parler.views import TranslatableSlugMixin, ViewUrlMixin from .models import BlogCategory, Post from .settings import get_setting -from django.shortcuts import get_object_or_404 User = get_user_model() @@ -167,8 +167,10 @@ class AuthorEntriesView(BaseBlogListView, ListView): return self.optimize(qs) def get_context_data(self, **kwargs): - kwargs['author'] = get_object_or_404(User, - **{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) return context From 21c0b3dbe949526c7d8253180130418351145901 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Thu, 18 Jan 2018 23:00:06 +0100 Subject: [PATCH 4/4] Add test --- tests/test_views.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/test_views.py b/tests/test_views.py index d6d6d2a..3f94b98 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -494,4 +494,15 @@ class ViewTest(BaseTest): view_obj.namespace, view_obj.config = get_app_instance(request) with self.assertRaises(Http404): 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()