Started testing Django integration, somewhat improved documentation
This commit is contained in:
parent
49180c7071
commit
7cd69ce7d7
@ -128,8 +128,9 @@ Item.</p>
|
|||||||
|
|
||||||
<p>
|
<p>
|
||||||
All the standard Django template tags and filter are supposed to
|
All the standard Django template tags and filter are supposed to
|
||||||
work, with the notable exception of the <code>date</code> filter and
|
work, with the notable exception of the <code>date</code> filter on
|
||||||
the <code>now</code> tag.
|
the updated and published dates of an item (it works on the main
|
||||||
|
<code>{{ date }}</code> variable).
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
@ -1,30 +1,47 @@
|
|||||||
import os.path
|
import os.path
|
||||||
import urlparse
|
import urlparse
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
import tmpl
|
||||||
from planet import config
|
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={}):
|
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
|
# 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
|
# I need to re-import the settings at every call because I have to
|
||||||
# set the TEMPLATE_DIRS variable programmatically
|
# set the TEMPLATE_DIRS variable programmatically
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
try:
|
||||||
settings.configure(
|
settings.configure(
|
||||||
DEBUG=True, TEMPLATE_DEBUG=True,
|
DEBUG=True, TEMPLATE_DEBUG=True,
|
||||||
TEMPLATE_DIRS=(os.path.dirname(script),)
|
TEMPLATE_DIRS=(os.path.dirname(script),)
|
||||||
)
|
)
|
||||||
|
except EnvironmentError:
|
||||||
|
pass
|
||||||
from django.template import Context
|
from django.template import Context
|
||||||
from django.template.loader import get_template
|
from django.template.loader import get_template
|
||||||
|
|
||||||
|
# set up the Django context by using the default htmltmpl
|
||||||
|
# datatype converters
|
||||||
context = Context()
|
context = Context()
|
||||||
context.update(template_info(doc))
|
context.update(tmpl.template_info(doc))
|
||||||
|
t = get_template(script)
|
||||||
|
|
||||||
|
if output_file:
|
||||||
reluri = os.path.splitext(os.path.basename(output_file))[0]
|
reluri = os.path.splitext(os.path.basename(output_file))[0]
|
||||||
context['url'] = urlparse.urljoin(config.link(),reluri)
|
context['url'] = urlparse.urljoin(config.link(),reluri)
|
||||||
|
|
||||||
t = get_template(script)
|
|
||||||
f = open(output_file, 'w')
|
f = open(output_file, 'w')
|
||||||
f.write(t.render(context))
|
f.write(t.render(context))
|
||||||
f.close()
|
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 %}
|
33
tests/test_filter_django.py
Normal file
33
tests/test_filter_django.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#!/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
|
Loading…
x
Reference in New Issue
Block a user