diff --git a/apps/common/templates/generic_detail.html b/apps/common/templates/generic_detail.html index dba9f840dc..e201229699 100644 --- a/apps/common/templates/generic_detail.html +++ b/apps/common/templates/generic_detail.html @@ -1,5 +1,6 @@ {% extends "base.html" %} {% load i18n %} +{% load subtemplates_tags %} {% block title %} :: {% with "true" as read_only %}{% with "true" as striptags %}{% include "calculate_form_title.html" %}{% endwith %}{% endwith %}{% endblock %} @@ -91,15 +92,7 @@ {% endwith %} {% endfor %} - {% for subtemplate in subtemplates_dict %} - {% with subtemplate.title as title %} - {% with subtemplate.object_list as object_list %} - {% with subtemplate.extra_columns as extra_columns %} - {% with subtemplate.hide_object as hide_object %} - {% with subtemplate.main_object as main_object %} - {% with subtemplate.hide_link as hide_link %} - {% with subtemplate.hide_header as hide_header %} - {% with subtemplate.navigation_object_links as navigation_object_links %} + {% for subtemplate in subtemplates_list %}
@@ -111,7 +104,7 @@ {% with subtemplate.form_action as form_action %} {% with "true" as read_only %} {% with subtemplate.form as form %} -
+
{% include subtemplate.name %}
{% endwith %} @@ -122,20 +115,13 @@ {% endwith %} {% endwith %} {% else %} - {% include subtemplate.name %} + {% render_subtemplate subtemplate.name subtemplate.context as rendered_subtemplate %} + {{ rendered_subtemplate }} {% endif %}
{% if subtemplate.grid_clear or not subtemplate.grid %}
{% endif %} - {% endwith %} - {% endwith %} - {% endwith %} - {% endwith %} - {% endwith %} - {% endwith %} - {% endwith %} - {% endwith %} {% endfor %}
{% endblock %} diff --git a/apps/common/templatetags/subtemplates_tags.py b/apps/common/templatetags/subtemplates_tags.py new file mode 100644 index 0000000000..157a7f0c89 --- /dev/null +++ b/apps/common/templatetags/subtemplates_tags.py @@ -0,0 +1,51 @@ +import re + +from django.template import Node, TemplateSyntaxError, Library, \ + Variable, Context +from django.template.loader import get_template + +register = Library() + + +class RenderSubtemplateNode(Node): + def __init__(self, template_name, template_context, var_name): + self.template_name = template_name + self.template_context = template_context + self.var_name = var_name + + def render(self, context): + template_name = Variable(self.template_name).resolve(context) + template_context = Variable(self.template_context).resolve(context) + + new_context = Context(context) + new_context.update(Context(template_context, autoescape=context.autoescape)) + + csrf_token = context.get('csrf_token', None) + if csrf_token is not None: + new_context['csrf_token'] = csrf_token + + context[self.var_name] = get_template(template_name).render(new_context) + return '' + + +@register.tag +def render_subtemplate(parser, token): + # This version uses a regular expression to parse tag contents. + try: + # Splitting by None == splitting by spaces. + tag_name, arg = token.contents.split(None, 1) + except ValueError: + raise TemplateSyntaxError('%r tag requires arguments' % token.contents.split()[0]) + m = re.search(r'(.*?) (.*?) as (\w+)', arg) + if not m: + raise TemplateSyntaxError('%r tag had invalid arguments' % tag_name) + template_name, template_context, var_name = m.groups() + + if (template_name[0] == template_name[-1] and template_name[0] in ('"', "'")): + raise TemplateSyntaxError('%r tag\'s template name argument should not be in quotes' % tag_name) + + if (template_context[0] == template_context[-1] and template_context[0] in ('"', "'")): + raise TemplateSyntaxError('%r tag\'s template context argument should not be in quotes' % tag_name) + + return RenderSubtemplateNode(template_name, template_context, var_name) + #format_string[1:-1] diff --git a/apps/documents/views.py b/apps/documents/views.py index 1556358fd8..d222b2ac80 100644 --- a/apps/documents/views.py +++ b/apps/documents/views.py @@ -70,7 +70,7 @@ def document_list(request, object_list=None, title=None): check_permissions(request.user, 'documents', [PERMISSION_DOCUMENT_VIEW]) return render_to_response('generic_list.html', { - 'object_list': object_list if object_list else Document.objects.only('file_filename', 'file_extension').all(), + 'object_list': object_list if not (object_list is None) else Document.objects.only('file_filename', 'file_extension').all(), 'title': title if title else _(u'documents'), 'multi_select_as_buttons': True, 'hide_links': True, @@ -287,17 +287,19 @@ def document_view(request, document_id): }, ] - subtemplates_dict = [] + subtemplates_list = [] if document.tags.count(): - subtemplates_dict.append(get_tags_subtemplate(document)) + subtemplates_list.append(get_tags_subtemplate(document)) - subtemplates_dict.append( + subtemplates_list.append( { 'name': 'generic_list_subtemplate.html', - 'title': _(u'metadata'), - 'object_list': document.documentmetadata_set.all(), - 'extra_columns': [{'name': _(u'value'), 'attribute': 'value'}], - 'hide_link': True, + 'context': { + 'title': _(u'metadata'), + 'object_list': document.documentmetadata_set.all(), + 'extra_columns': [{'name': _(u'value'), 'attribute': 'value'}], + 'hide_link': True, + } }, ) @@ -312,31 +314,35 @@ def document_view(request, document_id): metadata_groups = dict([(group, data) for group, data in metadata_groups.items() if data]) if metadata_groups: - subtemplates_dict.append( + subtemplates_list.append( { - 'title': _(u'document groups (%s)') % len(metadata_groups.keys()), - 'form': MetaDataGroupForm(groups=metadata_groups, current_document=document, links=[ - metadata_group_link - ]), 'name': 'generic_form_subtemplate.html', - 'form_action': reverse('metadatagroup_action'), - 'submit_method': 'GET', + 'context': { + 'title': _(u'document groups (%s)') % len(metadata_groups.keys()), + 'form': MetaDataGroupForm(groups=metadata_groups, current_document=document, links=[ + metadata_group_link + ]), + 'form_action': reverse('metadatagroup_action'), + 'submit_method': 'GET', + } } ) if FILESERVING_ENABLE: - subtemplates_dict.append({ + subtemplates_list.append({ 'name': 'generic_list_subtemplate.html', - 'title': _(u'index links'), - 'object_list': document.documentmetadataindex_set.all(), - 'hide_link': True + 'context': { + 'title': _(u'index links'), + 'object_list': document.documentmetadataindex_set.all(), + 'hide_link': True + } }) return render_to_response('generic_detail.html', { 'form_list': form_list, 'object': document, 'document': document, - 'subtemplates_dict': subtemplates_dict, + 'subtemplates_list': subtemplates_list, }, context_instance=RequestContext(request)) @@ -836,17 +842,19 @@ def document_view_simple(request, document_id): }, ] - subtemplates_dict = [] + subtemplates_list = [] if document.tags.count(): - subtemplates_dict.append(get_tags_subtemplate(document)) + subtemplates_list.append(get_tags_subtemplate(document)) - subtemplates_dict.append( + subtemplates_list.append( { 'name': 'generic_list_subtemplate.html', - 'title': _(u'metadata'), - 'object_list': document.documentmetadata_set.all(), - 'extra_columns': [{'name': _(u'value'), 'attribute': 'value'}], - 'hide_link': True, + 'context': { + 'title': _(u'metadata'), + 'object_list': document.documentmetadata_set.all(), + 'extra_columns': [{'name': _(u'value'), 'attribute': 'value'}], + 'hide_link': True, + } }, ) @@ -861,18 +869,20 @@ def document_view_simple(request, document_id): metadata_groups = dict([(group, data) for group, data in metadata_groups.items() if data]) if metadata_groups: - subtemplates_dict.append( + subtemplates_list.append( { - 'title': _(u'document groups (%s)') % len(metadata_groups.keys()), - 'form': MetaDataGroupForm( - groups=metadata_groups, current_document=document, - links=[ - metadata_group_link - ] - ), 'name': 'generic_form_subtemplate.html', - 'form_action': reverse('metadatagroup_action'), - 'submit_method': 'GET', + 'context': { + 'title': _(u'document groups (%s)') % len(metadata_groups.keys()), + 'form': MetaDataGroupForm( + groups=metadata_groups, current_document=document, + links=[ + metadata_group_link + ] + ), + 'form_action': reverse('metadatagroup_action'), + 'submit_method': 'GET', + } } ) @@ -880,7 +890,7 @@ def document_view_simple(request, document_id): 'form_list': form_list, 'object': document, 'document': document, - 'subtemplates_dict': subtemplates_dict, + 'subtemplates_list': subtemplates_list, }, context_instance=RequestContext(request)) diff --git a/apps/navigation/templatetags/navigation_tags.py b/apps/navigation/templatetags/navigation_tags.py index f256d0ff36..2977edf273 100644 --- a/apps/navigation/templatetags/navigation_tags.py +++ b/apps/navigation/templatetags/navigation_tags.py @@ -137,7 +137,7 @@ def _get_object_navigation_links(context, menu_name=None, links_dict=object_navi """ navigation_object_links = Variable('navigation_object_links').resolve(context) if navigation_object_links: - return navigation_object_links + return [link for link in resolve_links(context, navigation_object_links, current_view, current_path)] except VariableDoesNotExist: pass