Example #1
0
def render_inlines(value):
    """
    Renders inlines in a ``Post`` by passing them through inline templates.

    Template Syntax::

        {{ post.body|render_inlines|markdown:"safe" }}

    Inline Syntax (singular)::

        <inline type="<app_name>.<model_name>" id="<id>" class="med_left" />

    Inline Syntax (plural)::

        <inline type="<app_name>.<model_name>" ids="<id>, <id>, <id>" />

    An inline template will be used to render the inline. Templates will be
    located in the following maner:

        ``inline_media/<app_name>_<model_name>.html``

    The template will be passed the following context:

        ``object``
            An object for the corresponding passed id.

    or

        ``object_list``
            A list of objects for the corresponding ids.

    It would be wise to anticipate both object_list and object unless
    you know for sure one or the other will only be present.
    """
    return inlines(value)
 def test_small_with_reversed_default_options(self):
     tmpl = 'inline_media/inline_media.pictureset.default.html'
     positions = ['inline_small_left', 'inline_small_right']
     self._reverse_default_boolean_field_values()
     for cssclass in positions:
         with self.assertRaises(Exception):
             html = inlines(self.tag % cssclass, return_list=False)
 def test_small_with_default_options(self):
     # size disabled in tests.settings.INLINE_MEDIA_CUSTOM_SIZES
     tmpl = 'inline_media/inline_media.pictureset.default.html'
     positions = ['inline_small_left', 'inline_small_right']
     for cssclass in positions:
         with self.assertRaises(Exception):
             html = inlines(self.tag % cssclass, return_list=False)
 def test_small_with_default_options(self):
     tmpl = 'inline_media/inline_media.picture.default.html'
     positions = ['inline_small_left', 'inline_small_right']
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # default options:
         #  - show_as_link = True
         #  - show_description_inline = True
         #  - show_author = False
         #  - show_license = False
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a', attrs={'class':'picture'})
         self.assert_(len(links) == 1)
         descrip = soup.findAll('span', attrs={'class':'inline_description'})
         self.assert_(len(descrip) == 1)
         self.assert_(html.find('inline_author') == -1)
         self.assert_(html.find('inline_license') == -1)
Example #5
0
 def test_medium_with_default_options(self):
     tmpl = 'inline_media/inline_media.picture.default.html'
     positions = ['inline_medium_left', 'inline_medium_right']
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # default options:
         #  - show_as_link = True
         #  - show_description_inline = True
         #  - show_author = False
         #  - show_license = False
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a', attrs={'class': 'picture'})
         self.assert_(len(links) == 1)
         descrip = soup.findAll('span',
                                attrs={'class': 'inline_description'})
         self.assert_(len(descrip) == 1)
         self.assert_(html.find('inline_author') == -1)
         self.assert_(html.find('inline_license') == -1)
Example #6
0
 def test_mini_with_default_options(self):
     tmpl = 'inline_media/inline_media.picture.mini.html'
     positions = ['inline_mini_left', 'inline_mini_right']
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # default options:
         #  - show_as_link = True
         #  - show_description_inline = True
         #  - show_author = False
         #  - show_license = False
         # But! template for size 'mini' never includes
         # picture's author, license or description
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a', attrs={'class': 'picture'})
         self.assert_(len(links) == 1)
         self.assert_(html.find('inline_description') == -1)
         self.assert_(html.find('inline_author') == -1)
         self.assert_(html.find('inline_license') == -1)
 def test_large_with_default_options(self):
     tmpl = 'inline_media/inline_media.pictureset.default.html'
     positions = ['inline_large_left', 'inline_large_right']
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # default options:
         #  - show_description_inline = True
         #  - show_counter = False
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a')
         self.assert_(len(links) == 3)
         # check order
         order = [int(x)-1 for x in self.picset.order.split(",")]
         for idx, link in zip(order, links):
             self.assert_(link['href'] == self.pics[idx].url)
         descrip = soup.findAll('span', attrs={'class':'inline_description'})
         self.assert_(len(descrip) == 1)
         self.assert_(html.find('inline_counter') == -1)
Example #8
0
 def test_large_with_reversed_default_options(self):
     tmpl = 'inline_media/inline_media.picture.default.html'
     positions = ['inline_large_left', 'inline_large_right']
     self._reverse_default_boolean_field_values()
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # reversed default options:
         #  - show_as_link = False
         #  - show_description_inline = False
         #  - show_author = True
         #  - show_license = True
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a', attrs={'class': 'picture'})
         self.assert_(len(links) == 0)
         self.assert_(html.find('inline_description') == -1)
         author = soup.findAll('span', attrs={'class': 'inline_author'})
         self.assert_(len(author) == 1)
         license = soup.findAll('span', attrs={'class': 'inline_license'})
         self.assert_(len(license) == 1)
Example #9
0
 def test_large_with_reversed_default_options(self):
     tmpl = 'inline_media/inline_media.pictureset.default.html'
     positions = ['inline_large_left', 'inline_large_right']
     self._reverse_default_boolean_field_values()
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # default options:
         #  - show_description_inline = False
         #  - show_counter = True
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a')
         self.assert_(len(links) == 3)
         # check order
         order = [int(x)-1 for x in self.picset.order.split(",")]
         for idx, link in zip(order, links):
             self.assert_(link['href'] == self.pics[idx].url)
         self.assert_(html.find('inline_description') == -1)
         counter = soup.findAll('span', attrs={'class': 'inline_counter'})
         self.assert_(len(counter) == 1)
Example #10
0
 def test_full_with_reversed_default_options(self):
     tmpl = 'inline_media/inline_media.pictureset.full.html'
     positions = ['inline_full_left',
                  'inline_full_center',
                  'inline_full_right']
     self._reverse_default_boolean_field_values()
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # default options:
         #  - show_description_inline = False
         #  - show_counter = True
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a')
         self.assert_(len(links) == 3)
         # check order
         order = [int(x)-1 for x in self.picset.order.split(",")]
         for idx, link in zip(order, links):
             self.assert_(link['href'] == self.pics[idx].url)
         self.assert_(html.find('inline_description') == -1)
         counter = soup.findAll('span', attrs={'class': 'inline_counter'})
         self.assert_(len(counter) == 1)
Example #11
0
 def test_full_with_default_options(self):
     tmpl = 'inline_media/inline_media.pictureset.full.html'
     positions = ['inline_full_left',
                  'inline_full_center',
                  'inline_full_right']
     for cssclass in positions:
         self.assert_(tmpl in self._inline_with_css_class(cssclass))
         # default options:
         #  - show_description_inline = True
         #  - show_counter = False
         html = inlines(self.tag % cssclass, return_list=False)
         soup = BeautifulSoup(html)
         links = soup.findAll('a')
         self.assertEqual(len(links), 3)
         # check order
         order = [int(x)-1 for x in self.picset.order.split(",")]
         for idx, link in zip(order, links):
             self.assert_(link['href'] == self.pics[idx].url)
         descrip = soup.findAll('span',
                                attrs={'class': 'inline_description'})
         self.assert_(len(descrip) == 1)
         self.assert_(html.find('inline_counter') == -1)
Example #12
0
 def test_small_with_default_options(self):
     # size disabled in tests.settings.INLINE_MEDIA_CUSTOM_SIZES
     positions = ['inline_small_left', 'inline_small_right']
     for cssclass in positions:
         with self.assertRaises(Exception):
             inlines(self.tag % cssclass, return_list=False)
Example #13
0
 def test_mini_with_default_options(self):
     # size disabled in tests.settings.INLINE_MEDIA_CUSTOM_SIZES
     positions = ['inline_mini_left', 'inline_mini_right']
     for cssclass in positions:
         with self.assertRaises(Exception):
             inlines(self.tag % cssclass, return_list=False)
Example #14
0
 def test_inlines_with_return_list_false(self):
     html_content = inlines(self.tag, return_list=False)
     self.assertEqual(
         '<div class="inline_small_left"><H3>The Title</H3><p>Blah blah ...</p></div>\n',
         html_content)
Example #15
0
 def item_description(self, item):
     return inlines(item.body)
    def render(self, context):
        if self.is_variable:
            real_slug = template.Variable(self.slug).resolve(context)
        else:
            real_slug = self.slug
        if isinstance(self.template_name, template.Variable):
            real_template = self.template_name.resolve(context)
        else:
            real_template = self.template_name
        # Eventually we want to pass the whole context to the template so that
        # users have the maximum of flexibility of what to do in there.
        if self.with_template:
            new_ctx = template.Context({})
            new_ctx.update(context)
        try:
            flatblock = None
            if self.cache_time != 0:
                cache_key = settings.CACHE_PREFIX + real_slug
                flatblock = cache.get(cache_key)
            if flatblock is None:

                # if flatblock's slug is hard-coded in template then it is
                # safe and convenient to auto-create block if it doesn't exist.
                # This behaviour can be configured using the
                # FLATBLOCKS_XTD_AUTOCREATE_STATIC_BLOCKS setting
                if self.is_variable or not settings.AUTOCREATE_STATIC_BLOCKS:
                    flatblock = FlatBlockXtd.objects.get(slug=real_slug)
                else:
                    flatblock, _ = FlatBlockXtd.objects.get_or_create(
                                      slug=real_slug,
                                      defaults = {'content': real_slug}
                                   )
                if self.cache_time != 0:
                    if self.cache_time is None or self.cache_time == 'None':
                        logger.debug("Caching %s for the cache's default "
                                     "timeout" % (real_slug,))
                        cache.set(cache_key, flatblock)
                    else:
                        logger.debug("Caching %s for %s seconds" % 
                                     (real_slug, str(self.cache_time)))
                        cache.set(cache_key, flatblock, int(self.cache_time))
                else:
                    logger.debug("Don't cache %s" % (real_slug,))

            if self.evaluated:
                flatblock.raw_content = flatblock.content
                flatblock.raw_header = flatblock.header
                flatblock.content = self._evaluate(flatblock.content, context)
                flatblock.header = self._evaluate(flatblock.header, context)

            if self.with_template:
                tmpl = loader.get_template(real_template)
                new_ctx.update({'flatblock':flatblock})
                return tmpl.render(new_ctx)
            else:
                return inlines(flatblock.content)
        except template.TemplateDoesNotExist:
            raise template.TemplateSyntaxError("Template '{0}' does not exist"
                                               "".format(real_template))
        except FlatBlockXtd.DoesNotExist:
            return ''
Example #17
0
 def item_description(self, item):
     child = getattr(item, item.content_type.model)
     if hasattr(child, "abstract"):
         return inlines(child.abstract_markup)
     return inlines(child.body_markup)
Example #18
0
 def test_inlines_with_return_list_true(self):
     inline_list = inlines(self.tag, return_list=True)
     self.assert_(len(inline_list) == 1)
     self.assert_(inline_list[0]["object"] == self.obj)
     self.assert_(inline_list[0]["class"] == u'inline_small_left')
     self.assert_(inline_list[0]["content_type"] == u'tests.mediamodeltest')
Example #19
0
 def item_description(self, item):
     return inlines(item.content_object.body)
Example #20
0
 def item_description(self, item):
     if item.content_type.model == "story":
         if item.object.abstract:
             return inlines(item.object.abstract_markup)
     return inlines(item.object.body_markup)
Example #21
0
 def item_description(self, item):
     if item.abstract:
         return inlines(item.abstract_markup)
     return inlines(item.body_markup)
Example #22
0
def extract_inlines(value):
    return inlines(value, True)
Example #23
0
 def item_description(self, item):
     if item.abstract:
         return inlines(item.abstract_markup)
     return inlines(item.body_markup)
Example #24
0
 def test_small_with_reversed_default_options(self):
     positions = ['inline_small_left', 'inline_small_right']
     self._reverse_default_boolean_field_values()
     for cssclass in positions:
         with self.assertRaises(Exception):
             inlines(self.tag % cssclass, return_list=False)
Example #25
0
 def item_description(self, item):
     return inlines(item.object.body)
Example #26
0
 def test_inlines_with_return_list_false(self):
     html_content = inlines(self.tag, return_list=False)
     self.assertEqual(('<div class="inline_small_left"><h3>The Title</h3>'
                       '<p>Blah blah ...</p></div>\n'), html_content)
Example #27
0
 def test_inlines_with_return_list_true(self):
     inline_list = inlines(self.tag, return_list=True)
     self.assert_(len(inline_list) == 1)
     self.assert_(inline_list[0]["object"] == self.obj)
     self.assert_(inline_list[0]["class"] == 'inline_small_left')
     self.assert_(inline_list[0]["content_type"] == 'tests.modelmediatest')
Example #28
0
 def item_description(self, item):
     child = getattr(item, item.content_type.model)
     if hasattr(child, "abstract"):
         return inlines(child.abstract_markup)
     return inlines(child.body_markup)
Example #29
0
 def item_description(self, item):
     if item.content_type.model == "story":
         if item.object.abstract:
             return inlines(item.object.abstract_markup)
     return inlines(item.object.body_markup)