Utilities

Prefilled attributes

The two functions prefilled_attribute and prefill_entry_list help you avoid massive amounts of database queries when displaying a list of CMS items with content objects. This is especially useful if f.e. your blog content is derived from FeinCMS and you want to show a list of recent blog entries.

Example:

from django.utils.translation import ugettext_lazy as _

from feincms.content.image.models import ImageContent
from feincms.content.richtext.models import RichTextContent
from feincms.models import Base
from feincms.utils import prefilled_attribute, prefill_entry_list

class Author(models.Model):
    # ...

class Entry(Base):
    authors = models.ManyToManyField

    author_list = prefilled_attr('authors')
    richtexts = prefilled_attr('richtextcontent_set')
    images = prefilled_attr('imagecontent_set')

Entry.create_content_type(RichTextContent)
Entry.create_content_type(ImageContent, POSITION_CHOICES=(
    ('block', _('block')),
    ('left', _('left')),
    ('right', _('right')),
    )

Then, inside your view function or inside a template tag, call prefill_entry_list with the attribute names:

prefill_entry_list(queryset, 'authors', 'richtextcontent_set', 'imagecontent_set')

or:

{% load feincms_tags %}
{% feincms_prefill_entry_list object_list "authors,richtextcontent_set,imagecontent_set" %}
feincms.utils.copy_model_instance(obj, exclude=None)

Copy a model instance, excluding primary key and optionally a list of specified fields.

feincms.utils.prefill_entry_list(queryset, *attrs, **kwargs)

Prefill a queryset with related data. Instead of querying the related tables over and over for every single entry of the queryset, the absolute minimum of queries is performed per related field, one for reverse foreign keys, two for many to many fields. The returned data is assigned to the individual entries afterwards, where it can be made easily accessible by using the prefilled_attribute property generator above.

You may optionally pass a region argument here, which will be applied to reverse foreign key relations. This is obviously most useful for fetching content objects.

feincms.utils.shorten_string(str, max_length=50)

Shorten a string for display, truncate it intelligently when too long. Try to cut it in 2/3 + ellipsis + 1/3 of the original title. The first part also try to cut at white space instead of in mid-word.

HTML utilities

feincms.utils.html.cleanse.cleanse_html(html)

Clean HTML code from ugly copy-pasted CSS and empty elements

Removes everything not explicitly allowed in cleanse_html_allowed.

Requires lxml and beautifulsoup.

feincms.utils.html.tidy.tidy_html(html)

Process an input string containing HTML and return a tuple (xhtml, errors, warnings) containing the output of tidylib and lists of validation errors and warnings.

Input must be unicode. Output will be valid XHTML.

Template tag helpers

I really hate repeating myself. These are helpers that avoid typing the whole thing over and over when implementing additional template tags

They help implementing tags of the following forms:

{% tag as var_name %}
{% tag of template_var as var_name %}
{% tag of template_var as var_name arg1,arg2,kwarg3=4 %}

Table Of Contents

Previous topic

Translations

Next topic

Views and decorators

This Page