This one uses - and is very similar to - http://www.djangosnippets.org/snippets/852/
It gets the data from a pledgie.com campaign and parses it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | from django import template
from django.utils import simplejson
import urllib
register = template.Library()
class PledgieParserNode(template.Node):
def __init__(self, var_name, pledgie_id=None, pledgie_id_var_name=None):
self.pledgie_id = pledgie_id
self.pledgie_id_var_name = pledgie_id_var_name
self.var_name = var_name
def render(self, context):
if self.pledgie_id_var_name:
try:
url = "http://pledgie.com/campaigns/%d.json" % context[self.pledgie_id_var_name]
except KeyError:
raise template.TemplateSyntaxError, "the variable \"%s\" can't be found in the context" % self.pledgie_id_var_name
else:
url = "http://pledgie.com/campaigns/%d.json" % self.pledgie_id
data = urllib.urlopen(url).read()
context[self.var_name] = simplejson.loads(data)
return ''
import re
@register.tag(name="get_pledgie_campaign_details")
def get_pledgie_campaign_details(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 template.TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0]
m = re.search(r'(.*?) as (\w+)', arg)
if not m:
raise template.TemplateSyntaxError, "%r tag had invalid arguments" % tag_name
pledgie_id, var_name = m.groups()
if pledgie_id[0] == pledgie_id[-1] and pledgie_id[0] in ('"', "'"):
try:
pledgie_id = int(pledgie_id[1:-1])
except ValueError:
raise template.TemplateSyntaxError, "the pledgie id should be an integer..."
return PledgieParserNode(var_name, pledgie_id=pledgie_id)
else:
return PledgieParserNode(var_name, pledgie_id_var_name=pledgie_id)
# this also uses snippet http://www.djangosnippets.org/snippets/852/ !!
"""
{% load cache %}
{% load rss %}
{% load pledgie %}
{% cache 300 pledgie_cache %}
{% get_pledgie_campaign_details "1174" as pledgie %}
{{ pledgie.amount_raised }}{{ pledgie.currency }} {% if pledgie.goal %}({{ pledgie.percent_towards_goal }}%) of {{ pledgie.goal }}{{ pledgie.currency }}{% endif %} raised for {{ pledgie.title }}
{% get_rss "http://pledgie.com/campaigns/1174/donations_feed.xml" as pledgie_rss %}
<h1>Latest 10 donators</h1>
<ul>
{% for entry in pledgie_rss.entries|slice:":10" %}
<li>{{ entry.title }}</li>
{% endfor %}
</ul>
{% endcache %}
"""
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.