Побить урл на сегменты и вытащить из него всю фигню

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def get_section_context(request, path):
path = path.split('/')
section_count = len(path)
if section_count > 31:
raise Http404
flt = {}
while path:
flt['parent__' * len(flt) + 'slug'] = path.pop()
flt['parent__' * (len(flt)-1) + 'language'] = request.LANGUAGE_CODE
if section_count == 1:
flt['parent__isnull'] = True
try:
item = Section.objects.get(**flt)
except Section.DoesNotExist:
raise Http404
if item.is_root_node():
root = item
ancestors = None
siblings = None
else:
ancestors = item.get_ancestors()
root = ancestors[0]
siblings = item.get_siblings(include_self=True)
submenu = root.get_children()
children = submenu if item is root else item.get_children()
return {'item': item, 'ancestors': ancestors,
'root': root, 'submenu': submenu,
'children': children, 'siblings': siblings}