Started testing Django integration, somewhat improved documentation

This commit is contained in:
Antonio Cavedoni 2007-02-16 00:51:53 +01:00
parent 49180c7071
commit 7cd69ce7d7
6 changed files with 90 additions and 16 deletions

View File

@ -128,8 +128,9 @@ Item.</p>
<p>
All the standard Django template tags and filter are supposed to
work, with the notable exception of the <code>date</code> filter and
the <code>now</code> tag.
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>

View File

@ -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
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))
context.update(tmpl.template_info(doc))
t = get_template(script)
if output_file:
reluri = os.path.splitext(os.path.basename(output_file))[0]
context['url'] = urlparse.urljoin(config.link(),reluri)
t = get_template(script)
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)

View File

@ -0,0 +1,2 @@
[Planet]
name: Django on Venus

View 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>

View File

@ -0,0 +1 @@
{% for item in Items %}{{ item.title }}{% endfor %}

View 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