mirror of
https://github.com/jazzband/django-dbtemplates.git
synced 2026-03-16 22:20:28 +00:00
added syntax checker management command
This commit is contained in:
parent
6aac801025
commit
9e2d3bed8d
3 changed files with 27 additions and 7 deletions
|
|
@ -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.",
|
||||
|
|
|
|||
20
dbtemplates/management/commands/check_template_syntax.py
Normal file
20
dbtemplates/management/commands/check_template_syntax.py
Normal 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')
|
||||
|
|
@ -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)
|
||||
|
||||
Loading…
Reference in a new issue