2011-07-31 10:46:29 +02:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
2013-02-16 16:19:20 +01:00
|
|
|
|
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
|
|
|
|
|
2013-02-16 16:19:20 +01:00
|
|
|
|
:copyright: 2011–2013 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
|
"""
|
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
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):
|
2013-02-16 16:19:20 +01:00
|
|
|
|
def __init__(self, title='', app='', stylefile='', url='', permission=True, selected=False):
|
2012-03-18 17:11:58 +01:00
|
|
|
|
self.title = title
|
2012-09-05 00:20:28 +02:00
|
|
|
|
self.app = app
|
2013-02-16 16:19:20 +01:00
|
|
|
|
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:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
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:
|
2012-11-24 14:01:21 +01:00
|
|
|
|
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,
|
2012-11-24 14:01:21 +01:00
|
|
|
|
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)
|