Add edit configuration link to toolbar

This commit is contained in:
Iacopo Spalletti 2015-09-30 09:34:11 +02:00
parent 2fd5bca07c
commit a703b188fa
4 changed files with 42 additions and 12 deletions

View file

@ -4,9 +4,9 @@ from __future__ import absolute_import, print_function, unicode_literals
from cms.toolbar_base import CMSToolbar from cms.toolbar_base import CMSToolbar
from cms.toolbar_pool import toolbar_pool from cms.toolbar_pool import toolbar_pool
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _, override
from .models import BLOG_CURRENT_POST_IDENTIFIER from .models import BLOG_CURRENT_NAMESPACE, BLOG_CURRENT_POST_IDENTIFIER
@toolbar_pool.register @toolbar_pool.register
@ -17,16 +17,21 @@ class BlogToolbar(CMSToolbar):
if not self.request.user.has_perm('djangocms_blog.add_post'): if not self.request.user.has_perm('djangocms_blog.add_post'):
return # pragma: no cover return # pragma: no cover
admin_menu = self.toolbar.get_or_create_menu('djangocms_blog', _('Blog')) admin_menu = self.toolbar.get_or_create_menu('djangocms_blog', _('Blog'))
url = reverse('admin:djangocms_blog_post_changelist') with override(self.current_lang):
admin_menu.add_modal_item(_('Post list'), url=url) url = reverse('admin:djangocms_blog_post_changelist')
url = reverse('admin:djangocms_blog_post_add') admin_menu.add_modal_item(_('Post list'), url=url)
admin_menu.add_modal_item(_('Add post'), url=url) url = reverse('admin:djangocms_blog_post_add')
admin_menu.add_modal_item(_('Add post'), url=url)
current_config = getattr(self.request, BLOG_CURRENT_NAMESPACE, None)
if current_config:
url = reverse('admin:djangocms_blog_blogconfig_change', args=(current_config.pk,))
admin_menu.add_modal_item(_('Edit configuration'), url=url)
current_post = getattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, None) current_post = getattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, None)
if current_post and self.request.user.has_perm('djangocms_blog.change_post'): # pragma: no cover # NOQA if current_post and self.request.user.has_perm('djangocms_blog.change_post'): # pragma: no cover # NOQA
admin_menu.add_modal_item(_('Edit Post'), reverse( admin_menu.add_modal_item(_('Edit Post'), reverse(
'admin:djangocms_blog_post_change', args=(current_post.pk,)), 'admin:djangocms_blog_post_change', args=(current_post.pk,)),
active=True) active=True)
def post_template_populate(self): def post_template_populate(self):
current_post = getattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, None) current_post = getattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, None)

View file

@ -23,6 +23,7 @@ from .managers import GenericDateTaggedManager
from .settings import get_setting from .settings import get_setting
BLOG_CURRENT_POST_IDENTIFIER = 'djangocms_post_current' BLOG_CURRENT_POST_IDENTIFIER = 'djangocms_post_current'
BLOG_CURRENT_NAMESPACE = 'djangocms_post_current_config'
@python_2_unicode_compatible @python_2_unicode_compatible

View file

@ -12,7 +12,7 @@ from django.utils.translation import get_language
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
from parler.views import TranslatableSlugMixin, ViewUrlMixin from parler.views import TranslatableSlugMixin, ViewUrlMixin
from .models import BLOG_CURRENT_POST_IDENTIFIER, BlogCategory, Post from .models import BLOG_CURRENT_NAMESPACE, BLOG_CURRENT_POST_IDENTIFIER, BlogCategory, Post
from .settings import get_setting from .settings import get_setting
User = get_user_model() User = get_user_model()
@ -42,6 +42,7 @@ class BaseBlogView(AppConfigMixin, ViewUrlMixin):
) )
if not getattr(self.request, 'toolbar', False) or not self.request.toolbar.edit_mode: if not getattr(self.request, 'toolbar', False) or not self.request.toolbar.edit_mode:
queryset = queryset.published() queryset = queryset.published()
setattr(self.request, BLOG_CURRENT_NAMESPACE, self.config)
return queryset return queryset
def get_template_names(self): def get_template_names(self):

View file

@ -4,15 +4,19 @@ from __future__ import absolute_import, print_function, unicode_literals
import os.path import os.path
from aldryn_apphooks_config.utils import get_app_instance from aldryn_apphooks_config.utils import get_app_instance
from cms.toolbar.items import ModalItem
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.http import Http404 from django.http import Http404
from django.utils.timezone import now from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from parler.tests.utils import override_parler_settings from parler.tests.utils import override_parler_settings
from parler.utils.conf import add_default_language_settings from parler.utils.conf import add_default_language_settings
from parler.utils.context import smart_override, switch_language from parler.utils.context import smart_override, switch_language
from djangocms_blog.feeds import LatestEntriesFeed, TagFeed from djangocms_blog.feeds import LatestEntriesFeed, TagFeed
from djangocms_blog.models import BLOG_CURRENT_NAMESPACE
from djangocms_blog.sitemaps import BlogSitemap from djangocms_blog.sitemaps import BlogSitemap
from djangocms_blog.views import ( from djangocms_blog.views import (
AuthorEntriesView, CategoryEntriesView, PostArchiveView, PostDetailView, PostListView, AuthorEntriesView, CategoryEntriesView, PostArchiveView, PostDetailView, PostListView,
@ -34,8 +38,10 @@ class ViewTest(BaseTest):
view_obj = PostListView() view_obj = PostListView()
view_obj.request = request view_obj.request = request
view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.namespace, view_obj.config = get_app_instance(request)
self.assertEqual(getattr(request, BLOG_CURRENT_NAMESPACE, None), None)
self.assertEqual(list(view_obj.get_queryset()), [posts[0]]) self.assertEqual(list(view_obj.get_queryset()), [posts[0]])
self.assertEqual(getattr(request, BLOG_CURRENT_NAMESPACE), self.app_config_1)
request = self.get_page_request(pages[1], self.user, lang='en', edit=False) request = self.get_page_request(pages[1], self.user, lang='en', edit=False)
view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.namespace, view_obj.config = get_app_instance(request)
@ -60,16 +66,33 @@ class ViewTest(BaseTest):
self.assertEqual(context['post_list'][0].title, 'Third post') self.assertEqual(context['post_list'][0].title, 'Third post')
response = view_obj.render_to_response(context) response = view_obj.render_to_response(context)
self.assertContains(response, context['post_list'][0].get_absolute_url()) self.assertContains(response, context['post_list'][0].get_absolute_url())
self.assertEqual(getattr(request, BLOG_CURRENT_NAMESPACE), self.app_config_1)
with smart_override('it'): with smart_override('it'):
request = self.get_page_request(pages[1], self.user, lang='it', edit=True) request = self.get_page_request(pages[1], self.user, lang='it', edit=True)
view_obj = PostListView()
view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.namespace, view_obj.config = get_app_instance(request)
view_obj.request = request view_obj.request = request
view_obj.kwargs = {}
view_obj.object_list = view_obj.get_queryset() view_obj.object_list = view_obj.get_queryset()
context = view_obj.get_context_data(object_list=view_obj.object_list) context = view_obj.get_context_data(object_list=view_obj.object_list)
self.assertEqual(context['post_list'][0].title, 'Terzo post') self.assertEqual(context['post_list'][0].title, 'Terzo post')
response = view_obj.render_to_response(context) response = view_obj.render_to_response(context)
self.assertContains(response, context['post_list'][0].get_absolute_url()) self.assertContains(response, context['post_list'][0].get_absolute_url())
blog_menu = request.toolbar.get_or_create_menu('djangocms_blog', _('Blog'))
self.assertEqual(len(blog_menu.items), 3)
self.assertEqual(len(blog_menu.find_items(
ModalItem, url=reverse('admin:djangocms_blog_post_changelist')
)), 1)
self.assertEqual(len(blog_menu.find_items(
ModalItem, url=reverse('admin:djangocms_blog_post_add')
)), 1)
self.assertEqual(len(blog_menu.find_items(
ModalItem, url=reverse(
'admin:djangocms_blog_blogconfig_change', args=(self.app_config_1.pk,)
)
)), 1)
def test_get_view_url(self): def test_get_view_url(self):
posts = self.get_posts() posts = self.get_posts()