Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 111 lines (95 sloc) 3.493 kb
9966d47 @chrisglass Source encoding changes for menus app
chrisglass authored
1 # -*- coding: utf-8 -*-
5c9aa1f @digi604 imports now working, show_menu displays nothing yet
digi604 authored
2 from menus.base import Modifier
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
3 from menus.menu_pool import menu_pool
a36165f @digi604 some other menu refactoring... building up the api
digi604 authored
4
5 class Marker(Modifier):
6 """
7 searches the current selected node and marks them.
8 current_node: selected = True
9 siblings: sibling = True
10 descendants: descendant = True
11 ancestors: ancestor = True
12 """
38225bb @digi604 testsuite working again
digi604 authored
13 def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
14 if post_cut or breadcrumb:
bda49b2 @digi604 menu_levels working. nav-extenders correctly hiding if not assigned
digi604 authored
15 return nodes
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
16 selected = None
17 root_nodes = []
18 for node in nodes:
73bebf0 @digi604 cut levels working... only some inactive levels one level off
digi604 authored
19 if not hasattr(node, "descendant"):
20 node.descendant = False
21 if not hasattr(node, "ancestor"):
22 node.ancestor = False
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
23 if not node.parent:
24 if selected and not selected.parent:
25 node.sibling = True
bda49b2 @digi604 menu_levels working. nav-extenders correctly hiding if not assigned
digi604 authored
26 root_nodes.append(node)
3c4ad7c @timgraham Removed trailing whitespace and blank lines.
timgraham authored
27 if node.selected:
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
28 if node.parent:
098811c @kezabelle replace single character variables with .. slightly better/longer ones. ...
kezabelle authored
29 newnode = node
30 while newnode.parent:
31 newnode = newnode.parent
32 newnode.ancestor = True
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
33 for sibling in node.parent.children:
34 if not sibling.selected:
35 sibling.sibling = True
36 else:
098811c @kezabelle replace single character variables with .. slightly better/longer ones. ...
kezabelle authored
37 for root_node in root_nodes:
38 if not root_node.selected:
39 root_node.sibling = True
3c4ad7c @timgraham Removed trailing whitespace and blank lines.
timgraham authored
40 if node.children:
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
41 self.mark_descendants(node.children)
42 selected = node
43 if node.children:
44 node.is_leaf_node = False
45 else:
46 node.is_leaf_node = True
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
47 return nodes
3c4ad7c @timgraham Removed trailing whitespace and blank lines.
timgraham authored
48
a36165f @digi604 some other menu refactoring... building up the api
digi604 authored
49 def mark_descendants(self, nodes):
50 for node in nodes:
51 node.descendant = True
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
52 self.mark_descendants(node.children)
53
2d19d5c @digi604 all tests pass, nav extenders fully working now
digi604 authored
54
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
55
56 class Level(Modifier):
57 """
58 marks all node levels
59 """
eef231d @digi604 breadcrumb, language_chooser, sub_menu working... by id still missing
digi604 authored
60 post_cut = True
3c4ad7c @timgraham Removed trailing whitespace and blank lines.
timgraham authored
61
38225bb @digi604 testsuite working again
digi604 authored
62 def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
63 if breadcrumb:
64 return nodes
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
65 for node in nodes:
3c4ad7c @timgraham Removed trailing whitespace and blank lines.
timgraham authored
66
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
67 if not node.parent:
68 if post_cut:
69 node.menu_level = 0
70 else:
71 node.level = 0
72 self.mark_levels(node, post_cut)
3c4ad7c @timgraham Removed trailing whitespace and blank lines.
timgraham authored
73
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
74 return nodes
3c4ad7c @timgraham Removed trailing whitespace and blank lines.
timgraham authored
75
76
eef231d @digi604 breadcrumb, language_chooser, sub_menu working... by id still missing
digi604 authored
77 def mark_levels(self, node, post_cut):
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
78 for child in node.children:
eef231d @digi604 breadcrumb, language_chooser, sub_menu working... by id still missing
digi604 authored
79 if post_cut:
80 child.menu_level = node.menu_level + 1
81 else:
82 child.level = node.level + 1
83 self.mark_levels(child, post_cut)
6e71206 @digi604 show menu working. marking of nodes working. modifiers working. cutlevel...
digi604 authored
84
2d19d5c @digi604 all tests pass, nav extenders fully working now
digi604 authored
85
a36165f @digi604 some other menu refactoring... building up the api
digi604 authored
86
3c2fc7d @stefanfoulis added ability to display a page only for anonymous user (as well as only...
stefanfoulis authored
87 class AuthVisibility(Modifier):
a36165f @digi604 some other menu refactoring... building up the api
digi604 authored
88 """
89 Remove nodes that are login required or require a group
90 """
38225bb @digi604 testsuite working again
digi604 authored
91 def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
92 if post_cut or breadcrumb:
bda49b2 @digi604 menu_levels working. nav-extenders correctly hiding if not assigned
digi604 authored
93 return nodes
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
94 final = []
95 for node in nodes:
3c2fc7d @stefanfoulis added ability to display a page only for anonymous user (as well as only...
stefanfoulis authored
96 if (node.attr.get('visible_for_authenticated', True) and \
97 request.user.is_authenticated()) or \
98 (node.attr.get('visible_for_anonymous', True) and \
99 not request.user.is_authenticated()):
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
100 final.append(node)
3c2fc7d @stefanfoulis added ability to display a page only for anonymous user (as well as only...
stefanfoulis authored
101 else:
102 if node.parent and node in node.parent.children:
103 node.parent.children.remove(node)
1b16cd4 @digi604 monster commit for menu refactor
digi604 authored
104 return final
105
2d19d5c @digi604 all tests pass, nav extenders fully working now
digi604 authored
106
107 def register():
108 menu_pool.register_modifier(Marker)
3c2fc7d @stefanfoulis added ability to display a page only for anonymous user (as well as only...
stefanfoulis authored
109 menu_pool.register_modifier(AuthVisibility)
2d19d5c @digi604 all tests pass, nav extenders fully working now
digi604 authored
110 menu_pool.register_modifier(Level)
Something went wrong with that request. Please try again.