Commit bbf36937 authored by Olga Brani's avatar Olga Brani Committed by Giorgos Korfiatis

admin: Improve & test flatten_dict_to_dl filter

parent 46b03539
......@@ -96,7 +96,7 @@
<a href="" class="toggle-fade pull-right txt btn-toggle-info line-btn">Show raw data</a>
<!-- </div> -->
<dl class="dl-horizontal well fade-area vis area-0 clearfix">
{{ info|flatten_dict_to_dl }}
{% flatten_dict_to_dl info %}
</dl>
<pre class="info-data fade-area area-1 clearfix">{{ info }}</pre>
</div>
......
# Copyright (C) 2010-2014 GRNET S.A.
# Copyright (C) 2010-2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -483,15 +483,30 @@ def min_prefix():
else:
return ''
@register.filter
def flatten_dict_to_dl(d):
@register.simple_tag
def flatten_dict_to_dl(d, default_if_empty='-'):
"""
Recursively takes a self-nested dict and returns an HTML definition list --
WITHOUT opening and closing <dl> tags.
The dict is assumed to be in the proper format. For example, if ``var``
contains: ``{
'foo1': 'bar1',
'foo2': {
'foo3': 'bar3',
}
}``,
then ``{{ var|flatten_dict_to_dl }}`` would return::
<dt>foo1</dt><dd>bar1</dd><dt>foo3</dt><dd>bar3</dd>
"""
l = []
stack = d.items()
while stack:
k, v = stack.pop()
if isinstance(v, dict):
stack.extend(v.iteritems())
else:
a = '<dt>{0}</dt><dd>{1}</dd>'.format(k, v)
l.append(a)
return mark_safe(''.join(l))
if isinstance(d, dict):
stack = d.items()
while stack:
k, v = stack.pop()
if isinstance(v, dict):
stack.extend(v.iteritems())
else:
a = '<dt>{0}</dt><dd>{1}</dd>'.format(k, v or default_if_empty)
l.append(a)
return mark_safe(''.join(reversed(l)))
# Copyright (C) 2010-2014 GRNET S.A.
# Copyright (C) 2010-2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -20,3 +20,4 @@ from synnefo_admin.admin.tests.utils import *
from synnefo_admin.admin.tests.users import *
from synnefo_admin.admin.tests.projects import *
from synnefo_admin.admin.tests.vms import *
from synnefo_admin.admin.tests.templatetags import *
# Copyright (C) 2010-2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.test import TestCase
from synnefo_admin.admin.templatetags import admin_tags
class TemplateTagsTest(TestCase):
def test_flatten_dict_to_dl(self):
input1 = {
'foo': 'bar'
}
output1 = '<dt>foo</dt><dd>bar</dd>'
self.assertEqual(admin_tags.flatten_dict_to_dl(input1), output1)
input2 = {
'foo': 'bar',
'foo0': {
'foo1': 'bar1'
}
}
output2 = '<dt>foo</dt><dd>bar</dd><dt>foo1</dt><dd>bar1</dd>'
self.assertEqual(admin_tags.flatten_dict_to_dl(input2), output2)
input3 = [1, 2, 3]
output3 = ''
self.assertEqual(admin_tags.flatten_dict_to_dl(input3), output3)
input4 = {
'foo': ''
}
output4 = '<dt>foo</dt><dd>-</dd>'
self.assertEqual(admin_tags.flatten_dict_to_dl(input4), output4)
input5 = input4
output5 = '<dt>foo</dt><dd>boo</dd>'
self.assertEqual(admin_tags.flatten_dict_to_dl(input5, 'boo'), output5)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment