Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
itminedu
synnefo
Commits
bbf36937
Commit
bbf36937
authored
Mar 01, 2016
by
Olga Brani
Committed by
Giorgos Korfiatis
May 25, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
admin: Improve & test flatten_dict_to_dl filter
parent
46b03539
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
14 deletions
+80
-14
snf-admin-app/synnefo_admin/admin/templates/admin/_user_details.html
...pp/synnefo_admin/admin/templates/admin/_user_details.html
+1
-1
snf-admin-app/synnefo_admin/admin/templatetags/admin_tags.py
snf-admin-app/synnefo_admin/admin/templatetags/admin_tags.py
+27
-12
snf-admin-app/synnefo_admin/admin/tests/__init__.py
snf-admin-app/synnefo_admin/admin/tests/__init__.py
+2
-1
snf-admin-app/synnefo_admin/admin/tests/templatetags.py
snf-admin-app/synnefo_admin/admin/tests/templatetags.py
+50
-0
No files found.
snf-admin-app/synnefo_admin/admin/templates/admin/_user_details.html
View file @
bbf36937
...
...
@@ -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>
...
...
snf-admin-app/synnefo_admin/admin/templatetags/admin_tags.py
View file @
bbf36937
# Copyright (C) 2010-201
4
GRNET S.A.
# Copyright (C) 2010-201
6
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
)))
snf-admin-app/synnefo_admin/admin/tests/__init__.py
View file @
bbf36937
# Copyright (C) 2010-201
4
GRNET S.A.
# Copyright (C) 2010-201
6
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
*
snf-admin-app/synnefo_admin/admin/tests/templatetags.py
0 → 100644
View file @
bbf36937
# 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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment