def test_publish_button(self): page = create_page("test", "nav_playground.html", "en", published=True) request = self.get_page_request(page, self.get_superuser(), edit=True) toolbar = CMSToolbar(request) self.assertTrue(toolbar.edit_mode) items = toolbar.get_left_items() + toolbar.get_right_items() self.assertEqual(len(items), 7)
def test_anon(self): page = create_page("test", "nav_playground.html", "en") request = self.get_page_request(page, self.get_anon()) toolbar = CMSToolbar(request) items = toolbar.get_left_items() + toolbar.get_right_items() self.assertEqual(len(items), 0)
def test_toolbar_with_an_existing_usersetting(self): """ Test that UserSettings toolbar item URL is for change_view for superuser if UserSettings already exists for current site """ usersettings_model_opts = self.UserSettings._meta usersettings_obj = self.UserSettings.objects.create(**self.usersettings_data) from cms.toolbar.toolbar import CMSToolbar page = self.get_pages() request = self.get_page_request(page, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() admin_menu = toolbar.menus[ADMIN_MENU_IDENTIFIER] MENU_ITEM_TITLE = usersettings_model_opts.verbose_name change_url = '%s?%s' % ( reverse('admin:%s_%s_change' % ( usersettings_model_opts.app_label, usersettings_model_opts.module_name), args=(usersettings_obj.pk,)), IS_POPUP_VAR) usersettings_menu = \ admin_menu.find_items(ModalItem, name='%s ...' % force_unicode(MENU_ITEM_TITLE)) self.assertEqual(change_url, usersettings_menu[0].item.url)
def test_toolbar_with_items(self): """ Test that PageMeta/TitleMeta items are present for superuser if PageMeta/TitleMeta exists for current page """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() page_ext = PageMeta.objects.create(extended_object=page1) request = self.get_page_request(page1, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] meta_menu = page_menu.find_items(SubMenu, name=force_text(PAGE_META_MENU_TITLE))[0].item try: pagemeta_menu = meta_menu.find_items(ModalItem, name="{0}...".format(force_text(PAGE_META_ITEM_TITLE))) self.assertEqual(len(pagemeta_menu), 1) except AssertionError: pagemeta_menu = meta_menu.find_items(ModalItem, name="{0} ...".format(force_text(PAGE_META_ITEM_TITLE))) self.assertEqual(len(pagemeta_menu), 1) self.assertTrue(pagemeta_menu[0].item.url.startswith(reverse('admin:djangocms_page_meta_pagemeta_change', args=(page_ext.pk,)))) for title in page1.title_set.all(): language = get_language_object(title.language) try: titlemeta_menu = meta_menu.find_items(ModalItem, name='{0}...'.format(language['name'])) self.assertEqual(len(titlemeta_menu), 1) except AssertionError: titlemeta_menu = meta_menu.find_items(ModalItem, name='{0} ...'.format(language['name'])) self.assertEqual(len(titlemeta_menu), 1) try: title_ext = TitleMeta.objects.get(extended_object_id=title.pk) self.assertTrue(titlemeta_menu[0].item.url.startswith(reverse('admin:djangocms_page_meta_titlemeta_change', args=(title_ext.pk,)))) except TitleMeta.DoesNotExist: self.assertTrue(titlemeta_menu[0].item.url.startswith(reverse('admin:djangocms_page_meta_titlemeta_add')))
def test_toolbar_with_items(self): """ Test that PageTags/TitleTags items are present for superuser if PageTags/TitleTags exists for current page """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() page_ext = PageTags.objects.create(extended_object=page1) title_tags = TitleTags.objects.create(extended_object=page1.get_title_obj('en')) request = self.get_page_request(page1, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page')[0].item tags_menu = page_menu.find_items(SubMenu, name=force_text(PAGE_TAGS_MENU_TITLE))[0].item pagetags_menu = tags_menu.find_items(ModalItem, name="{0}...".format(force_text(PAGE_TAGS_ITEM_TITLE))) self.assertEqual(len(pagetags_menu), 1) self.assertEqual(len(pagetags_menu), 1) self.assertTrue(pagetags_menu[0].item.url.startswith(reverse('admin:djangocms_page_tags_pagetags_change', args=(page_ext.pk,)))) url_change = False url_add = False for title in page1.title_set.all(): language = get_language_object(title.language) titletags_menu = tags_menu.find_items(ModalItem, name='{0}...'.format(language['name'])) self.assertEqual(len(titletags_menu), 1) try: title_ext = TitleTags.objects.get(extended_object_id=title.pk) self.assertEqual(title_ext, title_tags) self.assertTrue(titletags_menu[0].item.url.startswith(reverse('admin:djangocms_page_tags_titletags_change', args=(title_ext.pk,)))) url_change = True except TitleTags.DoesNotExist: self.assertTrue(titletags_menu[0].item.url.startswith(reverse('admin:djangocms_page_tags_titletags_add'))) url_add = True self.assertTrue(url_change and url_add)
def test_toolbar(self): """ Test that PageMeta/TitleMeta items are present for superuser """ from cms.toolbar.toolbar import CMSToolbar NEW_CMS_LANGS = { 1: [ { 'code': 'en', 'name': 'English', 'public': True, }, { 'code': 'it', 'name': 'Italiano', 'public': True, }, ], 'default': { 'hide_untranslated': False, }, } page1, page2 = self.get_pages() with self.settings(CMS_LANGUAGES=NEW_CMS_LANGS): request = self.get_page_request(page1, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] meta_menu = page_menu.find_items(SubMenu, name=force_text(PAGE_META_MENU_TITLE))[0].item try: self.assertEqual(len(meta_menu.find_items(ModalItem, name="{0}...".format(force_text(PAGE_META_ITEM_TITLE)))), 1) except AssertionError: self.assertEqual(len(meta_menu.find_items(ModalItem, name="{0} ...".format(force_text(PAGE_META_ITEM_TITLE)))), 1) self.assertEqual(len(meta_menu.find_items(ModalItem)), len(NEW_CMS_LANGS[1])+1)
def test_nonstaff(self): page = create_page("test", "nav_playground.html", "en", published=True) request = self.get_page_request(page, self.get_nonstaff()) toolbar = CMSToolbar(request) items = toolbar.get_left_items() + toolbar.get_right_items() # Logo + edit-mode + logout self.assertEqual(len(items), 0)
def test_template_change_permission(self): with SettingsOverride(CMS_PERMISSIONS=True): page = create_page("test", "nav_playground.html", "en", published=True) request = self.get_page_request(page, self.get_nonstaff()) toolbar = CMSToolbar(request) items = toolbar.get_left_items() + toolbar.get_right_items() self.assertEqual([item for item in items if item.css_class_suffix == "templates"], [])
def test_not_is_staff(self): request = RequestFactory().get("/en/?edit") request.session = {} request.LANGUAGE_CODE = "en" request.user = AnonymousUser() toolbar = CMSToolbar(request) self.assertEqual(len(toolbar.get_left_items()), 0) self.assertEqual(len(toolbar.get_right_items()), 0)
def test_no_page_superuser(self): request = self.get_page_request(None, self.get_superuser(), "/") toolbar = CMSToolbar(request) items = toolbar.get_left_items() + toolbar.get_right_items() # Logo + edit-mode + admin-menu + logout self.assertEqual(len(items), 3) admin_items = toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER, "Test").get_items() self.assertEqual(len(admin_items), 7, admin_items)
def test_no_publish_button(self): page = create_page("test", "nav_playground.html", "en", published=True) request = self.get_page_request(page, self.get_staff(), edit=True) toolbar = CMSToolbar(request) self.assertTrue(page.has_change_permission(request)) self.assertFalse(page.has_publish_permission(request)) self.assertTrue(toolbar.edit_mode) items = toolbar.get_left_items() + toolbar.get_right_items() # Logo + edit-mode + templates + page-menu + admin-menu + logout self.assertEqual(len(items), 6)
def test_toolbar_no_locale(self): page = create_page('test', 'nav_playground.html', 'en-us', published=True) sub = create_page('sub', 'nav_playground.html', 'en-us', published=True, parent=page) # loads the urlconf before reverse below sub.get_absolute_url('en-us') request = self.get_page_request(sub, self.get_superuser(), edit=True) del request.LANGUAGE_CODE toolbar = CMSToolbar(request) toolbar.set_object(sub) self.assertEqual(toolbar.get_object_public_url(), '/sub/')
def test_no_page(self): """ Test that no page menu is present if request not in a page """ from cms.toolbar.toolbar import CMSToolbar request = self.get_page_request(None, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page') self.assertEqual(page_menu, [])
def test_cache_placeholder(self): template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) placeholder = page1.placeholders.filter(slot="body")[0] add_plugin(placeholder, "TextPlugin", 'en', body="English") add_plugin(placeholder, "TextPlugin", 'de', body="Deutsch") request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(5, 9)): self.render_template_obj(template, {}, request) request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) request.toolbar.edit_mode = False template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" with self.assertNumQueries(1): self.render_template_obj(template, {}, request) # toolbar request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) request.toolbar.edit_mode = True request.toolbar.show_toolbar = True template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" with self.assertNumQueries(3): self.render_template_obj(template, {}, request) page1.publish('en') exclude = [ 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware' ] overrides = dict(CMS_PAGE_CACHE=False) if getattr(settings, 'MIDDLEWARE', None): overrides['MIDDLEWARE'] = [ mw for mw in settings.MIDDLEWARE if mw not in exclude ] else: overrides['MIDDLEWARE_CLASSES'] = [ mw for mw in settings.MIDDLEWARE_CLASSES if mw not in exclude ] with self.settings(**overrides): with self.assertNumQueries(FuzzyInt(13, 25)): self.client.get('/en/') with self.assertNumQueries(FuzzyInt(5, 11)): self.client.get('/en/') overrides['CMS_PLACEHOLDER_CACHE'] = False with self.settings(**overrides): with self.assertNumQueries(FuzzyInt(7, 15)): self.client.get('/en/')
def test_out_of_order(self): page = create_page("toolbar-page", "nav_playground.html", "en", published=True) request = self.get_page_request(page, self.get_staff(), '/') toolbar = CMSToolbar(request) menu1 = toolbar.get_or_create_menu("test") menu2 = toolbar.get_or_create_menu("test", "Test", side=toolbar.RIGHT, position=2) self.assertEqual(menu1, menu2) self.assertEqual(menu1.name, 'Test') self.assertEqual(len(toolbar.get_right_items()), 1)
def test_no_page(self): """ Test that no page menu is present if request not in a page """ from cms.toolbar.toolbar import CMSToolbar request = self.get_page_request(None, self.user, "/", edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name="Page") self.assertEqual(page_menu, [])
def test_toolbar(self): """ Test that PageSitemapProperties item is present for superuser """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() request = self.get_page_request(page1, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] self.assertEqual(len(page_menu.find_items(ModalItem, name="%s ..." % force_text(PAGE_SITEMAP_MENU_TITLE))), 1)
def test_no_perm(self): """ Test that no page menu is present if user has no perm """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page') self.assertEqual(page_menu, [])
def test_no_cache_plugin(self): page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) placeholder1 = page1.placeholders.filter(slot="body")[0] placeholder2 = page1.placeholders.filter(slot="right-column")[0] plugin_pool.register_plugin(NoCachePlugin) add_plugin(placeholder1, "TextPlugin", 'en', body="English") add_plugin(placeholder2, "TextPlugin", 'en', body="Deutsch") request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) template = Template("{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}") rctx = RequestContext(request) with self.assertNumQueries(3): template.render(rctx) request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) template = Template("{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}") rctx = RequestContext(request) with self.assertNumQueries(1): template.render(rctx) add_plugin(placeholder1, "NoCachePlugin", 'en') page1.publish('en') request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) template = Template("{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}") rctx = RequestContext(request) with self.assertNumQueries(4): render = template.render(rctx) with self.assertNumQueries(FuzzyInt(14, 18)): response = self.client.get('/en/') resp1 = response.content.decode('utf8').split("$$$")[1] request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) template = Template("{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}") rctx = RequestContext(request) with self.assertNumQueries(4): render2 = template.render(rctx) with self.settings(CMS_PAGE_CACHE=False): with self.assertNumQueries(FuzzyInt(8, 13)): response = self.client.get('/en/') resp2 = response.content.decode('utf8').split("$$$")[1] self.assertNotEqual(render, render2) self.assertNotEqual(resp1, resp2) plugin_pool.unregister_plugin(NoCachePlugin)
def test_toolbar(self): urls = 'cmsplugin_seocheck.tests.urls' request = self.get_request() toolbar = CMSToolbar(request) toolbar.get_left_items() seo_menu = toolbar.menus['seo_check'] self.assertEqual(len(seo_menu.items), 1) self.assertEqual( len( seo_menu.find_items( ModalItem, url=reverse('cmsplugin_seocheck:check_modal'))), 1)
def test_template_change_permission(self): with SettingsOverride(CMS_PERMISSIONS=True): page = create_page('test', 'nav_playground.html', 'en', published=True) request = self.get_page_request(page, self.get_nonstaff()) toolbar = CMSToolbar(request) items = toolbar.get_left_items() + toolbar.get_right_items() self.assertEqual([ item for item in items if item.css_class_suffix == 'templates' ], [])
def test_cache_placeholder(self): template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) page1_url = page1.get_absolute_url() placeholder = page1.placeholders.filter(slot="body")[0] add_plugin(placeholder, "TextPlugin", 'en', body="English") add_plugin(placeholder, "TextPlugin", 'de', body="Deutsch") request = self.get_request(page1_url) request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(5, 9)): self.render_template_obj(template, {}, request) request = self.get_request(page1_url) request.session['cms_edit'] = True request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" with self.assertNumQueries(2): self.render_template_obj(template, {}, request) # toolbar with self.login_user_context(self.get_superuser()): request = self.get_request(page1_url) request.session['cms_edit'] = True request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) request.toolbar.show_toolbar = True template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" with self.assertNumQueries(4): self.render_template_obj(template, {}, request) page1.publish('en') exclude = [ 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware' ] overrides = dict( CMS_PAGE_CACHE=False, MIDDLEWARE=[mw for mw in settings.MIDDLEWARE if mw not in exclude], ) with self.settings(**overrides): with self.assertNumQueries(FuzzyInt(13, 25)): self.client.get(page1_url) with self.assertNumQueries(FuzzyInt(5, 11)): self.client.get(page1_url) overrides['CMS_PLACEHOLDER_CACHE'] = False with self.settings(**overrides): with self.assertNumQueries(FuzzyInt(7, 15)): self.client.get(page1_url)
def test_perm_permissions(self): """ Test that no page menu is present if user has general page Page.change_perm but not permission on current page """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() self.user_staff.user_permissions.add(Permission.objects.get(codename='change_page')) self.user_staff = User.objects.get(pk=self.user_staff.pk) request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page') self.assertEqual(page_menu, [])
def test_perm(self): """ Test that page meta menu is present if user has Page.change_perm """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() self.user_staff.user_permissions.add(Permission.objects.get(codename='change_page')) self.user_staff = User.objects.get(pk=self.user_staff.pk) request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] self.assertEqual(len(page_menu.find_items(ModalItem, name="%s ..." % force_text(PAGE_SITEMAP_MENU_TITLE))), 1)
def test_toolbar(self): """ Test that PageMeta/TitleMeta items are present for superuser """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() request = self.get_page_request(page1, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] meta_menu = page_menu.find_items(SubMenu, name=force_unicode(PAGE_META_MENU_TITLE))[0].item self.assertEqual(len(meta_menu.find_items(ModalItem, name="%s ..." % force_unicode(PAGE_META_ITEM_TITLE))), 1) self.assertEqual(len(meta_menu.find_items(ModalItem)), len(self.languages) + 1)
def test_toolbar_staff(self): # Test that the toolbar contains edit mode switcher if placeholders are available apphooks = ( 'cms.test_utils.project.placeholderapp.cms_apps.Example1App', ) with self.settings(CMS_APPHOOKS=apphooks, ROOT_URLCONF='cms.test_utils.project.placeholderapp_urls'): self.create_base_structure('Example1App', 'en') ex1 = Example1.objects.create(char_1='1', char_2='2', char_3='3', char_4='4', date_field=now()) path = reverse('example_detail', kwargs={'pk': ex1.pk}) self.user = self._create_user('admin_staff', True, True) with self.login_user_context(self.user): response = self.client.get(path+"?edit") toolbar = CMSToolbar(response.context['request']) toolbar.populate() placeholder_toolbar = PlaceholderToolbar(response.context['request'], toolbar, True, path) placeholder_toolbar.populate() placeholder_toolbar.init_placeholders_from_request() placeholder_toolbar.add_structure_mode() self.assertEqual(len(placeholder_toolbar.toolbar.get_right_items()), 1) self.user = self._create_user('staff', True, False) with self.login_user_context(self.user): response = self.client.get(path+"?edit") response.context['request'].user = get_user_model().objects.get(pk=self.user.pk) toolbar = CMSToolbar(response.context['request']) toolbar.populate() placeholder_toolbar = PlaceholderToolbar(response.context['request'], toolbar, True, path) placeholder_toolbar.populate() placeholder_toolbar.init_placeholders_from_request() placeholder_toolbar.add_structure_mode() self.assertEqual(len(placeholder_toolbar.toolbar.get_right_items()), 0) self.user.user_permissions.add(Permission.objects.get(codename='change_example1')) with self.login_user_context(self.user): response = self.client.get(path+"?edit") response.context['request'].user = get_user_model().objects.get(pk=self.user.pk) toolbar = CMSToolbar(response.context['request']) toolbar.populate() placeholder_toolbar = PlaceholderToolbar(response.context['request'], toolbar, True, path) placeholder_toolbar.populate() placeholder_toolbar.init_placeholders_from_request() placeholder_toolbar.add_structure_mode() self.assertEqual(len(placeholder_toolbar.toolbar.get_right_items()), 0) permission = Permission.objects.get(codename='use_structure') self.user.user_permissions.add(permission) response.context['request'].user = get_user_model().objects.get(pk=self.user.pk) placeholder_toolbar = PlaceholderToolbar(response.context['request'], toolbar, True, path) placeholder_toolbar.populate() placeholder_toolbar.init_placeholders_from_request() placeholder_toolbar.add_structure_mode() self.assertEqual(len(placeholder_toolbar.toolbar.get_right_items()), 1) self.user = None
def test_no_change_button(self): page = create_page("test", "nav_playground.html", "en", published=True) user = self.get_staff() user.user_permissions.all().delete() request = self.get_page_request(page, user, edit=True) toolbar = CMSToolbar(request) self.assertFalse(page.has_change_permission(request)) self.assertFalse(page.has_publish_permission(request)) items = toolbar.get_left_items() + toolbar.get_right_items() # Logo + page-menu + admin-menu + logout self.assertEqual(len(items), 3, items) admin_items = toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER, "Test").get_items() self.assertEqual(len(admin_items), 6, admin_items)
def test_perm(self): """ Test that page tags menu is present if user has Page.change_perm """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() self.user_staff.user_permissions.add(Permission.objects.get(codename='change_page')) self.user_staff = User.objects.get(pk=self.user_staff.pk) request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page')[0].item tags_menu = page_menu.find_items(SubMenu, name=force_unicode(PAGE_TAGS_MENU_TITLE))[0].item self.assertEqual(len(tags_menu.find_items(ModalItem, name="%s ..." % force_unicode(PAGE_TAGS_ITEM_TITLE))), 1)
def _render_menu(self, user=None, **kwargs): request = RequestFactory().get("/") if not user: is_auth_user = kwargs.get("is_auth_user", True) user = self.get_superuser() if is_auth_user else AnonymousUser() request.user = user request.session = {} toolbar = CMSToolbar(request) if kwargs.get("edit_mode", False): toolbar.edit_mode_active = True toolbar.preview_mode_active = False elif kwargs.get("preview_mode", False): toolbar.edit_mode_active = False toolbar.preview_mode_active = True else: toolbar.edit_mode_active = False toolbar.preview_mode_active = False request.toolbar = toolbar context = {"request": request} template = Template("{% load menu_tags %}" "{% show_menu 0 100 100 100 %}") template.render(Context(context)) return context
def test_perm(self): """ Test that page tags menu is present if user has Page.change_perm """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() self.user_staff.user_permissions.add(Permission.objects.get(codename='change_page')) self.user_staff = User.objects.get(pk=self.user_staff.pk) request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page')[0].item tags_menu = page_menu.find_items(SubMenu, name=force_text(PAGE_TAGS_MENU_TITLE))[0].item self.assertEqual(len(tags_menu.find_items(ModalItem, name="{0}...".format(force_text(PAGE_TAGS_ITEM_TITLE)))), 1)
def test_perm_permissions(self): """ Test that no page menu is present if user has general page Page.change_perm but not permission on current page """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() self.user_staff.user_permissions.add( Permission.objects.get(codename='change_page')) self.user_staff = User.objects.get(pk=self.user_staff.pk) request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page') self.assertEqual(page_menu, [])
def test_no_perm(self): """ Test that no page menu is present if user has no perm """ from cms.toolbar.toolbar import CMSToolbar page1, __ = self.get_pages() request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page') try: self.assertEqual(page_menu, []) except AssertionError: meta_menu = page_menu[0].item.find_items(SubMenu, name=force_text(PAGE_META_MENU_TITLE)) self.assertEqual(meta_menu, [])
def test_no_perm(self): """ Test that no page menu is present if user has no perm """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page') try: self.assertEqual(page_menu, []) except AssertionError: tags_menu = page_menu[0].item.find_items(SubMenu, name=force_text(PAGE_TAGS_MENU_TITLE)) self.assertEqual(tags_menu, [])
def test_toolbar(self): """ Test that PageSitemapProperties item is present for superuser """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() request = self.get_page_request(page1, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] self.assertEqual( len( page_menu.find_items(ModalItem, name="%s ..." % force_text(PAGE_SITEMAP_MENU_TITLE))), 1)
def test_toolbar_with_items(self): """ Test that Blog toolbar is present and contains all items """ from cms.toolbar.toolbar import CMSToolbar post = self._get_post(self.data['en'][0]) page1, page2 = self.get_pages() request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True) setattr(request, BLOG_CURRENT_POST_IDENTIFIER, post) toolbar = CMSToolbar(request) toolbar.get_left_items() blog_menu = toolbar.menus['djangocms_blog'] 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_post_change', args=(post.pk,)))), 1)
def test_toolbar_with_items(self): """ Test that Blog toolbar is present and contains all items """ from cms.toolbar.toolbar import CMSToolbar posts = self.get_posts() pages = self.get_pages() request = self.get_page_request(pages[0], self.user, r'/en/blog/', edit=True) setattr(request, BLOG_CURRENT_POST_IDENTIFIER, posts[0]) toolbar = CMSToolbar(request) toolbar.get_left_items() blog_menu = toolbar.menus['djangocms_blog'] 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_post_change', args=(posts[0].pk,)))), 1)
def test_no_change_button(self): page = create_page('test', 'nav_playground.html', 'en', published=True) user = self.get_staff() user.user_permissions.all().delete() request = self.get_page_request(page, user, edit=True) toolbar = CMSToolbar(request) self.assertFalse(page.has_change_permission(request)) self.assertFalse(page.has_publish_permission(request)) items = toolbar.get_left_items() + toolbar.get_right_items() # Logo + page-menu + admin-menu + logout self.assertEqual(len(items), 3, items) admin_items = toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER, 'Test').get_items() self.assertEqual(len(admin_items), 6, admin_items)
def test_toolbar_with_items(self): """ Test that PageSitemapProperties item is present for superuser if PageSitemapProperties exists for current page """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() page_ext = PageSitemapProperties.objects.create( extended_object=page1, priority='0.2', changefreq='never') request = self.get_page_request(page1, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] meta_menu = page_menu.find_items(ModalItem, name="%s ..." % force_text(PAGE_SITEMAP_MENU_TITLE))[0].item self.assertTrue(meta_menu.url.startswith(reverse('admin:djangocms_page_sitemap_pagesitemapproperties_change', args=(page_ext.pk,)))) self.assertEqual(force_text(page_ext), force_text(_(u'Sitemap values for Page %s') % page1.pk))
def test_nested_plugins_language_fallback(self): """ Tests language_fallback placeholder configuration for nested plugins""" page_en = create_page('page_en', 'col_two.html', 'en') title_de = create_title("de", "page_de", page_en) placeholder_en = page_en.placeholders.get(slot='col_left') placeholder_de = title_de.page.placeholders.get(slot='col_left') link_en = add_plugin(placeholder_en, LinkPlugin, 'en', name='en name', url='http://example.com/en') add_plugin(placeholder_en, TextPlugin, 'en', target=link_en, body='en body') context_en = SekizaiContext() context_en['request'] = self.get_request(language="en", page=page_en) context_de = SekizaiContext() context_de['request'] = self.get_request(language="de", page=page_en) conf = { 'col_left': { 'language_fallback': True, }, } with self.settings(CMS_PLACEHOLDER_CONF=conf): content_de = _render_placeholder(placeholder_de, context_de) self.assertRegexpMatches(content_de, "<a href=\"http://example.com/en\"") self.assertRegexpMatches(content_de, "en body") context_de2 = SekizaiContext() request = self.get_request(language="de", page=page_en) request.user = self.get_superuser() request.toolbar = CMSToolbar(request) request.toolbar.edit_mode = True context_de2['request'] = request del (placeholder_de._plugins_cache) cache.clear() content_de2 = _render_placeholder(placeholder_de, context_de2) self.assertFalse("en body" in content_de2) # remove the cached plugins instances del (placeholder_de._plugins_cache) cache.clear() # Then we add a plugin to check for proper rendering link_de = add_plugin(placeholder_en, LinkPlugin, 'de', name='de name', url='http://example.com/de') add_plugin(placeholder_en, TextPlugin, 'de', target=link_de, body='de body') content_de = _render_placeholder(placeholder_de, context_de) self.assertRegexpMatches(content_de, "<a href=\"http://example.com/de\"") self.assertRegexpMatches(content_de, "de body")
def test_templatetags_page_placeholder_as_variable(self): """ The output of the "page_placeholder" template tag can be retrieved as a variable instead of being returned directly in the template. """ user = UserFactory(is_staff=True, is_superuser=True) page = create_page("Test", "richie/fullwidth.html", "en", published=True) placeholder = page.placeholders.all()[0] add_plugin(placeholder, CKEditorPlugin, "en", body="<b>Test</b>") request = RequestFactory().get("/") request.current_page = page request.user = user request.session = {} request.toolbar = CMSToolbar(request) request.toolbar.is_staff = True template = ( '{% load cms_tags extra_tags %}{% page_placeholder "maincontent" page as var %}' "<h1>{{ var }}</h1>") with self.assertNumQueries(3): output = self.render_template_obj(template, {"page": page}, request) self.assertEqual("<h1><b>Test</b>\n</h1>", output)
def test_templatetags_page_placeholder_current_page(self): """ The "page_placeholder" template tag should work as the "placeholder" template tag when the "page" passed as argument is the current page and edit mode is off. """ user = UserFactory(is_staff=True, is_superuser=True) page = create_page("Test", "richie/fullwidth.html", "en", published=True) placeholder = page.placeholders.all()[0] add_plugin(placeholder, CKEditorPlugin, "en", body="<b>Test</b>") request = RequestFactory().get("/") request.current_page = page request.user = user request.session = {} request.toolbar = CMSToolbar(request) request.toolbar.is_staff = True template = ( '{% load cms_tags extra_tags %}{% page_placeholder "maincontent" page %}' ) # If edit mode is off, he frontend editing markup should not be included in the page with self.assertNumQueries(3): output = self.render_template_obj(template, {"page": page}, request) self.assertEqual("<b>Test</b>\n", output)
def test_templatetags_page_placeholder_other_page(self): """ The "page_placeholder" template tag should work as the "show_placeholder" template tag when the "page" passed as argument is different from the current page (readonly display). """ user = UserFactory(is_staff=True, is_superuser=True) current_page = create_page("Current", "richie/fullwidth.html", "en", published=True) page = create_page("Test", "richie/fullwidth.html", "en", published=True) placeholder = page.placeholders.all()[0] add_plugin(placeholder, CKEditorPlugin, "en", body="<b>Test</b>") request = RequestFactory().get("/") request.current_page = current_page request.user = user request.session = {"cms_edit": True} request.toolbar = CMSToolbar(request) request.toolbar.is_staff = True template = ( '{% load cms_tags extra_tags %}{% page_placeholder "maincontent" page %}' ) with self.assertNumQueries(3): output = self.render_template_obj(template, {"page": page}, request) self.assertEqual("<b>Test</b>\n", output)
def TTLCacheExpirationPlugin(self): page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) placeholder1 = page1.placeholders.filter(slot="body")[0] placeholder2 = page1.placeholders.filter(slot="right-column")[0] plugin_pool.register_plugin(TTLCacheExpirationPlugin) add_plugin(placeholder1, "TextPlugin", 'en', body="English") add_plugin(placeholder2, "TextPlugin", 'en', body="Deutsch") # Add *CacheExpirationPlugins, one expires in 50s, the other in 40s. # The page should expire in the least of these, or 40s. add_plugin(placeholder1, "TTLCacheExpirationPlugin", 'en') # Ensure that we're testing in an environment WITHOUT the MW cache, as # we are testing the internal page cache, not the MW cache. exclude = [ 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.cache.CacheMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', ] overrides = { 'MIDDLEWARE': [mw for mw in settings.MIDDLEWARE if mw not in exclude] } with self.settings(**overrides): page1.publish('en') request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(14, 25)): # was 14, 24 response = self.client.get('/en/') self.assertTrue('max-age=50' in response['Cache-Control'], response['Cache-Control']) plugin_pool.unregister_plugin(TTLCacheExpirationPlugin)
def test_toolbar(self): """ Test that UserSettings toolbar are present for superuser """ from cms.toolbar.toolbar import CMSToolbar page = self.get_pages() request = self.get_page_request(page, self.user, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() admin_menu = toolbar.menus[ADMIN_MENU_IDENTIFIER] usersettings_model_opts = self.UserSettings._meta MENU_ITEM_TITLE = usersettings_model_opts.verbose_name self.assertEqual(len(admin_menu.find_items( ModalItem, name="%s ..." % force_unicode(MENU_ITEM_TITLE))), 1)
def process_request(self, request): """ If we should show the toolbar for this request, put it on request.toolbar. Then call the request_hook on the toolbar. """ if 'edit' in request.GET and not request.session.get('cms_edit', False): if not request.session.get('cms_edit', False): menu_pool.clear() request.session['cms_edit'] = True if request.session.get('cms_build', False): request.session['cms_build'] = False if 'edit_off' in request.GET and request.session.get('cms_edit', True): if request.session.get('cms_edit', True): menu_pool.clear() request.session['cms_edit'] = False if request.session.get('cms_build', False): request.session['cms_build'] = False if 'build' in request.GET and not request.session.get('cms_build', False): request.session['cms_build'] = True if request.user.is_staff: try: request.cms_latest_entry = LogEntry.objects.filter( user=request.user, action_flag__in=(ADDITION, CHANGE) ).only('pk').order_by('-pk')[0].pk except IndexError: request.cms_latest_entry = -1 request.toolbar = CMSToolbar(request)
def test_timedelta_cache_plugin(self): page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) placeholder1 = page1.placeholders.filter(slot="body")[0] placeholder2 = page1.placeholders.filter(slot="right-column")[0] plugin_pool.register_plugin(TimeDeltaCacheExpirationPlugin) add_plugin(placeholder1, "TextPlugin", 'en', body="English") add_plugin(placeholder2, "TextPlugin", 'en', body="Deutsch") # Add *TimeDeltaCacheExpirationPlugin, expires in 45s. add_plugin(placeholder1, "TimeDeltaCacheExpirationPlugin", 'en') # Ensure that we're testing in an environment WITHOUT the MW cache, as # we are testing the internal page cache, not the MW cache. exclude = [ 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.cache.CacheMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', ] mw_classes = [mw for mw in settings.MIDDLEWARE_CLASSES if mw not in exclude] with self.settings(MIDDLEWARE_CLASSES=mw_classes): page1.publish('en') request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(14, 25)): # was 14, 24 response = self.client.get('/en/') self.assertTrue('max-age=45' in response['Cache-Control'], response['Cache-Control']) plugin_pool.unregister_plugin(TimeDeltaCacheExpirationPlugin)
def _get_toolbar(self, content_obj, **kwargs): """Helper method to set up the toolbar """ request = RequestFactory().get('/') request.user = UserFactory() request.session = {} cms_toolbar = CMSToolbar(request) toolbar = VersioningToolbar(request, toolbar=cms_toolbar, is_current_app=True, app_path='/') toolbar.toolbar.obj = content_obj if kwargs.get('edit_mode', False): toolbar.toolbar.edit_mode_active = True toolbar.toolbar.content_mode_active = False toolbar.toolbar.structure_mode_active = False elif kwargs.get('preview_mode', False): toolbar.toolbar.edit_mode_active = False toolbar.toolbar.content_mode_active = True toolbar.toolbar.structure_mode_active = False elif kwargs.get('structure_mode', False): toolbar.toolbar.edit_mode_active = False toolbar.toolbar.content_mode_active = False toolbar.toolbar.structure_mode_active = True return toolbar
def test_restore_clipboard(self): cascade_clipboard = CascadeClipboard.objects.create( identifier=self.identifier, data=self.placeholder_data) cascade_clipboard.save() request = self.get_request('/') request.toolbar = CMSToolbar(request) self.assertIsNotNone(request.toolbar.clipboard) # check that clipboard is empty self.assertEqual(request.toolbar.clipboard.cmsplugin_set.count(), 0) # copy plugins from clipboard to placeholder change_clipboard_url = '/en/admin/cmsplugin_cascade/cascadeclipboard/{}/' if DJANGO_VERSION < (1, 9): change_clipboard_url = change_clipboard_url.format( cascade_clipboard.pk) else: change_clipboard_url = (change_clipboard_url + 'change/').format( cascade_clipboard.pk) data = { 'identifier': self.identifier, 'restore_clipboard': 'Restore', 'data': json.dumps(self.placeholder_data) } response = self.client.post(change_clipboard_url, data) self.assertEqual(response.status_code, 302) change_clipboard_url = response['location'] response = self.client.get(change_clipboard_url, data) self.assertEqual(response.status_code, 200) needle = '<li class="success">The Persited Clipboard Content "Test saved clipboard" was changed successfully. You may edit it again below.</li>' self.assertContains(response, needle) # check if clipboard has been populated with plugins from serialized data self.assertEqual(request.toolbar.clipboard.cmsplugin_set.count(), 5)
def test_placeholder_or_in_edit_mode(self): """ Tests the {% placeholder or %} templatetag in edit mode. """ t = u'{% load cms_tags %}' + \ u'|{% placeholder "empty" or %}No content{% endplaceholder %}' superuser = self.get_superuser() with self.login_user_context(superuser): endpoint = self.test_page.get_absolute_url() + '?edit' request = self.get_request(endpoint, page=self.test_page) request.session['cms_edit'] = True request.toolbar = CMSToolbar(request) renderer = self.get_content_renderer(request) context = SekizaiContext() context['cms_content_renderer'] = renderer placeholder = self.test_page.placeholders.get(slot='empty') expected = renderer.render_placeholder( placeholder, context=context, language='en', page=self.test_page, editable=True, ) expected = u'|{}No content'.format(expected) rendered = self.render(self.test_page, template=t, request=request) self.assertEqual(rendered, self.strip_rendered(expected))
def test_toolbar_multiple_supported_apps(self): # Test with a basic application with no defined app_name and no namespace self.create_base_structure(APP_NAME, 'en') path = reverse('detail', kwargs={'id': 20}) request = self.get_request(path) toolbar = CMSToolbar(request) self.assertEqual( toolbar.toolbars[ 'cms.test_utils.project.sampleapp.cms_toolbars.CategoryToolbar'] .app_path, 'cms.test_utils.project.placeholderapp') self.assertFalse(toolbar.toolbars[ 'cms.test_utils.project.sampleapp.cms_toolbars.CategoryToolbar']. is_current_app) self.assertEqual( toolbar.toolbars[ 'cms.test_utils.project.extensionapp.cms_toolbars.MyTitleExtensionToolbar'] .app_path, 'cms.test_utils.project.placeholderapp') self.assertFalse(toolbar.toolbars[ 'cms.test_utils.project.extensionapp.cms_toolbars.MyTitleExtensionToolbar'] .is_current_app) self.assertEqual( toolbar.toolbars[ 'cms.test_utils.project.extensionapp.cms_toolbars.MyPageExtensionToolbar'] .app_path, 'cms.test_utils.project.placeholderapp') self.assertTrue(toolbar.toolbars[ 'cms.test_utils.project.extensionapp.cms_toolbars.MyPageExtensionToolbar'] .is_current_app) self.assertEqual( toolbar.toolbars[ 'cms.test_utils.project.placeholderapp.cms_toolbars.Example1Toolbar'] .app_path, 'cms.test_utils.project.placeholderapp') self.assertTrue(toolbar.toolbars[ 'cms.test_utils.project.placeholderapp.cms_toolbars.Example1Toolbar'] .is_current_app)
def process_request(self, request): """ If we should show the toolbar for this request, put it on request.toolbar. Then call the request_hook on the toolbar. """ if 'edit' in request.GET and not request.session.get( 'cms_edit', False): if not request.session.get('cms_edit', False): menu_pool.clear() request.session['cms_edit'] = True if request.session.get('cms_build', False): request.session['cms_build'] = False if 'edit_off' in request.GET and request.session.get('cms_edit', True): if request.session.get('cms_edit', True): menu_pool.clear() request.session['cms_edit'] = False if request.session.get('cms_build', False): request.session['cms_build'] = False if 'build' in request.GET and not request.session.get( 'cms_build', False): request.session['cms_build'] = True if request.user.is_staff: request.session['cms_log_entries'] = LogEntry.objects.filter( user=request.user).count() request.toolbar = CMSToolbar(request)
def test_templatetags_blockplugin(self): """ The "blockplugin" template tag should render content to the page in which it is placed. """ user = AnonymousUser() page = create_page("Test", "richie/single_column.html", "en", published=True) placeholder = page.placeholders.all()[0] plugin = add_plugin(placeholder, CKEditorPlugin, "en", body="<b>Test</b>") request = RequestFactory().get("/") request.current_page = page request.user = user request.session = {} request.toolbar = CMSToolbar(request) template = ("{% load extra_tags %}{% blockplugin plugin %}" "{{ instance.body|safe }}" "{% endblockplugin %}") with self.assertNumQueries(1): output = self.render_template_obj(template, { "plugin": plugin }, request).replace("\n", "") self.assertEqual("<b>Test</b>", output)
def test_dual_legacy_cache_plugins(self): page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) placeholder1 = page1.placeholders.filter(slot="body")[0] placeholder2 = page1.placeholders.filter(slot="right-column")[0] plugin_pool.register_plugin(LegacyCachePlugin) add_plugin(placeholder1, "TextPlugin", 'en', body="English") add_plugin(placeholder2, "TextPlugin", 'en', body="Deutsch") # Adds a no-cache plugin. In older versions of the CMS, this would # prevent the page from caching in, but since this plugin also defines # get_cache_expiration() it is ignored. add_plugin(placeholder1, "LegacyCachePlugin", 'en') # Ensure that we're testing in an environment WITHOUT the MW cache, as # we are testing the internal page cache, not the MW cache. exclude = [ 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.cache.CacheMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', ] overrides = dict() if getattr(settings, 'MIDDLEWARE', None): overrides['MIDDLEWARE'] = [mw for mw in settings.MIDDLEWARE if mw not in exclude] else: overrides['MIDDLEWARE_CLASSES'] = [mw for mw in settings.MIDDLEWARE_CLASSES if mw not in exclude] with self.settings(**overrides): page1.publish('en') request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(14, 25)): response = self.client.get('/en/') self.assertTrue('no-cache' not in response['Cache-Control']) plugin_pool.unregister_plugin(LegacyCachePlugin)
def test_lti_consumer_view_set_get_context_method_is_cached( self, mock_params): """ If "memory_cache" is defined, get_context method is cached for 9 minutes and 30 seconds to optimize db accesses without return stale oauth credentials. """ placeholder = Placeholder.objects.create(slot="test") lti_consumer = LTIConsumerFactory() model_instance = add_plugin( placeholder, LTIConsumerPlugin, "en", url=lti_consumer.url, lti_provider_id=lti_consumer.lti_provider_id, ) request = RequestFactory().get("/") request.user = AnonymousUser() request.session = {} request.toolbar = CMSToolbar(request) view_set = LTIConsumerViewsSet() with self.assertNumQueries(1): view_set.get_context(request, "v1.0", model_instance.pk) mock_params.assert_called_once() mock_params.reset_mock() with self.assertNumQueries(0): view_set.get_context(request, "v1.0", model_instance.pk) mock_params.assert_not_called()
def get_toolbar(self, request): form = RequestToolbarForm(request.GET or None) if not form.is_valid(): return HttpResponseBadRequest('Invalid parameters') form_data = form.cleaned_data cms_path = form_data.get('cms_path') or request.path_info origin_url = urlparse(cms_path) attached_obj = form_data.get('attached_obj') current_page = get_page_from_request(request, use_path=origin_url.path, clean_path=True) if attached_obj and current_page and not (attached_obj == current_page): return HttpResponseBadRequest( 'Generic object does not match current page') data = QueryDict(query_string=origin_url.query, mutable=True) placeholders = request.GET.getlist("placeholders[]") if placeholders: data.setlist('placeholders[]', placeholders) request = copy.copy(request) request.GET = data request.current_page = current_page request.toolbar = CMSToolbar(request, request_path=origin_url.path) request.toolbar.set_object(attached_obj or current_page) return HttpResponse(request.toolbar.render())
def test_perm_permissions(self): """ Test that no page menu is present if user has general page Page.change_perm but not permission on current page """ from cms.toolbar.toolbar import CMSToolbar page1, __ = self.get_pages() self.user_staff.user_permissions.add(Permission.objects.get(codename='change_page')) self.user_staff = User.objects.get(pk=self.user_staff.pk) request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.find_items(Menu, name='Page') try: self.assertEqual(page_menu, []) except AssertionError: meta_menu = page_menu[0].item.find_items(SubMenu, name=force_text(PAGE_META_MENU_TITLE)) self.assertEqual(meta_menu, [])
def test_perm(self): """ Test that page meta menu is present if user has Page.change_perm """ from cms.toolbar.toolbar import CMSToolbar page1, page2 = self.get_pages() self.user_staff.user_permissions.add(Permission.objects.get(codename='change_page')) self.user_staff = User.objects.get(pk=self.user_staff.pk) request = self.get_page_request(page1, self.user_staff, '/', edit=True) toolbar = CMSToolbar(request) toolbar.get_left_items() page_menu = toolbar.menus['page'] meta_menu = page_menu.find_items(SubMenu, name=force_text(PAGE_META_MENU_TITLE))[0].item try: self.assertEqual(len(meta_menu.find_items(ModalItem, name="{0}...".format(force_text(PAGE_META_ITEM_TITLE)))), 1) except AssertionError: self.assertEqual(len(meta_menu.find_items(ModalItem, name="{0} ...".format(force_text(PAGE_META_ITEM_TITLE)))), 1)
def test_no_page_staff(self): request = self.get_page_request(None, self.get_staff(), '/') toolbar = CMSToolbar(request) toolbar.populate() toolbar.post_template_populate() items = toolbar.get_left_items() + toolbar.get_right_items() # Logo + admin-menu + logout self.assertEqual(len(items), 2, items) admin_items = toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER, 'Test').get_items() self.assertEqual(len(admin_items), 6, admin_items)
def test_no_page_anon(self): request = self.get_page_request(None, self.get_anon(), '/') toolbar = CMSToolbar(request) toolbar.populate() toolbar.post_template_populate() items = toolbar.get_left_items() + toolbar.get_right_items() self.assertEqual(len(items), 0)