From 826f0cd640bc8072284d16fdb47219104058abc7 Mon Sep 17 00:00:00 2001
From: olivier <>
Date: Wed, 27 Jan 2010 14:46:10 +0000
Subject: [PATCH] make the dl_field template tag more django-like
---
.../telemeta_default/collection_detail.html | 66 +++++++++----------
telemeta/templatetags/telemeta_utils.py | 26 +++++---
2 files changed, 50 insertions(+), 42 deletions(-)
diff --git a/telemeta/templates/telemeta_default/collection_detail.html b/telemeta/templates/telemeta_default/collection_detail.html
index 98160517..fa915b92 100644
--- a/telemeta/templates/telemeta_default/collection_detail.html
+++ b/telemeta/templates/telemeta_default/collection_detail.html
@@ -44,14 +44,14 @@
{% block general_info %}
- {% dl_field collection.title %}
- {% dl_field collection.alt_title %}
- {% dl_field collection.creator %}
+ {% dl_field collection "title" %}
+ {% dl_field collection "alt_title" %}
+ {% dl_field collection "creator" %}
{% if collection.recorded_from_year %}
- {% trans "Recording year" %}
- {{ collection.recorded_from_year }} {{ collection.recorded_to_year|prepend:" - " }}
{% endif %}
- {% dl_field collection.year_published %}
+ {% dl_field collection "year_published" %}
{% endblock general_info %}
@@ -61,8 +61,8 @@
- {% dl_field collection.countries join with ", " %}
- {% dl_field collection.ethnic_groups join with ", " %}
+ {% dl_field collection "countries" join with ", " %}
+ {% dl_field collection "ethnic_groups" join with ", " %}
@@ -76,19 +76,19 @@
{% if collection.collector_is_creator %}
{% if collection.creator %}
- - {% trans "Collector" %}
- {{ collector.creator }}
+ - {% trans "Collector" %}
- {{ collection.creator }}
{% endif%}
{% else %}
- {% dl_field collection.collector %}
+ {% dl_field collection "collector" %}
{% endif %}
- {% dl_field collection.publisher %}
- {% dl_field collection.publisher_collection %}
- {% dl_field collection.publisher_serial %}
- {% dl_field collection.booklet_author %}
- {% dl_field collection.external_references %}
- {% dl_field collection.doctype_code %}
- {% dl_field collection.public_access %}
- {% dl_field collection.legal_rights %}
+ {% dl_field collection "publisher" %}
+ {% dl_field collection "publisher_collection" %}
+ {% dl_field collection "publisher_serial" %}
+ {% dl_field collection "booklet_author" %}
+ {% dl_field collection "external_references" %}
+ {% dl_field collection "doctype_code" %}
+ {% dl_field collection "public_access" %}
+ {% dl_field collection "legal_rights" %}
@@ -100,16 +100,16 @@
- {% dl_field collection.acquisition_mode %}
- {% dl_field collection.cnrs_contributor %}
- {% dl_field collection.metadata_writer %}
- {% dl_field collection.booklet_description %}
- {% dl_field collection.publishing_status %}
- {% dl_field collection.alt_ids %}
- {% dl_field collection.comment %}
- {% dl_field collection.metadata_writer %}
- {% dl_field collection.travail %}
- {% dl_field collection.items_done %}
+ {% dl_field collection "acquisition_mode" %}
+ {% dl_field collection "cnrs_contributor" %}
+ {% dl_field collection "metadata_writer" %}
+ {% dl_field collection "booklet_description" %}
+ {% dl_field collection "publishing_status" %}
+ {% dl_field collection "alt_ids" %}
+ {% dl_field collection "comment" %}
+ {% dl_field collection "metadata_writer" %}
+ {% dl_field collection "travail" %}
+ {% dl_field collection "items_done" %}
@@ -121,15 +121,15 @@
- {% dl_field collection.code %}
- {% dl_field collection.old_code %}
+ {% dl_field collection "code" %}
+ {% dl_field collection "old_code" %}
- {% trans "Media type" %}
- {% trans "Audio" %}
- {% dl_field collection.approx_duration %}
- {% dl_field collection.computed_duration %}
- {% dl_field collection.physical_items_num %}
+ {% dl_field collection "approx_duration" %}
+ {% dl_field collection "computed_duration" %}
+ {% dl_field collection "physical_items_num" %}
- {% trans "Number of items" %}
- {{ collection.items.count }}
- {% dl_field collection.physical_format %}
- {% dl_field collection.ad_conversion %}
+ {% dl_field collection "physical_format" %}
+ {% dl_field collection "ad_conversion" %}
diff --git a/telemeta/templatetags/telemeta_utils.py b/telemeta/templatetags/telemeta_utils.py
index 0fb014d4..e92cbfdf 100644
--- a/telemeta/templatetags/telemeta_utils.py
+++ b/telemeta/templatetags/telemeta_utils.py
@@ -98,18 +98,21 @@ def to_dublincore(resource):
return dc.express_collection(resource)
class DescriptionListFieldNode(template.Node):
- def __init__(self, variable, join_with = None):
- cut = variable.split('.')
- self.model = template.Variable('.'.join(cut[:-1]))
- self.member = cut[-1]
+ def __init__(self, model, attr, join_with = None):
+ self.model = model
+ self.member = attr
self.join_with = join_with
def render(self, context):
try:
model = self.model.resolve(context)
- label = html.escape(capfirst(unicode(model.field_label(self.member))))
+ if isinstance(self.member, template.Variable):
+ member = self.member.resolve(context)
+ else:
+ member = self.member
+ label = html.escape(capfirst(unicode(model.field_label(member))))
try:
- value = getattr(model, self.member)
+ value = getattr(model, member)
except AttributeError:
value = ''
except template.VariableDoesNotExist:
@@ -134,10 +137,10 @@ def dl_field(parser, token):
cut = token.split_contents()
join_with = None
try:
- tag_name, variable = cut
+ tag_name, model, attr = cut
except ValueError:
try:
- tag_name, variable, arg3, arg4, arg5 = cut
+ tag_name, model, attr, arg3, arg4, arg5 = cut
if arg3 == 'join' and arg4 == 'with'and arg5[0] == arg5[-1] and arg5[0] in ('"', "'"):
join_with = arg5[1:-1]
else:
@@ -146,7 +149,12 @@ def dl_field(parser, token):
raise template.TemplateSyntaxError("%r tag: invalid arguments"
% token.contents.split()[0])
- return DescriptionListFieldNode(variable, join_with=join_with)
+ if attr[0] == attr[-1] and attr[0] in ('"', "'"):
+ attr = attr[1:-1]
+ else:
+ attr = template.Variable(attr)
+ model = template.Variable(model)
+ return DescriptionListFieldNode(model, attr, join_with=join_with)
@register.filter
def prepend(str, prefix):
--
2.39.5