menus.modifiers: 73 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/menus/modifiers.py

Stats: 0 executed, 71 missed, 2 excluded, 38 ignored

  1. # -*- coding: utf-8 -*-
  2. from menus.base import Modifier
  3. from menus.menu_pool import menu_pool
  4. class Marker(Modifier):
  5. """
  6. searches the current selected node and marks them.
  7. current_node: selected = True
  8. siblings: sibling = True
  9. descendants: descendant = True
  10. ancestors: ancestor = True
  11. """
  12. def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
  13. if post_cut or breadcrumb:
  14. return nodes
  15. selected = None
  16. root_nodes = []
  17. for node in nodes:
  18. if not hasattr(node, "descendant"):
  19. node.descendant = False
  20. if not hasattr(node, "ancestor"):
  21. node.ancestor = False
  22. if not node.parent:
  23. if selected and not selected.parent:
  24. node.sibling = True
  25. root_nodes.append(node)
  26. if node.selected:
  27. if node.parent:
  28. newnode = node
  29. while newnode.parent:
  30. newnode = newnode.parent
  31. newnode.ancestor = True
  32. for sibling in node.parent.children:
  33. if not sibling.selected:
  34. sibling.sibling = True
  35. else:
  36. for root_node in root_nodes:
  37. if not root_node.selected:
  38. root_node.sibling = True
  39. if node.children:
  40. self.mark_descendants(node.children)
  41. selected = node
  42. if node.children:
  43. node.is_leaf_node = False
  44. else:
  45. node.is_leaf_node = True
  46. return nodes
  47. def mark_descendants(self, nodes):
  48. for node in nodes:
  49. node.descendant = True
  50. self.mark_descendants(node.children)
  51. class Level(Modifier):
  52. """
  53. marks all node levels
  54. """
  55. post_cut = True
  56. def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
  57. if breadcrumb:
  58. return nodes
  59. for node in nodes:
  60. if not node.parent:
  61. if post_cut:
  62. node.menu_level = 0
  63. else:
  64. node.level = 0
  65. self.mark_levels(node, post_cut)
  66. return nodes
  67. def mark_levels(self, node, post_cut):
  68. for child in node.children:
  69. if post_cut:
  70. child.menu_level = node.menu_level + 1
  71. else:
  72. child.level = node.level + 1
  73. self.mark_levels(child, post_cut)
  74. class AuthVisibility(Modifier):
  75. """
  76. Remove nodes that are login required or require a group
  77. """
  78. def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
  79. if post_cut or breadcrumb:
  80. return nodes
  81. final = []
  82. for node in nodes:
  83. if (node.attr.get('visible_for_authenticated', True) and \
  84. request.user.is_authenticated()) or \
  85. (node.attr.get('visible_for_anonymous', True) and \
  86. not request.user.is_authenticated()):
  87. final.append(node)
  88. else:
  89. if node.parent and node in node.parent.children:
  90. node.parent.children.remove(node)
  91. return final
  92. def register():
  93. menu_pool.register_modifier(Marker)
  94. menu_pool.register_modifier(AuthVisibility)
  95. menu_pool.register_modifier(Level)