Commits

Lukasz Balcerzak committed ab3cdd5

Added RICHTEMPLATES_RESTRUCTUREDTEXT_PARSER_MAX_CHARS setting

  • Participants
  • Parent commits f8093b8

Comments (0)

Files changed (2)

richtemplates/settings.py

 
 register_rst_directives(RESTRUCTUREDTEXT_DIRECTIVES.items())
 
+RESTRUCTUREDTEXT_PARSER_MAX_CHARS = getattr(settings,
+    'RICHTEMPLATES_RESTRUCTUREDTEXT_PARSER_MAX_CHARS', 5000)
+
 
 # ======================= #
 # Pygments styles helpers #

richtemplates/views.py

 
 from docutils.utils import SystemMessage
 
+from richtemplates.settings import RESTRUCTUREDTEXT_PARSER_MAX_CHARS
+
 
 def handle403(request, template_name='403.html'):
     """
     if not request.is_ajax() or not request.method == 'POST':
         raise Http404()
     data = request.POST.get('data', '')
-    try:
-        rendered = restructuredtext(data)
-    except SystemMessage:
-        html = get_rst_error_as_html(
-            'Sorry but there are at severe errors in your text '
-            'and we cannot show it\'s preview.')
-        rendered = mark_safe(html)
+    if len(data) > RESTRUCTUREDTEXT_PARSER_MAX_CHARS:
+        html = get_rst_error_as_html('Text is too long (%s). Maximum is %s.' %
+            (len(data), RESTRUCTUREDTEXT_PARSER_MAX_CHARS))
+    else:
+        try:
+            html = restructuredtext(data)
+        except SystemMessage:
+            html = get_rst_error_as_html(
+                'Sorry but there are at severe errors in your text '
+                'and we cannot show it\'s preview.')
+    rendered = mark_safe(html)
     return HttpResponse(dumps(rendered), mimetype='application/json')