This commit is contained in:
GitHub Merge Button 2011-08-04 13:49:45 -07:00
commit 5625b98ef8
4 changed files with 65 additions and 4 deletions

View file

@ -7,6 +7,7 @@ from django.utils.safestring import mark_safe
from dbtemplates.conf import settings
from dbtemplates.models import (Template,
remove_cached_template, add_template_to_cache)
from dbtemplates.utils.template import check_template_syntax
# Check if django-reversion is installed and use reversions' VersionAdmin
# as the base admin class if yes
@ -91,7 +92,7 @@ class TemplateAdmin(TemplateModelAdmin):
list_filter = ('sites',)
save_as = True
search_fields = ('name', 'content')
actions = ['invalidate_cache', 'repopulate_cache']
actions = ['invalidate_cache', 'repopulate_cache', 'validate_syntax']
def invalidate_cache(self, request, queryset):
for template in queryset:
@ -115,6 +116,27 @@ class TemplateAdmin(TemplateModelAdmin):
repopulate_cache.short_description = _("Repopulate cache with "
"selected templates")
def validate_syntax(self, request, queryset):
errors = []
for template in queryset:
result = check_template_syntax(template)
if not result[0]:
errors.append('%s: %s' % (template.name, result[1]))
if errors:
message = ungettext(
"Template syntax check FAILED for %(names)s.",
"Template syntax check FAILED for %(count)d templates: %(names)s.",
len(errors))
self.message_user(request, message % {'count': len(errors),
'names': '; '.join(errors)})
else:
message = ungettext(
"Template syntax OK.",
"Template syntax OK for %(count)d templates.",
len(queryset))
self.message_user(request, message % {'count': len(queryset)})
validate_syntax.short_description = _("Check template syntax")
def site_list(self, template):
return ", ".join([site.name for site in template.sites.all()])
site_list.short_description = _('sites')

View file

@ -0,0 +1,20 @@
from django.core.management.base import CommandError, NoArgsCommand
from dbtemplates.models import Template
from dbtemplates.utils.template import check_template_syntax as check
class Command(NoArgsCommand):
help = "Ensures templates don't have syntax errors."
def handle_noargs(self, **options):
errors = []
for template in Template.objects.all():
result = check(template)
if not result[0]:
errors.append('%s: %s' % (template.name, result[1]))
if errors:
raise CommandError(
'Some templates contained errors\n%s' % '\n'.join(errors))
# NOTE: printing instead of using self.stdout.write to maintain
# Django 1.2 compatibility
print('OK')

View file

@ -14,7 +14,8 @@ from django.contrib.sites.models import Site
from dbtemplates.conf import settings
from dbtemplates.models import Template
from dbtemplates.utils.cache import get_cache_backend
from dbtemplates.utils.template import get_template_source
from dbtemplates.utils.template import (get_template_source,
check_template_syntax)
from dbtemplates.management.commands.sync_templates import (FILES_TO_DATABASE,
DATABASE_TO_FILES)
@ -96,3 +97,13 @@ class DbTemplatesTestCase(TestCase):
def test_get_cache(self):
self.assertTrue(isinstance(get_cache_backend(), BaseCache))
def test_check_template_syntax(self):
bad_template, _ = Template.objects.get_or_create(
name='bad.html', content='{% if foo %}Bar')
good_template, _ = Template.objects.get_or_create(
name='good.html', content='{% if foo %}Bar{% endif %}')
ok = check_template_syntax(bad_template)
self.assertFalse(ok)
ok = check_template_syntax(good_template)
self.assertTrue(ok)

View file

@ -1,8 +1,8 @@
from django import VERSION
from django.template import TemplateDoesNotExist
from django.template import (Template, TemplateDoesNotExist,
TemplateSyntaxError)
from django.utils.importlib import import_module
def get_loaders():
from django.template.loader import template_source_loaders
if template_source_loaders is None:
@ -48,3 +48,11 @@ def get_template_source(name):
except (ImportError, TemplateDoesNotExist):
pass
return None
def check_template_syntax(template):
try:
t = Template(template.content)
except TemplateSyntaxError, e:
return (False, e)
return (True, None)