Skip to content

Instantly share code, notes, and snippets.

@aishwarydhare
Last active March 4, 2019 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aishwarydhare/4a137797b0c44edfcd9f8a9baff1240a to your computer and use it in GitHub Desktop.
Save aishwarydhare/4a137797b0c44edfcd9f8a9baff1240a to your computer and use it in GitHub Desktop.
Creating a MenuList : Django Admin App
from collections import OrderedDict
from django.urls import resolve
from django.urls import reverse
def get_menu(context):
# [ 'display name', 'level 1 parent (optional)', 'django url', 'css icon', 'level 2 parent (optional)']
menus = [
['solo', '', 'solo_url', 'fa fa-desktop'],
['test', '', '', 'fa fa-desktop'],
['test1', 'test', '', ''],
['test2', 'test', 'all_items', '', 'test1'],
['test3', 'test', 'all_items2', '', 'test1'],
['test4', 'test', 'all_items3', '', 'test1'],
['test5', 'test', '', ''],
['test6', 'test', 'all_items', '', 'test5'],
['test7', 'test', 'all_items2', '', 'test5'],
['test8', 'test', 'all_items3', '', 'test5'],
]
menuList = OrderedDict()
for s in menus:
if s[1] != '' and len(s) == 4:
if 'level2_childs' not in menuList[s[1]]:
menuList[s[1]]['level2_childs'] = {s[0]: []}
else:
menuList[s[1]]['level2_childs'][s[0]] = []
if s[1] != '':
if len(s) > 4:
var = menuList[s[1]]['level2_childs'][s[4]]
var.append(s)
else:
menuList[s[1]]['childs'].append(s)
if s[2] != '':
s[2] = reverse(s[2])
s.append(resolve(s[2]).url_name)
else:
menuList[s[0]] = {}
menuList[s[0]]['childs'] = []
menuList[s[0]]['icon'] = s[3]
menuList[s[0]]['link_name'] = s[2]
if s[2] != '':
menuList[s[0]]['link'] = reverse(s[2])
try:
current_url = resolve(context.path_info).url_name
except Exception as e:
print('current url unable to detect in pages context', e)
else:
for key, value in menuList.items():
if menuList[key]['link_name'] == current_url:
menuList[key]['active'] = 'active'
break
elif len(menuList[key]['childs']) > 0:
for i in range(0, len(menuList[key]['childs'])):
if menuList[key]['childs'][i][3] == current_url:
# menuList[key]['childs'][i].append('active')
menuList[key]['active'] = 'active'
break
if 'level2_childs' in menuList[key]:
for level1_key in menuList[key]['level2_childs']:
arr = menuList[key]['level2_childs'][level1_key]
for i in range(0, len(arr)):
if arr[i][-1] == current_url:
arr[i].append('active')
menuList[key]['active'] = 'active'
break
return {'menu_list': menuList}
# Custom Filter
'''
@register.filter(name='get_length_by_key')
def get_length_by_key(val, key):
return len(val[key])
@register.filter(name='get_array_inside_dict_by_key')
def get_array_inside_dict_by_key(val, key):
return val[key]
'''
# Sample HTML
'''
<!-- using CSS from Inspinia Admin Template http://webapplayers.com/inspinia_admin-v2.9.1/ --->
{% for key, values in menu_list.items %}
<li class="{{ values.active }}">
<a href="{{ values.link }}">
<i class="{{ values.icon }}"></i>
<span class="nav-label">{{ key }}</span>
{% if values.childs|length > 0 %}
<span class="fa arrow"></span>
{% endif %}
</a>
{% if values.childs|length > 0 %}
<ul class="nav nav-second-level collapse">
{% for secondLevelMenu in values.childs %}
<li class="{{ secondLevelMenu.4 }}">
<a href="{{ secondLevelMenu.2 }}">
{{ secondLevelMenu.0 }}
{% if values.level2_childs|get_length_by_key:secondLevelMenu.0 > 0 %}
<span class="fa arrow"></span>
{% endif %}
</a>
{% if values.level2_childs|get_length_by_key:secondLevelMenu.0 > 0 %}
<ul class="nav nav-third-level collapse" aria-expanded="false">
{% for thirdLevelMenu in values.level2_childs|get_array_inside_dict_by_key:secondLevelMenu.0 %}
<li class="{{ thirdLevelMenu.6 }}">
<a href="{{ thirdLevelMenu.2 }}">
{{ thirdLevelMenu.0 }}
</a>
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment