Permalink
Browse files

Replaced get_content_used_on_pages_json template tag with a model met…

…hod, and added filter for escaping JSON for use in HTML
  • Loading branch information...
1 parent b988ffa commit bad4907bb1c2447bbee615f44cca6e17bf1ccace Chi Shang Cheng committed Jul 5, 2011
Showing with 19 additions and 34 deletions.
  1. +11 −0 fiber/models.py
  2. +1 −1 fiber/templates/fiber/admin.html
  3. +1 −1 fiber/templates/fiber/content_item.html
  4. +6 −32 fiber/templatetags/fiber_tags.py
View
@@ -8,6 +8,7 @@
from django.core.urlresolvers import reverse
from django.core.files.images import get_image_dimensions
from django.utils.html import strip_tags
+from django.utils import simplejson
from mptt.models import MPTTModel
@@ -127,6 +128,16 @@ def get_change_url(self):
named_url = 'fiber_admin:%s_%s_change' % (self._meta.app_label, self._meta.object_name.lower())
return reverse(named_url, args=(self.id, ))
+ def get_used_on_pages_json(self):
+ page_content_items = self.page_content_items.all()
+ json_pages = []
+ for page_content_item in page_content_items:
+ json_pages.append({
+ 'title': page_content_item.page.title,
+ 'url': page_content_item.page.get_absolute_url(),
+ })
+ return simplejson.dumps(json_pages)
+
class Meta:
verbose_name = _('content item')
verbose_name_plural = _('content items')
@@ -32,7 +32,7 @@
<a href="#">{{ group.title }}</a>
<ul>
{% for content_item in group.content_items %}
- <li data-fiber-data='{"type": "content_item", "id": {{ content_item.id }}, "url": "{{ content_item.get_change_url }}", "used_on_pages": {% content_used_on_pages_json content_item %}}'>
+ <li data-fiber-data='{"type": "content_item", "id": {{ content_item.id }}, "url": "{{ content_item.get_change_url }}", "used_on_pages": {{ content_item.get_used_on_pages_json|safe|escape_json_for_html }}}'>
<a href="#">{{ content_item }}</a>
</li>
{% endfor %}
@@ -3,7 +3,7 @@
{% if user.is_staff %}
{% if fiber_page %}
{% if content_item.page_content_item %}
- <div data-fiber-data='{"type": "content_item", "id": {{ content_item.id }}, "url": "{{ content_item.get_change_url }}", "add_url": "{{ ContentItem.get_add_url }}", "page_id": {{ fiber_page.id }}, "block_name": "{{ fiber_block_name }}", "page_content_item_id": {{ content_item.page_content_item.id }}, "used_on_pages": {% content_used_on_pages_json content_item %}}' class="content">
+ <div data-fiber-data='{"type": "content_item", "id": {{ content_item.id }}, "url": "{{ content_item.get_change_url }}", "add_url": "{{ ContentItem.get_add_url }}", "page_id": {{ fiber_page.id }}, "block_name": "{{ fiber_block_name }}", "page_content_item_id": {{ content_item.page_content_item.id }}, "used_on_pages": {{ content_item.get_used_on_pages_json|safe|escape_json_for_html }}}' class="content">
{% else %}
<div data-fiber-data='{"type": "content_item", "id": {{ content_item.id }}, "url": "{{ content_item.get_change_url }}", "add_url": "{{ ContentItem.get_add_url }}", "page_id": {{ fiber_page.id }}, "block_name": "{{ fiber_block_name }}"}' class="content">
{% endif %}
@@ -208,35 +208,9 @@ def editable_attrs(parser, token):
return EditableAttrsNode(instance_var)
-def get_content_used_on_pages_json(instance):
- page_content_items = instance.page_content_items.all()
- json_pages = []
- for page_content_item in page_content_items:
- json_pages.append({
- 'title': page_content_item.page.title,
- 'url': page_content_item.page.get_absolute_url(),
- })
- return simplejson.dumps(json_pages)
-
-
-class ContentUsedOnPagesJSONNode(template.Node):
-
- def __init__(self, instance_var):
- self.instance_var = template.Variable(instance_var)
-
- def render(self, context):
- try:
- instance = self.instance_var.resolve(context)
- return get_content_used_on_pages_json(instance)
- except template.VariableDoesNotExist:
- return ''
-
-
-@register.tag(name='content_used_on_pages_json')
-def content_used_on_pages_json(parser, token):
- try:
- instance_var = token.split_contents()[1]
- except ValueError:
- raise template.TemplateSyntaxError, "%r tag requires one argument" % token.contents.split()[0]
-
- return ContentUsedOnPagesJSONNode(instance_var)
+@register.filter(name='escape_json_for_html')
+def escape_json_for_html(value):
+ """
+ Escapes valid JSON for use in HTML, e.g. convert single quote to HTML character entity
+ """
+ return value.replace("'", "&#39;")

0 comments on commit bad4907

Please sign in to comment.