def test_render_timeout(self): """ The ``render_placeholder`` should detect the maximum timeout that can be used for caching. """ # Attach contents to the parent object. page2 = TestPage.objects.create(pk=9, contents="TEST2!") placeholder2 = Placeholder.objects.create_for_object(page2, 'slot2') RawHtmlTestItem.objects.create_for_placeholder(placeholder2, html='<b>Item1!</b>', sort_order=1) output = rendering.render_placeholder(self.dummy_request, placeholder2, parent_object=page2, cachable=False) self.assertEqual(output.html, '<b>Item1!</b>') self.assertEqual(output.cache_timeout, DEFAULT_TIMEOUT) item2 = TimeoutTestItem.objects.create_for_placeholder( placeholder2, html='<b>Item2!</b>', sort_order=1) output = rendering.render_placeholder(self.dummy_request, placeholder2, parent_object=page2, cachable=False) self.assertEqual(output.html, '<b>Item1!</b><b>Item2!</b>') self.assertEqual( output.cache_timeout, 60 ) # this is that timeout that should be used for the placeholder cache item.
def get_value(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) # Parse arguments try: placeholder = _get_placeholder_arg(self.args[0], tag_args[0]) except RuntimeWarning as e: return u"<!-- {0} -->".format(e) template_name = tag_kwargs.get('template', None) cachable = is_true(tag_kwargs.get('cachable', not bool(template_name))) # default: True unless there is a template. fallback_language = is_true(tag_kwargs.get('fallback', False)) if template_name and cachable and not extract_literal(self.kwargs['template']): # If the template name originates from a variable, it can change any time. # See PagePlaceholderNode.render_tag() why this is not allowed. raise TemplateSyntaxError("{0} tag does not allow 'cachable' for variable template names!".format(self.tag_name)) # Fetching placeholder.parent should not cause queries if fetched via PlaceholderFieldDescriptor. # See render_placeholder() for more details output = rendering.render_placeholder(request, placeholder, placeholder.parent, template_name=template_name, cachable=cachable, limit_parent_language=True, fallback_language=fallback_language ) rendering.register_frontend_media(request, output.media) # Need to track frontend media here, as the template tag can't return it. return output.html
def render(self, request, instance, **kwargs): # Not using "template" parameter yet. shared_content = instance.shared_content return mark_safe( render_placeholder(request, shared_content.contents, parent_object=shared_content))
def render(self, context): request = _get_request(context) placeholder = self.placeholder_expr.resolve(context) if placeholder is None: return "<!-- placeholder object is None -->" elif isinstance(placeholder, Placeholder): pass elif isinstance(placeholder, basestring): slot = placeholder try: placeholder = Placeholder.objects.get_by_slot(None, slot) except Placeholder.DoesNotExist: return "<!-- global placeholder '{0}' does not yet exist -->".format( slot) elif isinstance(placeholder, Manager): try: placeholder = placeholder.all()[0] except IndexError: return "<!-- No placeholders found for query -->".format( self.placeholder_expr) else: raise ValueError( "The field '{0}' does not refer to a placeholder or slotname!". format(self.placeholder_expr)) return rendering.render_placeholder(request, placeholder)
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) (placeholder, ) = tag_args if placeholder is None: return "<!-- placeholder object is None -->" elif isinstance(placeholder, Placeholder): pass elif isinstance(placeholder, basestring): # This feature only exists at database level, the "sharedcontent" plugin solves this issue. slot = placeholder try: placeholder = Placeholder.objects.get_by_slot(None, slot) except Placeholder.DoesNotExist: return "<!-- global placeholder '{0}' does not yet exist -->".format( slot) elif isinstance(placeholder, Manager): try: placeholder = placeholder.all()[0] except IndexError: return "<!-- No placeholders found for query -->".format( self.args[0]) else: raise ValueError( "The field '{0}' does not refer to a placeholder or slotname!". format(self.args[0])) return rendering.render_placeholder(request, placeholder)
def preview_canvas(self, request, pk): # Avoid the proxy model stuff, allow both to work. page = self.get_base_object(pk) return render(request, 'admin/pages/preview_canvas.html', { 'page': page, 'body': mark_safe(render_placeholder(request, page.body).html) })
def render(self, context): request = self.get_request(context) # Get the placeholder parent = self.parent_expr.resolve(context) slot = self.slot_expr.resolve(context) fallback_language = self._is_true(self.fallback_expr.resolve( context)) if self.fallback_expr else False try: placeholder = Placeholder.objects.get_by_slot(parent, slot) except Placeholder.DoesNotExist: return "<!-- placeholder '{0}' does not yet exist -->".format(slot) template_name = self.template_expr.resolve( context) if self.template_expr else None output = rendering.render_placeholder( request, placeholder, parent, template_name=template_name, fallback_language=fallback_language) rendering.register_frontend_media( request, output.media ) # Assume it doesn't hurt. TODO: should this be optional? return output.html
def render(self, request, instance, **kwargs): # Not using "template" parameter yet of render_placeholder(). # The render_placeholder() returns a ContentItemOutput object, which contains both the media and HTML code. # Hence, no mark_safe() or escaping is applied here. shared_content = instance.shared_content return render_placeholder( request, shared_content.contents, parent_object=shared_content, fallback_language=True )
def preview_canvas(self, request, pk): # Avoid the proxy model stuff, allow both to work. page = self.get_base_object(pk) return render( request, 'admin/pages/preview_canvas.html', { 'page': page, 'body': mark_safe(render_placeholder(request, page.body).html) })
def render_shared_content(self, request, sharedcontent, template_name=None, cachable=None): # All parsing done, perform the actual rendering placeholder = sharedcontent.contents # Another DB query return rendering.render_placeholder(request, placeholder, sharedcontent, template_name=template_name, cachable=cachable, fallback_language=True )
def test_render_timeout(self): """ The ``render_placeholder`` should detect the maximum timeout that can be used for caching. """ # Attach contents to the parent object. page2 = TestPage.objects.create(pk=9, contents="TEST2!") placeholder2 = Placeholder.objects.create_for_object(page2, 'slot2') RawHtmlTestItem.objects.create_for_placeholder(placeholder2, html='<b>Item1!</b>', sort_order=1) output = rendering.render_placeholder(self.dummy_request, placeholder2, parent_object=page2, cachable=False) self.assertEqual(output.html, '<b>Item1!</b>') self.assertEqual(output.cache_timeout, DEFAULT_TIMEOUT) item2 = TimeoutTestItem.objects.create_for_placeholder(placeholder2, html='<b>Item2!</b>', sort_order=1) output = rendering.render_placeholder(self.dummy_request, placeholder2, parent_object=page2, cachable=False) self.assertEqual(output.html, '<b>Item1!</b><b>Item2!</b>') self.assertEqual(output.cache_timeout, 60) # this is that timeout that should be used for the placeholder cache item.
def render(self, request, instance, **kwargs): # Not using "template" parameter yet of render_placeholder(). # The render_placeholder() returns a ContentItemOutput object, which contains both the media and HTML code. # Hence, no mark_safe() or escaping is applied here. shared_content = instance.shared_content return render_placeholder(request, shared_content.contents, parent_object=shared_content, fallback_language=True)
def test_render_media(self): """ Test that 'class FrontendMedia' works. """ placeholder = factories.create_placeholder() factories.create_content_item(MediaTestItem, placeholder=placeholder, html='MEDIA_TEST') output = rendering.render_placeholder(self.dummy_request, placeholder) self.assertEqual(output.html.strip(), 'MEDIA_TEST') self.assertEqual(output.media._js, ['testapp/media_item.js']) self.assertEqual(output.media._css, {'screen': ['testapp/media_item.css']})
def test_render_media(self): """ Test that 'class FrontendMedia' works. """ placeholder = factories.create_placeholder() factories.create_content_item(MediaTestItem, placeholder=placeholder, html="MEDIA_TEST") output = rendering.render_placeholder(self.dummy_request, placeholder) self.assertEqual(output.html.strip(), "MEDIA_TEST") self.assertEqual(output.media._js, ["testapp/media_item.js"]) self.assertEqual(output.media._css, {"screen": ["testapp/media_item.css"]})
def render(self, context): request = _get_request(context) # Get the placeholder parent = self.parent_expr.resolve(context) slot = self.slot_expr.resolve(context) try: placeholder = Placeholder.objects.get_by_slot(parent, slot) except Placeholder.DoesNotExist: return "<!-- placeholder '{0}' does not yet exist -->".format(slot) return rendering.render_placeholder(request, placeholder, parent)
def render(self, context): request = get_request_var(context) # Get the placeholder slot = self.slot_expr.resolve(context) try: sharedcontent = SharedContent.objects.get(slug=slot) except SharedContent.DoesNotExist: return "<!-- shared content '{0}' does not yet exist -->".format(slot) template_name = self.template_expr.resolve(context) if self.template_expr else None return rendering.render_placeholder(request, sharedcontent.contents, sharedcontent, template_name=template_name)
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) (slot,) = tag_args # Get the placeholder try: sharedcontent = SharedContent.objects.get(slug=slot) except SharedContent.DoesNotExist: return "<!-- shared content '{0}' does not yet exist -->".format(slot) template_name = tag_kwargs.get('template') or None return rendering.render_placeholder(request, sharedcontent.contents, sharedcontent, template_name=template_name, fallback_language=True)
def get_value(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) output = None # Process arguments parent, slot = tag_args template_name = tag_kwargs.get("template", None) # cachable default is True unless there is a template. cachable = is_true(tag_kwargs.get("cachable", not bool(template_name))) fallback_language = is_true(tag_kwargs.get("fallback", False)) if template_name and cachable and not extract_literal(self.kwargs["template"]): # If the template name originates from a variable, it can change any time. # It's not possible to create a reliable output cache for for that, # as it would have to include any possible template name in the key. raise TemplateSyntaxError( "{} tag does not allow 'cachable' for variable template names!".format( self.tag_name ) ) if ( appsettings.FLUENT_CONTENTS_CACHE_OUTPUT and appsettings.FLUENT_CONTENTS_CACHE_PLACEHOLDER_OUTPUT and cachable ): # See if the entire placeholder output is cached, # if so, no database queries have to be performed. # This will be omitted when an template is used, # because there is no way to expire that or tell whether that template is cacheable. output = get_cached_placeholder_output(parent, slot) if output is None: # Get the placeholder try: placeholder = Placeholder.objects.get_by_slot(parent, slot) except Placeholder.DoesNotExist: return f"<!-- placeholder '{slot}' does not yet exist -->" output = rendering.render_placeholder( request, placeholder, parent, template_name=template_name, cachable=cachable, limit_parent_language=True, fallback_language=fallback_language, ) # Assume it doesn't hurt to register media. TODO: should this be optional? rendering.register_frontend_media(request, output.media) return output.html
def render(self, context): request = get_request_var(context) # Get the placeholder parent = self.parent_expr.resolve(context) slot = self.slot_expr.resolve(context) try: placeholder = Placeholder.objects.get_by_slot(parent, slot) except Placeholder.DoesNotExist: return "<!-- placeholder '{0}' does not yet exist -->".format(slot) template_name = self.template_expr.resolve(context) if self.template_expr else None return rendering.render_placeholder(request, placeholder, parent, template_name=template_name)
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) try: placeholder = _get_placeholder_arg(self.args[0], tag_args[0]) except RuntimeWarning as e: return u"<!-- {0} -->".format(e) # To support filtering the placeholders by parent language, the parent object needs to be known. # Fortunately, the PlaceholderFieldDescriptor makes sure this doesn't require an additional query. parent_object = placeholder.parent output = rendering.render_placeholder(request, placeholder, parent_object) rendering.register_frontend_media(request, output.media) # Assume it doesn't hurt. TODO: should this be optional? return output.html
def render(self, context): request = self.get_request(context) # Get the placeholder parent = self.parent_expr.resolve(context) slot = self.slot_expr.resolve(context) try: placeholder = Placeholder.objects.get_by_slot(parent, slot) except Placeholder.DoesNotExist: return "<!-- placeholder '{0}' does not yet exist -->".format(slot) template_name = self.template_expr.resolve(context) if self.template_expr else None output = rendering.render_placeholder(request, placeholder, parent, template_name=template_name) rendering.register_frontend_media(request, output.media) # Assume it doesn't hurt. TODO: should this be optional? return output.html
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) (slot, ) = tag_args # Get the placeholder try: sharedcontent = SharedContent.objects.get(slug=slot) except SharedContent.DoesNotExist: return "<!-- shared content '{0}' does not yet exist -->".format( slot) template_name = tag_kwargs.get('template') or None return rendering.render_placeholder(request, sharedcontent.contents, sharedcontent, template_name=template_name)
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) (slot,) = tag_args if isinstance(slot, SharedContent): # Allow passing a sharedcontent, just like 'render_placeholder' does. sharedcontent = slot else: # Get the placeholder try: site = Site.objects.get_current() sharedcontent = SharedContent.objects.parent_site(site).get(slug=slot) except SharedContent.DoesNotExist: return "<!-- shared content '{0}' does not yet exist -->".format(slot) template_name = tag_kwargs.get('template') or None return rendering.render_placeholder(request, sharedcontent.contents, sharedcontent, template_name=template_name, fallback_language=True)
def render(self, context): request = self.get_request(context) # Get the placeholder parent = self.parent_expr.resolve(context) slot = self.slot_expr.resolve(context) try: placeholder = Placeholder.objects.get_by_slot(parent, slot) except Placeholder.DoesNotExist: return "<!-- placeholder '{0}' does not yet exist -->".format(slot) template_name = self.template_expr.resolve( context) if self.template_expr else None return rendering.render_placeholder(request, placeholder, parent, template_name=template_name)
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) try: placeholder = _get_placeholder_arg(self.args[0], tag_args[0]) except RuntimeWarning as e: return u"<!-- {0} -->".format(e) # To support filtering the placeholders by parent language, the parent object needs to be known. # Fortunately, the PlaceholderFieldDescriptor makes sure this doesn't require an additional query. parent_object = placeholder.parent output = rendering.render_placeholder(request, placeholder, parent_object) rendering.register_frontend_media( request, output.media ) # Assume it doesn't hurt. TODO: should this be optional? return output.html
def get_value(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) output = None # Process arguments parent, slot = tag_args template_name = tag_kwargs.get('template', None) cachable = is_true(tag_kwargs.get('cachable', not bool(template_name))) # default: True unless there is a template. fallback_language = is_true(tag_kwargs.get('fallback', False)) if template_name and cachable and not extract_literal(self.kwargs['template']): # If the template name originates from a variable, it can change any time. # It's not possible to create a reliable output cache for for that, # as it would have to include any possible template name in the key. raise TemplateSyntaxError("{0} tag does not allow 'cachable' for variable template names!".format(self.tag_name)) if appsettings.FLUENT_CONTENTS_CACHE_OUTPUT \ and appsettings.FLUENT_CONTENTS_CACHE_PLACEHOLDER_OUTPUT \ and cachable: # See if the entire placeholder output is cached, # if so, no database queries have to be performed. # This will be omitted when an template is used, # because there is no way to expire that or tell whether that template is cacheable. output = get_cached_placeholder_output(parent, slot) if output is None: # Get the placeholder try: placeholder = Placeholder.objects.get_by_slot(parent, slot) except Placeholder.DoesNotExist: return "<!-- placeholder '{0}' does not yet exist -->".format(slot) output = rendering.render_placeholder(request, placeholder, parent, template_name=template_name, cachable=cachable, limit_parent_language=True, fallback_language=fallback_language ) rendering.register_frontend_media(request, output.media) # Assume it doesn't hurt. TODO: should this be optional? return output.html
def render(self, context): request = _get_request(context) placeholder = self.placeholder_expr.resolve(context) if placeholder is None: return "<!-- placeholder object is None -->" elif isinstance(placeholder, Placeholder): pass elif isinstance(placeholder, basestring): slot = placeholder try: placeholder = Placeholder.objects.get_by_slot(None, slot) except Placeholder.DoesNotExist: return "<!-- global placeholder '{0}' does not yet exist -->".format(slot) elif isinstance(placeholder, Manager): try: placeholder = placeholder.all()[0] except IndexError: return "<!-- No placeholders found for query -->".format(self.placeholder_expr) else: raise ValueError("The field '{0}' does not refer to a placeholder or slotname!".format(self.placeholder_expr)) return rendering.render_placeholder(request, placeholder)
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) (slot, ) = tag_args if isinstance(slot, SharedContent): # Allow passing a sharedcontent, just like 'render_placeholder' does. sharedcontent = slot else: # Get the placeholder try: site = Site.objects.get_current() sharedcontent = SharedContent.objects.parent_site(site).get( slug=slot) except SharedContent.DoesNotExist: return "<!-- shared content '{0}' does not yet exist -->".format( slot) template_name = tag_kwargs.get('template') or None return rendering.render_placeholder(request, sharedcontent.contents, sharedcontent, template_name=template_name, fallback_language=True)
def render_tag(self, context, *tag_args, **tag_kwargs): request = self.get_request(context) (placeholder,) = tag_args if placeholder is None: return "<!-- placeholder object is None -->" elif isinstance(placeholder, Placeholder): pass elif isinstance(placeholder, basestring): # This feature only exists at database level, the "sharedcontent" plugin solves this issue. slot = placeholder try: placeholder = Placeholder.objects.get_by_slot(None, slot) except Placeholder.DoesNotExist: return "<!-- global placeholder '{0}' does not yet exist -->".format(slot) elif isinstance(placeholder, Manager): try: placeholder = placeholder.all()[0] except IndexError: return "<!-- No placeholders found for query -->".format(self.args[0]) else: raise ValueError("The field '{0}' does not refer to a placeholder or slotname!".format(self.args[0])) return rendering.render_placeholder(request, placeholder)
def get_meta_description(self, **kwargs): request = kwargs.get('request') s = MLStripper() s.feed(mark_safe(render_placeholder(request, self.contents).html)) return truncatechars(s.get_data(), 250)
def to_native(self, obj): request = self.context.get('request', None) contents_html = render_placeholder(request, obj) contents_html = Template(contents_html).render(Context({})) return contents_html
def to_native(self, obj): request = self.context.get('request', None) contents_html = render_placeholder(request, obj) return contents_html
def to_representation(self, obj): request = self.context.get('request', None) contents_html = mark_safe(render_placeholder(request, obj).html) return contents_html
def render_shared_content(self, request, sharedcontent, template_name): # All parsing done, perform the actual rendering output = rendering.render_placeholder(request, sharedcontent.contents, sharedcontent, template_name=template_name, fallback_language=True) rendering.register_frontend_media(request, output.media) # Need to track frontend media here, as the template tag can't return it. return output.html
def get_meta_description(self, **kwargs): request = kwargs.get('request') s = MLStripper() s.feed(mark_safe(render_placeholder(request, self.body).html)) return truncatechars(s.get_data(), 200)
def get_meta_description(self, **kwargs): request = kwargs.get('request') s = MLStripper() s.feed(render_placeholder(request, self.contents)) return truncatechars(s.get_data(), 250)
def render(self, request, instance, **kwargs): # Not using "template" parameter yet. shared_content = instance.shared_content return mark_safe(render_placeholder(request, shared_content.contents, parent_object=shared_content))