Skip to content

Commit d85e0ae

Browse files
committed
Added template rendering verification on dbtemplate form submission in order to catch errors in edited templates before they are committed. This should prevent server 500 errors when rendering dbtemplate pages. Fixes issue ietf-tools#1113.
- Legacy-Id: 6079
1 parent 84a07d7 commit d85e0ae

5 files changed

Lines changed: 49 additions & 39 deletions

File tree

ietf/dbtemplate/forms.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
from django import forms
2+
from django.core.exceptions import ValidationError
3+
from django.template import Context
24

35
from ietf.dbtemplate.models import DBTemplate
6+
from ietf.dbtemplate.template import PlainTemplate, RSTTemplate, DjangoTemplate
47

8+
import debug
59

610
class DBTemplateForm(forms.ModelForm):
711

12+
def clean_content(self):
13+
try:
14+
content = self.cleaned_data['content']
15+
debug.show('type(content)')
16+
debug.show('content')
17+
if self.instance.type.slug == 'rst':
18+
return_code = RSTTemplate(content).render(Context({}))
19+
debug.show('return_code')
20+
elif self.instance.type.slug == 'django':
21+
DjangoTemplate(content).render(Context({}))
22+
elif self.instance.type.slug == 'plain':
23+
PlainTemplate(content).render(Context({}))
24+
else:
25+
raise ValidationError("Unexpected DBTemplate.type.slug: %s" % self.type.slug)
26+
except Exception, e:
27+
raise ValidationError(e)
28+
return content
29+
830
class Meta:
931
model = DBTemplate
1032
fields = ('content', )

ietf/dbtemplate/models.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,12 @@
1212

1313

1414
class DBTemplate(models.Model):
15-
path = models.CharField(
16-
max_length=255,
17-
unique=True,
18-
blank=False,
19-
null=False,
20-
)
21-
title = models.CharField(
22-
max_length=255,
23-
blank=False,
24-
null=False,
25-
)
26-
variables = models.TextField(
27-
blank=True,
28-
null=True,
29-
)
30-
type = models.ForeignKey(
31-
DBTemplateTypeName,
32-
)
33-
content = models.TextField(
34-
blank=False,
35-
null=False,
36-
)
37-
group = models.ForeignKey(
38-
Group,
39-
blank=True,
40-
null=True,
41-
)
15+
path = models.CharField( max_length=255, unique=True, blank=False, null=False, )
16+
title = models.CharField( max_length=255, blank=False, null=False, )
17+
variables = models.TextField( blank=True, null=True, )
18+
type = models.ForeignKey( DBTemplateTypeName, )
19+
content = models.TextField( blank=False, null=False, )
20+
group = models.ForeignKey( Group, blank=True, null=True, )
4221

4322
def __unicode__(self):
4423
return self.title

ietf/dbtemplate/template.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import string
33
from docutils.core import publish_string
4+
from docutils.utils import SystemMessage
5+
import debug
46

57
from django.template import Template as DjangoTemplate, TemplateDoesNotExist, TemplateEncodingError
68
from django.template.loader import BaseLoader
@@ -40,16 +42,23 @@ class RSTTemplate(PlainTemplate):
4042

4143
def render(self, context):
4244
interpolated_string = super(RSTTemplate, self).render(context)
43-
return publish_string(source=interpolated_string,
44-
writer_name='html',
45-
settings_overrides={
46-
'input_encoding': 'unicode',
47-
'output_encoding': 'unicode',
48-
'embed_stylesheet': False,
49-
'xml_declaration': False,
50-
'template': RST_TEMPLATE,
51-
})
52-
45+
try:
46+
return publish_string(source=interpolated_string,
47+
writer_name='html',
48+
settings_overrides={
49+
'input_encoding': 'unicode',
50+
'output_encoding': 'unicode',
51+
'embed_stylesheet': False,
52+
'xml_declaration': False,
53+
'template': RST_TEMPLATE,
54+
'halt_level': 2,
55+
})
56+
except SystemMessage, e:
57+
e.message = e.message.replace('<string>:', 'line ')
58+
args = list(e.args)
59+
args[0] = args[0].replace('<string>:', 'line ')
60+
e.args = tuple(args)
61+
raise e
5362

5463
class Loader(BaseLoader):
5564

ietf/nomcom/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ class Media:
678678

679679

680680
class NomComTemplateForm(BaseNomcomForm, DBTemplateForm):
681-
681+
content = forms.CharField(label="Text", widget=forms.Textarea(attrs={'cols': '120', 'rows':'40', }))
682682
fieldsets = [('Template content', ('content', )), ]
683683

684684

ietf/templates/nomcom/nomcomform.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ <h2>{{ fieldset.name }}</h2>
1818
<div class="fieldWidget">
1919
<div id="{{ field.html_name }}_help" class="formHelp"> {{ field.help_text }}</div>
2020
{{ field }}
21-
{{ field.errors }}
21+
<pre>{{ field.errors }}</pre>
2222
</div>
2323
<div class="endfield"></div>
2424
</div>

0 commit comments

Comments
 (0)