OpenSlides/openslides/utils/template.py

84 lines
2.6 KiB
Python
Raw Normal View History

2011-07-31 10:46:29 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
openslides.utils.template
~~~~~~~~~~~~~~~~~~~~~~~~~
2011-07-31 10:46:29 +02:00
2012-04-25 22:29:19 +02:00
Useful template functions for OpenSlides.
2011-07-31 10:46:29 +02:00
:copyright: 20112013 by OpenSlides team, see AUTHORS.
2011-07-31 10:46:29 +02:00
:license: GNU GPL, see LICENSE for more details.
"""
from django.template import loader, Context
2012-07-10 13:19:12 +02:00
from django.template.loader_tags import BlockNode, ExtendsNode
2011-07-31 10:46:29 +02:00
2012-03-18 17:11:58 +01:00
class Tab(object):
def __init__(self, title='', app='', stylefile='', url='', permission=True, selected=False):
2012-03-18 17:11:58 +01:00
self.title = title
self.app = app
self.stylefile = stylefile
self.url = url
2012-03-18 17:11:58 +01:00
self.permission = permission
self.selected = selected
2012-11-24 01:42:10 +01:00
## All following function are only needed to render a block from a template
## and could be removed, if the template worked with an include-statement instead.
## Its only used for ajax-request from the projector.
2011-07-31 10:46:29 +02:00
def get_template(template):
if isinstance(template, (tuple, list)):
return loader.select_template(template)
return loader.get_template(template)
2012-03-18 17:11:58 +01:00
2011-07-31 10:46:29 +02:00
class BlockNotFound(Exception):
pass
2012-03-18 17:11:58 +01:00
2011-07-31 10:46:29 +02:00
def render_template_block(template, block, context):
"""
2012-07-10 13:19:12 +02:00
Renders a single block from a template. This template should have previously
been rendered.
2011-07-31 10:46:29 +02:00
"""
return render_template_block_nodelist(template.nodelist, block, context)
2012-03-18 17:11:58 +01:00
2011-07-31 10:46:29 +02:00
def render_template_block_nodelist(nodelist, block, context):
for node in nodelist:
if isinstance(node, BlockNode) and node.name == block:
return node.render(context)
for key in ('nodelist', 'nodelist_true', 'nodelist_false'):
if hasattr(node, key):
try:
return render_template_block_nodelist(
getattr(node, key), block, context)
2011-07-31 10:46:29 +02:00
except:
pass
for node in nodelist:
if isinstance(node, ExtendsNode):
try:
return render_template_block(
node.get_parent(context), block, context)
2011-07-31 10:46:29 +02:00
except BlockNotFound:
pass
raise BlockNotFound
2012-03-18 17:11:58 +01:00
2012-07-10 13:19:12 +02:00
def render_block_to_string(template_name, block, dictionary=None,
context_instance=None):
2011-07-31 10:46:29 +02:00
"""
2012-07-10 13:19:12 +02:00
Loads the given template_name and renders the given block with the given
dictionary as context. Returns a string.
2011-07-31 10:46:29 +02:00
"""
dictionary = dictionary or {}
t = get_template(template_name)
if context_instance:
context_instance.update(dictionary)
else:
context_instance = Context(dictionary)
t.render(context_instance)
return render_template_block(t, block, context_instance)