Skip to content

indentation on session vars #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n %}
{% load i18n debug_toolbar_utils %}

<h4>{% trans 'View information' %}</h4>
<table>
Expand Down Expand Up @@ -71,7 +71,7 @@ <h4>{% trans 'SESSION Variables' %}</h4>
{% for key, value in session %}
<tr class="{% cycle 'djDebugOdd' 'djDebugEven' %}">
<td>{{ key|escape }}</td>
<td>{{ value|escape }}</td>
<td><code>{{ value|escape|indent_dict }}</code></td>
</tr>
{% endfor %}
</tbody>
Expand Down
42 changes: 42 additions & 0 deletions debug_toolbar/templatetags/debug_toolbar_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#coding=utf-8
from __future__ import unicode_literals

from django import template
from django.utils.numberformat import format
Expand All @@ -10,3 +12,43 @@ def dotted_number(number):
number = float(number)
return format(number, '.', 6)


@register.filter
def indent_dict(value, indent_nb_space=4):
indent = ' ' * indent_nb_space
indent_nb = 0
result = ''
cr = False
inc = 0
for v in value:
inc = inc + 1
ind = indent * indent_nb
if v == '{':
cr = True
indent_nb = indent_nb + 1
if value[inc] == '}':
result += v
prev_v = v
continue
result += '%s\n' % v
elif v == '}':
indent_nb = indent_nb - 1
ind = indent * indent_nb
if prev_v == '{':
result += v
prev_v = v
continue
result += '\n%s%s' % (ind, v)
elif v == ',':
result += '%s\n' % v
cr = True
elif cr:
if v == ' ':
continue
result += ind + v
cr = False
else:
result += v
prev_v = v

return result
90 changes: 90 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# coding=utf-8
from __future__ import unicode_literals

import thread

from django.conf import settings
Expand All @@ -13,6 +16,7 @@
from debug_toolbar.toolbar.loader import DebugToolbar
from debug_toolbar.utils import get_name_from_obj
from debug_toolbar.utils.tracking import pre_dispatch, post_dispatch, callbacks
from debug_toolbar.templatetags.debug_toolbar_utils import indent_dict

rf = RequestFactory()

Expand Down Expand Up @@ -252,6 +256,92 @@ def test_queryset_hook(self):
self.assertIn('<<triggers database query>>', ctx)


class TemplateTagsTestCase(BaseTestCase):
def test_indent_dict(self):
input = (
"{'step_files': {u'1': {}, u'0': {}, u'3': {}, "
"u'2': {}}, 'step': u'4', 'extra_data': {}, 'step_data': "
"{u'1': {u'1-fax': [u''], u'1-site': [u''], "
"u'inscription_wizard-current_step': [u'1'], "
"u'1-city': [u'paris'], u'1-zip_code': [u'XXXXX'], "
"u'submit': [u'next step'], "
"u'1-phone': [u'XX XX XX XX XX'], "
"u'csrfmiddlewaretoken': [u'X'], "
"u'1-address': [u'xxx xxx xxx.'], "
"u'1-mobile_phone': [u'XX XX XX XX XX']}, "
"u'0': {u'0-email': [u'xxx@xxx.xx'], "
"u'inscription_wizard-current_step': [u'0'], "
"u'submit': [u'next step'], "
"u'csrfmiddlewaretoken': [u'X'], "
"u'0-origin': [u'0']}, "
"u'3': {u'3-honeypot': [u''], u'3-select_paiement': [u'1'], "
"u'inscription_wizard-current_step': [u'3'], "
"u'submit': [u'next step'], u'3-reglement': [u'on'], "
"u'csrfmiddlewaretoken': [u'X'], "
"u'3-keywords': [u''], "
"u'files[]': [u'']}, u'2': {u'2-co_first_name': [u''], "
"u'inscription_wizard-current_step': [u'2'], "
"u'2-co_fonction': [u''], u'2-name': [u'Dupond'], "
"u'submit': [u'next step'], "
"u'2-first_name': [u'Xavier'], "
"u'csrfmiddlewaretoken': [u'X']}}}"
)

output = (
"{\n"
" 'step_files': {\n"
" u'1': {},\n"
" u'0': {},\n"
" u'3': {},\n"
" u'2': {}\n"
" },\n"
" 'step': u'4',\n"
" 'extra_data': {},\n"
" 'step_data': {\n"
" u'1': {\n"
" u'1-fax': [u''],\n"
" u'1-site': [u''],\n"
" u'inscription_wizard-current_step': [u'1'],\n"
" u'1-city': [u'paris'],\n"
" u'1-zip_code': [u'XXXXX'],\n"
" u'submit': [u'next step'],\n"
" u'1-phone': [u'XX XX XX XX XX'],\n"
" u'csrfmiddlewaretoken': [u'X'],\n"
" u'1-address': [u'xxx xxx xxx.'],\n"
" u'1-mobile_phone': [u'XX XX XX XX XX']\n"
" },\n"
" u'0': {\n"
" u'0-email': [u'xxx@xxx.xx'],\n"
" u'inscription_wizard-current_step': [u'0'],\n"
" u'submit': [u'next step'],\n"
" u'csrfmiddlewaretoken': [u'X'],\n"
" u'0-origin': [u'0']\n"
" },\n"
" u'3': {\n"
" u'3-honeypot': [u''],\n"
" u'3-select_paiement': [u'1'],\n"
" u'inscription_wizard-current_step': [u'3'],\n"
" u'submit': [u'next step'],\n"
" u'3-reglement': [u'on'],\n"
" u'csrfmiddlewaretoken': [u'X'],\n"
" u'3-keywords': [u''],\n"
" u'files[]': [u'']\n"
" },\n"
" u'2': {\n"
" u'2-co_first_name': [u''],\n"
" u'inscription_wizard-current_step': [u'2'],\n"
" u'2-co_fonction': [u''],\n"
" u'2-name': [u'Dupond'],\n"
" u'submit': [u'next step'],\n"
" u'2-first_name': [u'Xavier'],\n"
" u'csrfmiddlewaretoken': [u'X']\n"
" }\n"
" }\n"
"}"
)
self.assertEquals(indent_dict(input), output)


def module_func(*args, **kwargs):
"""Used by dispatch tests"""
return 'blah'
Expand Down