Django fixes
This commit is contained in:
commit
7d14cbcf64
1
THANKS
1
THANKS
@ -11,6 +11,7 @@ Chris Dolan - mkdir cache; default template_dirs; fix xsltproc
|
||||
David Sifry - rss 2.0 xslt template based on http://atom.geekhood.net/
|
||||
Morten Fredericksen - Support WordPress LinkManager OPML
|
||||
Harry Fuecks - default item date to feed date
|
||||
Antonio Cavendoni - Django templates
|
||||
|
||||
This codebase represents a radical refactoring of Planet 2.0, which lists
|
||||
the following contributors:
|
||||
|
@ -101,6 +101,43 @@ The data values within the <code>Items</code> array are as follows:</p>
|
||||
<code>new_</code> are only set if their values differ from the previous
|
||||
Item.</p>
|
||||
|
||||
<h3>django</h3>
|
||||
|
||||
<p>
|
||||
If you have the <a href="http://www.djangoproject.com/">Django</a>
|
||||
framework installed,
|
||||
<a href="http://www.djangoproject.com/documentation/templates/"
|
||||
>Django templates</a> are automatically available to Venus
|
||||
projects. You will have to save them with a <code>.html.dj</code>
|
||||
extension in your themes. The variable set is the same as the one
|
||||
from htmltmpl, above. In the Django template context you'll have
|
||||
access to <code>Channels</code> and <code>Items</code> and you'll be
|
||||
able to iterate through them.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
If you lose your way and want to introspect all the variable in the
|
||||
context, there's the useful <code>{% debug %}</code> template tag.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
In the <code>themes/django/</code> you'll find a sample Venus theme
|
||||
that uses the Django templates that might be a starting point for
|
||||
your own custom themes.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
All the standard Django template tags and filter are supposed to
|
||||
work, with the notable exception of the <code>date</code> filter on
|
||||
the updated and published dates of an item (it works on the main
|
||||
<code>{{ date }}</code> variable).
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Please note that Django, and therefore Venus' Django support,
|
||||
requires at least Python 2.3.
|
||||
</p>
|
||||
|
||||
<h3>xslt</h3>
|
||||
<p><a href="http://www.w3.org/TR/xslt">XSLT</a> is a paradox: it actually
|
||||
makes some simple things easier to do than htmltmpl, and certainly can
|
||||
|
@ -1,30 +1,47 @@
|
||||
import os.path
|
||||
import urlparse
|
||||
import datetime
|
||||
|
||||
import tmpl
|
||||
from planet import config
|
||||
from tmpl import template_info
|
||||
|
||||
def DjangoPlanetDate(value):
|
||||
return datetime.datetime(*value[:6])
|
||||
|
||||
# remap PlanetDate to be a datetime, so Django template authors can use
|
||||
# the "date" filter on these values
|
||||
tmpl.PlanetDate = DjangoPlanetDate
|
||||
|
||||
def run(script, doc, output_file=None, options={}):
|
||||
"""process a Django template file """
|
||||
"""process a Django template file"""
|
||||
|
||||
# this is needed to use the Django template system as standalone
|
||||
# I need to re-import the settings at every call because I have to
|
||||
# set the TEMPLATE_DIRS variable programmatically
|
||||
from django.conf import settings
|
||||
settings.configure(
|
||||
DEBUG=True, TEMPLATE_DEBUG=True,
|
||||
TEMPLATE_DIRS=(os.path.dirname(script),)
|
||||
)
|
||||
try:
|
||||
settings.configure(
|
||||
DEBUG=True, TEMPLATE_DEBUG=True,
|
||||
TEMPLATE_DIRS=(os.path.dirname(script),)
|
||||
)
|
||||
except EnvironmentError:
|
||||
pass
|
||||
from django.template import Context
|
||||
from django.template.loader import get_template
|
||||
|
||||
# set up the Django context by using the default htmltmpl
|
||||
# datatype converters
|
||||
context = Context()
|
||||
context.update(template_info(doc))
|
||||
|
||||
reluri = os.path.splitext(os.path.basename(output_file))[0]
|
||||
context['url'] = urlparse.urljoin(config.link(),reluri)
|
||||
|
||||
context.update(tmpl.template_info(doc))
|
||||
t = get_template(script)
|
||||
f = open(output_file, 'w')
|
||||
f.write(t.render(context))
|
||||
f.close()
|
||||
|
||||
if output_file:
|
||||
reluri = os.path.splitext(os.path.basename(output_file))[0]
|
||||
context['url'] = urlparse.urljoin(config.link(),reluri)
|
||||
f = open(output_file, 'w')
|
||||
f.write(t.render(context))
|
||||
f.close()
|
||||
else:
|
||||
# @@this is useful for testing purposes, but does it
|
||||
# belong here?
|
||||
return t.render(context)
|
||||
|
2
tests/data/filter/django/test.ini
Normal file
2
tests/data/filter/django/test.ini
Normal file
@ -0,0 +1,2 @@
|
||||
[Planet]
|
||||
name: Django on Venus
|
20
tests/data/filter/django/test.xml
Normal file
20
tests/data/filter/django/test.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
|
||||
<title>Example Feed</title>
|
||||
<link href="http://example.org/"/>
|
||||
<updated>2003-12-13T18:30:02Z</updated>
|
||||
<author>
|
||||
<name>John Doe</name>
|
||||
</author>
|
||||
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
|
||||
|
||||
<entry>
|
||||
<title>Atom-Powered Robots Run Amok</title>
|
||||
<link href="http://example.org/2003/12/13/atom03"/>
|
||||
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
|
||||
<updated>2003-12-13T18:30:02Z</updated>
|
||||
<summary>Some text.</summary>
|
||||
</entry>
|
||||
|
||||
</feed>
|
1
tests/data/filter/django/title.html.dj
Normal file
1
tests/data/filter/django/title.html.dj
Normal file
@ -0,0 +1 @@
|
||||
{% for item in Items %}{{ item.title }}{% endfor %}
|
34
tests/test_filter_django.py
Normal file
34
tests/test_filter_django.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os.path
|
||||
import unittest, xml.dom.minidom, datetime
|
||||
|
||||
from planet import config, logger
|
||||
from planet.shell import dj
|
||||
|
||||
class DjangoFilterTests(unittest.TestCase):
|
||||
|
||||
def test_django_filter(self):
|
||||
config.load('tests/data/filter/django/test.ini')
|
||||
results = dj.tmpl.template_info("<feed/>")
|
||||
self.assertEqual(results['name'], 'Django on Venus')
|
||||
|
||||
def test_django_date_type(self):
|
||||
config.load('tests/data/filter/django/test.ini')
|
||||
results = dj.tmpl.template_info("<feed/>")
|
||||
self.assertEqual(type(results['date']), datetime.datetime)
|
||||
|
||||
def test_django_item_title(self):
|
||||
config.load('tests/data/filter/django/test.ini')
|
||||
feed = open('tests/data/filter/django/test.xml')
|
||||
input = feed.read(); feed.close()
|
||||
results = dj.run(
|
||||
os.path.realpath('tests/data/filter/django/title.html.dj'), input)
|
||||
self.assertEqual(results, "Atom-Powered Robots Run Amok\n")
|
||||
|
||||
try:
|
||||
from django.conf import settings
|
||||
except ImportError:
|
||||
logger.warn("Django is not available => can't test django filters")
|
||||
del DjangoFilterTests.test_django_filter
|
||||
del DjangoFilterTests.test_django_item_title
|
@ -39,7 +39,7 @@
|
||||
<p class="entry-tools">
|
||||
by {{ item.channel_author }} on
|
||||
{{ item.date }} ·
|
||||
<a href="{{ item.id }}">permalink</a>
|
||||
<a href="{{ item.link }}">permalink</a>
|
||||
</p>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
Loading…
Reference in New Issue
Block a user