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 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', ] 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=50' in response['Cache-Control'], response['Cache-Control']) plugin_pool.unregister_plugin(TTLCacheExpirationPlugin)
def test_moving_plugin_to_different_placeholder(self): plugin_pool.register_plugin(DumbFixturePlugin) page = create_page("page", "nav_playground.html", "en", published=True) plugin_data = { 'plugin_type': 'DumbFixturePlugin', 'language': settings.LANGUAGES[0][0], 'placeholder': page.placeholders.get(slot='body').pk, } response = self.client.post(URL_CMS_PLUGIN_ADD % page.pk, plugin_data) self.assertEquals(response.status_code, 200) plugin_data['parent_id'] = int(response.content) del plugin_data['placeholder'] response = self.client.post(URL_CMS_PLUGIN_ADD % page.pk, plugin_data) self.assertEquals(response.status_code, 200) post = { 'plugin_id': int(response.content), 'placeholder': 'right-column', } response = self.client.post(URL_CMS_PLUGIN_MOVE % page.pk, post) self.assertEquals(response.status_code, 200) from cms.plugins.utils import build_plugin_tree build_plugin_tree(page.placeholders.get(slot='right-column').get_plugins_list()) plugin_pool.unregister_plugin(DumbFixturePlugin)
def test_no_cache_plugin(self): template = Template("{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}") 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.assertNumQueries(FuzzyInt(10, 14)): 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_08_unregister_non_existing_plugin_should_raise(self): number_of_plugins_before = len(plugin_pool.get_all_plugins()) raised = False try: # There should not be such a plugin registered if the others tests # don't leak plugins plugin_pool.unregister_plugin(DumbFixturePlugin) except PluginNotRegistered: raised = True self.assertTrue(raised) # Let's count, to make sure we didn't remove a plugin accidentally. number_of_plugins_after = len(plugin_pool.get_all_plugins()) self.assertEqual(number_of_plugins_before, number_of_plugins_after)
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 = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" with self.assertNumQueries(3): self.render_template_obj(template, {}, request) request = self.get_request('/en/') 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(1): self.render_template_obj(template, {}, request) 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 = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" with self.assertNumQueries(4): output = self.render_template_obj(template, {}, request) with self.assertNumQueries(FuzzyInt(14, 19)): 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 = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" with self.assertNumQueries(4): output2 = self.render_template_obj(template, {}, request) 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(output, output2) self.assertNotEqual(resp1, resp2) plugin_pool.unregister_plugin(NoCachePlugin)
def test_07_register_plugin_twice_should_raise(self): number_of_plugins_before = len(plugin_pool.get_all_plugins()) # The first time we register the plugin is should work plugin_pool.register_plugin(DumbFixturePlugin) # Let's add it a second time. We should catch and exception raised = False try: plugin_pool.register_plugin(DumbFixturePlugin) except PluginAlreadyRegistered: raised = True self.assertTrue(raised) # Let's also unregister the plugin now, and assert it's not in the # pool anymore plugin_pool.unregister_plugin(DumbFixturePlugin) # Let's make sure we have the same number of plugins as before: number_of_plugins_after = len(plugin_pool.get_all_plugins()) self.assertEqual(number_of_plugins_before, number_of_plugins_after)
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', ] 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)): # 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)
from models import UserContact from forms import UserContactForm, UserContactAdminForm class UserContactPlugin(ContactPlugin): name = _("User Contact Form") model = UserContact form = UserContactAdminForm contact_form = UserContactForm # We're using the original cmsplugin_contact templates here which # works fine but requires that the original plugin is in INSTALLED_APPS. email_template = "cmsplugin_user_contact/email.txt" render_template = "cmsplugin_user_contact/contact.html" subject_template = "cmsplugin_user_contact/subject.txt" change_form_template = "admin/cms/page/plugin_change_form.html" fieldsets = ( (None, {"fields": ("site_email", "email_label", "sender_label", "thanks", "submit")}), # (_('Spam Protection'), {'fields': ('spam_protection_method', 'akismet_api_key', # 'recaptcha_public_key', 'recaptcha_private_key', # 'recaptcha_theme') # }) ) plugin_pool.unregister_plugin(ContactPlugin) plugin_pool.register_plugin(UserContactPlugin)
from cms.plugin_pool import plugin_pool from cms.plugins.file.cms_plugins import FilePlugin as CMSFilePlugin from models import File class FilePlugin(CMSFilePlugin): model = File plugin_pool.unregister_plugin(FilePlugin) plugin_pool.register_plugin(FilePlugin)
from djangocms_text_ckeditor.cms_plugins import TextPlugin as TextPluginBase from cmsplugin_text_wrapper.models import TextWrapper from cmsplugin_text_wrapper.forms import TextForm class TextPlugin(TextPluginBase): model = TextWrapper form = TextForm def render(self, context, instance, placeholder): wrappers = [w[1] for w in settings.CMS_TEXT_WRAPPERS if w[0] == instance.wrapper] if wrappers: instance.render_template = wrappers[0].get('render_template') context.update(wrappers[0].get('extra_context', {})) extra_classes = [] for csscls in instance.classes: try: extra_classes.append(dict(instance.CLASSES)[int(csscls)]) except (ValueError, IndexError): pass context['extra_classes'] = ' '.join(extra_classes) context.update({ 'body': plugin_tags_to_user_html(instance.body, context, placeholder), 'placeholder': placeholder, 'object': instance, }) return context plugin_pool.unregister_plugin(TextPluginBase) plugin_pool.register_plugin(TextPlugin)
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] try: plugin_pool.register_plugin(NoCachePlugin) except PluginAlreadyRegistered: pass add_plugin(placeholder1, 'TextPlugin', 'en', body="English") add_plugin(placeholder2, 'TextPlugin', 'en', body="Deutsch") template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" # 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): # Request the page without the 'no-cache' plugin request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(18, 25)): response1 = self.client.get('/en/') content1 = response1.content # Fetch it again, it is cached. request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(0): response2 = self.client.get('/en/') content2 = response2.content self.assertEqual(content1, content2) # Once again with PAGE_CACHE=False, to prove the cache can # be disabled request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.settings(CMS_PAGE_CACHE=False): with self.assertNumQueries(FuzzyInt(5, 24)): response3 = self.client.get('/en/') content3 = response3.content self.assertEqual(content1, content3) # Add the 'no-cache' plugin 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) with self.assertNumQueries(FuzzyInt(4, 6)): output = self.render_template_obj(template, {}, request) with self.assertNumQueries(FuzzyInt(14, 24)): response = self.client.get('/en/') self.assertTrue("no-cache" in response['Cache-Control']) 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) with self.assertNumQueries(4): output2 = self.render_template_obj(template, {}, request) with self.settings(CMS_PAGE_CACHE=False): with self.assertNumQueries(FuzzyInt(8, 14)): response = self.client.get('/en/') resp2 = response.content.decode('utf8').split("$$$")[1] self.assertNotEqual(output, output2) self.assertNotEqual(resp1, resp2) plugin_pool.unregister_plugin(NoCachePlugin)
def tearDown(self): from django.core.cache import cache super(PlaceholderCacheTestCase, self).tearDown() plugin_pool.unregister_plugin(VaryCacheOnPlugin) cache.clear()
def test_expiration_cache_plugins(self): """ Tests that when used in combination, the page is cached to the shortest TTL. """ page1 = create_page('test page 1', 'nav_playground.html', 'en', published=True) page1_url = page1.get_absolute_url() placeholder1 = page1.placeholders.filter(slot="body")[0] placeholder2 = page1.placeholders.filter(slot="right-column")[0] plugin_pool.register_plugin(TTLCacheExpirationPlugin) try: plugin_pool.register_plugin(DateTimeCacheExpirationPlugin) except PluginAlreadyRegistered: pass try: plugin_pool.register_plugin(NoCachePlugin) except PluginAlreadyRegistered: pass 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') add_plugin(placeholder2, "DateTimeCacheExpirationPlugin", '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(page1_url) request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(14, 26)): response = self.client.get(page1_url) resp1 = response.content.decode('utf8').split("$$$")[1] self.assertTrue('max-age=40' in response['Cache-Control'], response['Cache-Control']) # noqa cache_control1 = response['Cache-Control'] expires1 = response['Expires'] time.sleep(1) # This ensures that the cache has aged measurably # Request it again, this time, it comes from the cache request = self.get_request(page1_url) request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(0): response = self.client.get(page1_url) resp2 = response.content.decode('utf8').split("$$$")[1] # Content will be the same self.assertEqual(resp2, resp1) # Cache-Control will be different because the cache has aged self.assertNotEqual(response['Cache-Control'], cache_control1) # However, the Expires timestamp will be the same self.assertEqual(response['Expires'], expires1) plugin_pool.unregister_plugin(TTLCacheExpirationPlugin) plugin_pool.unregister_plugin(DateTimeCacheExpirationPlugin) plugin_pool.unregister_plugin(NoCachePlugin)
class VideoTrackPlugin(BaseVideoTrackPlugin): model = VersionedVideoTrack fieldsets = [(None, { 'fields': ( 'kind', 'file_grouper', 'srclang', ) }), (_('Advanced settings'), { 'classes': ('collapse', ), 'fields': ( 'label', 'attributes', ) })] def get_render_template(self, context, instance, placeholder): return 'djangocms_versioning_filer/plugins/{}/video_track.html'.format( context['video_template']) plugin_pool.unregister_plugin(BaseVideoPlayerPlugin) plugin_pool.unregister_plugin(BaseVideoSourcePlugin) plugin_pool.unregister_plugin(BaseVideoTrackPlugin) plugin_pool.register_plugin(VideoPlayerPlugin) plugin_pool.register_plugin(VideoSourcePlugin) plugin_pool.register_plugin(VideoTrackPlugin)
context['instance'] = instance return context inlines = [SocialLinkInline] plugin_pool.register_plugin(MemberPlugin) class TextPlugin(CMSPluginBase): model = Text name = _("Text/HTML Plugin") render_template = "text.html" plugin_pool.unregister_plugin(TextPluginCMS) plugin_pool.register_plugin(TextPlugin) class EditorTextPlugin(TextPluginCMS): name = _("TinyMCE Text Plugin") plugin_pool.register_plugin(EditorTextPlugin) class FilerBackgroundImage(FilerFilePlugin): name = _("Background Image") exclude = ['title', 'target_blank'] render_template = "background_image.html"
def ready(self): def return_pass(self, r, p): pass AliasPlugin.get_extra_global_plugin_menu_items = return_pass AliasPlugin.get_extra_placeholder_menu_items = return_pass plugin_pool.unregister_plugin(AliasPlugin)
from django.utils.translation import ugettext as _ from django.contrib.staticfiles.templatetags.staticfiles import static from cms.plugin_base import CMSPluginBase from cms.models.pluginmodel import CMSPlugin from django.conf import settings import lib.util as util class MyIframePlugin(IframePlugin): text_enabled = True def icon_src(self, instance): return static("filer/icons/video_32x32.png") plugin_pool.unregister_plugin(IframePlugin) plugin_pool.register_plugin(MyIframePlugin) def format_names(names, reverse_first=False, maximum=None): ret = "" ix = 0 name = names[0] if reverse_first: ret += name["surname"] if name["forename"]: ret += ", " + name["forename"] if name["genName"]:
def setUp(self): if CarouselHolderPlugin in plugin_pool.registered_plugins: plugin_pool.unregister_plugin(CarouselHolderPlugin)
('width', 'height'), 'alignment', 'caption_text', 'attributes', ) }), (_('Link settings'), { 'classes': ('collapse',), 'fields': ( ('link_url', 'link_page'), 'link_target', 'link_attributes', ) }), (_('Cropping settings'), { 'classes': ('collapse',), 'fields': ( ('use_automatic_scaling', 'use_no_cropping'), ('use_crop', 'use_upscale'), 'thumbnail_options', ) }) ] def get_render_template(self, context, instance, placeholder): return 'djangocms_versioning_filer/plugins/{}/picture.html'.format(instance.template) plugin_pool.unregister_plugin(BasePicturePlugin) plugin_pool.register_plugin(PicturePlugin)
from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool from django.utils.translation import ugettext as _ from .models import SearchPlugin from haystack.forms import ModelSearchForm class CMSSearchPlugin(CMSPluginBase): model = SearchPlugin name = _("Search") # Name of the plugin render_template = "search/search_form.html" # template to render the plugin with def render(self, context, instance, placeholder): context.update({'form':ModelSearchForm(load_all=True)}) return context try: plugin_pool.unregister_plugin(CMSSearchPlugin) except: pass plugin_pool.register_plugin(CMSSearchPlugin) # register the plugin
def render(self, context, instance, placeholder): context['instance'] = instance return context inlines = [SocialLinkInline] plugin_pool.register_plugin(MemberPlugin) class TextPlugin(CMSPluginBase): model = Text name = _("Text/HTML Plugin") render_template = "text.html" plugin_pool.unregister_plugin(TextPluginCMS) plugin_pool.register_plugin(TextPlugin) class EditorTextPlugin(TextPluginCMS): name = _("TinyMCE Text Plugin") plugin_pool.register_plugin(EditorTextPlugin) class FilerBackgroundImage(FilerFilePlugin): name = _("Background Image") exclude = ['title', 'target_blank'] render_template = "background_image.html" plugin_pool.register_plugin(FilerBackgroundImage)
class FilePlugin(BaseFilePlugin): model = VersionedFile fieldsets = [ (None, { 'fields': ( 'file_grouper', 'file_name', ) }), (_('Advanced settings'), { 'classes': ('collapse', ), 'fields': ( 'template', ('link_target', 'link_title'), 'show_file_size', 'attributes', ) }), ] def get_render_template(self, context, instance, placeholder): return 'djangocms_versioning_filer/plugins/{}/file.html'.format( instance.template) plugin_pool.unregister_plugin(BaseFilePlugin) plugin_pool.register_plugin(FilePlugin)
context['audio_template']) class AudioTrackPlugin(BaseAudioTrackPlugin): model = VersionedAudioTrack fieldsets = [(None, { 'fields': ( 'kind', 'file_grouper', 'srclang', ) }), (_('Advanced settings'), { 'classes': ('collapse', ), 'fields': ( 'label', 'attributes', ) })] def get_render_template(self, context, instance, placeholder): return 'djangocms_versioning_filer/plugins/{}/audio_track.html'.format( context['audio_template']) plugin_pool.unregister_plugin(BaseAudioFilePlugin) plugin_pool.unregister_plugin(BaseAudioTrackPlugin) plugin_pool.register_plugin(AudioFilePlugin) plugin_pool.register_plugin(AudioTrackPlugin)
TEMPLATE_DIRS = getattr(settings, "TEMPLATE_DIRS", ()) + (os.path.join(os.path.dirname(text.__file__), 'templates'),) setattr(settings, 'TEMPLATE_DIRS', TEMPLATE_DIRS) class TextPlugin(TextPluginBase): model = TextWrapper form = TextForm def render(self, context, instance, placeholder): wrappers = [w[1] for w in settings.CMS_TEXT_WRAPPERS if w[0] == instance.wrapper] if wrappers: instance.render_template = wrappers[0].get('render_template') context.update(wrappers[0].get('extra_context', {})) extra_classes = [] for csscls in instance.classes: try: extra_classes.append(dict(instance.CLASSES)[int(csscls)]) except (ValueError, IndexError): pass context['extra_classes'] = ' '.join(extra_classes) context.update({ 'body': plugin_tags_to_user_html(instance.body, context, placeholder), 'placeholder': placeholder, 'object': instance, }) return context plugin_pool.unregister_plugin(TextPluginBase) plugin_pool.register_plugin(TextPlugin)
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] try: plugin_pool.register_plugin(NoCachePlugin) except PluginAlreadyRegistered: pass add_plugin(placeholder1, 'TextPlugin', 'en', body="English") add_plugin(placeholder2, 'TextPlugin', 'en', body="Deutsch") template = "{% load cms_tags %}{% placeholder 'body' %}{% placeholder 'right-column' %}" # 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): # Request the page without the 'no-cache' plugin request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(FuzzyInt(18, 25)): response1 = self.client.get('/en/') content1 = response1.content # Fetch it again, it is cached. request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(0): response2 = self.client.get('/en/') content2 = response2.content self.assertEqual(content1, content2) # Once again with PAGE_CACHE=False, to prove the cache can # be disabled request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.settings(CMS_PAGE_CACHE=False): with self.assertNumQueries(FuzzyInt(5, 24)): response3 = self.client.get('/en/') content3 = response3.content self.assertEqual(content1, content3) # Add the 'no-cache' plugin 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) with self.assertNumQueries(FuzzyInt(4, 6)): output = self.render_template_obj(template, {}, request) with self.assertNumQueries(FuzzyInt(14, 24)): response = self.client.get('/en/') self.assertTrue("no-cache" in response['Cache-Control']) 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) with self.assertNumQueries(4): output2 = self.render_template_obj(template, {}, request) 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(output, output2) self.assertNotEqual(resp1, resp2) plugin_pool.unregister_plugin(NoCachePlugin)
return get_plugin_template(instance, 'link', 'link', get_templates()) def render(self, context, instance, placeholder): link_classes = [] if instance.link_context: if instance.link_type == 'link': link_classes.append('text-{}'.format(instance.link_context)) else: link_classes.append('btn') if not instance.link_outline: link_classes.append('btn-{}'.format(instance.link_context)) else: link_classes.append('btn-outline-{}'.format( instance.link_context)) if instance.link_size: link_classes.append(instance.link_size) if instance.link_block: link_classes.append('btn-block') classes = concat_classes(link_classes + [ instance.attributes.get('class'), ]) instance.attributes['class'] = classes return super(Bootstrap4LinkPlugin, self).render(context, instance, placeholder) plugin_pool.unregister_plugin(LinkPlugin) plugin_pool.register_plugin(Bootstrap4LinkPlugin)
def test_expiration_cache_plugins(self): """ Tests that when used in combination, the page is cached to the shortest TTL. """ 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) try: plugin_pool.register_plugin(DateTimeCacheExpirationPlugin) except PluginAlreadyRegistered: pass try: plugin_pool.register_plugin(NoCachePlugin) except PluginAlreadyRegistered: pass 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') add_plugin(placeholder2, "DateTimeCacheExpirationPlugin", '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, 26)): response = self.client.get('/en/') resp1 = response.content.decode('utf8').split("$$$")[1] self.assertTrue('max-age=40' in response['Cache-Control'], response['Cache-Control']) # noqa cache_control1 = response['Cache-Control'] expires1 = response['Expires'] last_modified1 = response['Last-Modified'] time.sleep(1) # This ensures that the cache has aged measurably # Request it again, this time, it comes from the cache request = self.get_request('/en/') request.current_page = Page.objects.get(pk=page1.pk) request.toolbar = CMSToolbar(request) with self.assertNumQueries(0): response = self.client.get('/en/') resp2 = response.content.decode('utf8').split("$$$")[1] # Content will be the same self.assertEqual(resp2, resp1) # Cache-Control will be different because the cache has aged self.assertNotEqual(response['Cache-Control'], cache_control1) # However, the Expires timestamp will be the same self.assertEqual(response['Expires'], expires1) # As will the Last-Modified timestamp. self.assertEqual(response['Last-Modified'], last_modified1) plugin_pool.unregister_plugin(TTLCacheExpirationPlugin) plugin_pool.unregister_plugin(DateTimeCacheExpirationPlugin) plugin_pool.unregister_plugin(NoCachePlugin)
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(9, 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 tearDown(self): from django.core.cache import cache super().tearDown() plugin_pool.unregister_plugin(VaryCacheOnPlugin) cache.clear()