From 9e2d3bed8d3ef92fe8f4562d9ec1a44f1b05df82 Mon Sep 17 00:00:00 2001 From: Matt Dorn Date: Thu, 4 Aug 2011 11:46:44 -0500 Subject: [PATCH] added syntax checker management command --- dbtemplates/admin.py | 8 ++++---- .../commands/check_template_syntax.py | 20 +++++++++++++++++++ dbtemplates/utils/template.py | 6 +++--- 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 dbtemplates/management/commands/check_template_syntax.py diff --git a/dbtemplates/admin.py b/dbtemplates/admin.py index 1cae410..fec0470 100644 --- a/dbtemplates/admin.py +++ b/dbtemplates/admin.py @@ -119,16 +119,16 @@ class TemplateAdmin(TemplateModelAdmin): def validate_syntax(self, request, queryset): errors = [] for template in queryset: - ok = check_template_syntax(template) - if not ok: - errors.append(template.name) + 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)}) + 'names': '; '.join(errors)}) else: message = ungettext( "Template syntax OK.", diff --git a/dbtemplates/management/commands/check_template_syntax.py b/dbtemplates/management/commands/check_template_syntax.py new file mode 100644 index 0000000..ee31fff --- /dev/null +++ b/dbtemplates/management/commands/check_template_syntax.py @@ -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') diff --git a/dbtemplates/utils/template.py b/dbtemplates/utils/template.py index 84473ae..1f7d0a0 100644 --- a/dbtemplates/utils/template.py +++ b/dbtemplates/utils/template.py @@ -52,7 +52,7 @@ def get_template_source(name): def check_template_syntax(template): try: t = Template(template.content) - except TemplateSyntaxError: - return False - return True + except TemplateSyntaxError, e: + return (False, e) + return (True, None) \ No newline at end of file