Skip to content
This repository
Browse code

Added dynamic dashboard nav.

Use the fns in app.dashboard.nav to edit
  • Loading branch information...
commit 5bf851d8c187d7f6b948583a93655e09eea56fd2 1 parent fdee8c4
David Winterbottom authored February 24, 2012
1  oscar/apps/dashboard/app.py
@@ -29,6 +29,7 @@ def get_urls(self):
29 29
             url(r'^users/', include(self.users_app.urls)),
30 30
             url(r'^promotions/', include(self.promotions_app.urls)),
31 31
         )
  32
+
32 33
         return self.post_process_urls(urlpatterns)
33 34
 
34 35
     def get_url_decorator(self, url_name):
5  oscar/apps/dashboard/catalogue/app.py
@@ -3,6 +3,11 @@
3 3
 
4 4
 from oscar.core.application import Application
5 5
 from oscar.apps.dashboard.catalogue import views
  6
+from oscar.apps.dashboard.nav import register, Node
  7
+
  8
+node = Node('Catalogue')
  9
+node.add_child(Node('Products', 'dashboard:catalogue-product-list'))
  10
+register(node)
6 11
 
7 12
 
8 13
 class CatalogueApplication(Application):
1  oscar/apps/dashboard/models.py
... ...
@@ -0,0 +1 @@
  1
+
58  oscar/apps/dashboard/nav.py
... ...
@@ -0,0 +1,58 @@
  1
+from django.core.urlresolvers import reverse
  2
+
  3
+_nodes = []
  4
+
  5
+
  6
+class Node(object):
  7
+
  8
+    def __init__(self, label, url_name=None, url_args=None, url_kwargs=None, access_fn=None):
  9
+        self.label = label
  10
+        self.url_name = url_name
  11
+        self.url_args = url_args
  12
+        self.url_kwargs = url_kwargs
  13
+        self.access_fn = access_fn
  14
+        self.children = []
  15
+
  16
+    @property
  17
+    def is_heading(self):
  18
+        return self.url_name is None
  19
+
  20
+    @property
  21
+    def url(self):
  22
+        return reverse(self.url_name, args=self.url_args, kwargs=self.url_kwargs)
  23
+
  24
+    def add_child(self, node):
  25
+        self.children.append(node)
  26
+
  27
+    def is_visible(self, user):
  28
+        if not self.access_fn:
  29
+            return True
  30
+        return self.access_fn(user)
  31
+
  32
+    def filter(self, user):
  33
+        if not self.is_visible(user):
  34
+            return None
  35
+        node = Node(self.label, self.url_name, self.access_fn)
  36
+        for child in self.children:
  37
+            if child.is_visible(node):
  38
+                node.add_child(child)
  39
+        return node
  40
+
  41
+    def has_children(self):
  42
+        return len(self.children) > 0
  43
+
  44
+def register(node):
  45
+    _nodes.append(node)
  46
+
  47
+def flush():
  48
+    _nodes = []
  49
+
  50
+def get_nodes(user):
  51
+    nodes = []
  52
+    for node in _nodes:
  53
+        filtered_node = node.filter(user)
  54
+        if filtered_node:
  55
+            nodes.append(node)
  56
+    return nodes
  57
+
  58
+
6  oscar/apps/dashboard/orders/app.py
@@ -3,6 +3,12 @@
3 3
 
4 4
 from oscar.core.application import Application
5 5
 from oscar.apps.dashboard.orders import views
  6
+from oscar.apps.dashboard.nav import register, Node
  7
+
  8
+node = Node('Manage orders')
  9
+node.add_child(Node('Orders', 'dashboard:order-list'))
  10
+node.add_child(Node('Statistics', 'dashboard:order-summary'))
  11
+register(node)
6 12
 
7 13
 
8 14
 class OrdersDashboardApplication(Application):
2  oscar/apps/dashboard/orders/views.py
@@ -27,6 +27,8 @@
27 27
 EventHandler = get_class('order.processing', 'EventHandler')
28 28
 
29 29
 
  30
+
  31
+
30 32
 class OrderSummaryView(TemplateView):
31 33
     template_name = 'dashboard/orders/summary.html'
32 34
 
6  oscar/apps/dashboard/promotions/app.py
@@ -4,6 +4,12 @@
4 4
 from oscar.core.application import Application
5 5
 from oscar.apps.dashboard.promotions import views
6 6
 from oscar.apps.promotions.conf import PROMOTION_CLASSES
  7
+from oscar.apps.dashboard.nav import register, Node
  8
+
  9
+node = Node('Content blocks')
  10
+node.add_child(Node('All blocks', 'dashboard:promotion-list'))
  11
+node.add_child(Node('By page', 'dashboard:promotion-list-by-page'))
  12
+register(node)
7 13
 
8 14
 
9 15
 class PromotionsDashboardApplication(Application):
4  oscar/apps/dashboard/reports/app.py
@@ -3,6 +3,10 @@
3 3
 
4 4
 from oscar.core.application import Application
5 5
 from oscar.apps.dashboard.reports import views
  6
+from oscar.apps.dashboard.nav import register, Node
  7
+
  8
+node = Node('Reports', 'dashboard:reports-index')
  9
+register(node)
6 10
 
7 11
 
8 12
 class ReportsApplication(Application):
4  oscar/apps/dashboard/users/app.py
@@ -3,6 +3,10 @@
3 3
 
4 4
 from oscar.core.application import Application
5 5
 from oscar.apps.dashboard.users import views
  6
+from oscar.apps.dashboard.nav import register, Node
  7
+
  8
+node = Node('Users', 'dashboard:users-index')
  9
+register(node)
6 10
 
7 11
 
8 12
 class UserManagementApplication(Application):
17  oscar/templates/dashboard/layout.html
@@ -22,11 +22,24 @@
22 22
 
23 23
     <div class="container-fluid dashboard">
24 24
         <div class="row-fluid">
25  
-			{% dashboard_navigation request.user %}
  25
+			{% dashboard_navigation %}
26 26
 			<a href="{% url dashboard:index %}">Dashboard</a>
27 27
 			<ul class="primary-nav">
28 28
 				{% for item in nav_items %}
29  
-				<li><a class="submenu" href="{{ item.url }}">{{ item.text }}</a></li>
  29
+				<li>
  30
+				{% if item.is_heading %}
  31
+				<a href="#" class="submenu">{{ item.label }}</a>
  32
+				{% else %}
  33
+				<a class="submenu" href="{{ item.url }}">{{ item.label }}</a>
  34
+				{% endif %}
  35
+				{% if item.has_children %}
  36
+				<ul>
  37
+					{% for subitem in item.children %}
  38
+					<li><a href="{{ subitem.url }}">{{ subitem.label }}</a></li>
  39
+					{% endfor %}
  40
+				</ul>
  41
+				{% endif %}
  42
+				</li>
30 43
 				{% endfor %}
31 44
 			</ul>
32 45
 
21  oscar/templatetags/dashboard_tags.py
@@ -2,6 +2,7 @@
2 2
 from django.core.urlresolvers import reverse
3 3
 
4 4
 from oscar.apps.order.models import Order
  5
+from oscar.apps.dashboard.nav import get_nodes
5 6
 
6 7
 
7 8
 def get_num_user_orders(parser, token):
@@ -25,20 +26,19 @@ def render(self, context):
25 26
 
26 27
 
27 28
 def dashboard_navigation(parser, token):
28  
-    try:
29  
-        tag_name, user = token.split_contents()
30  
-    except ValueError:
31  
-        raise template.TempalteSyntaxError("User required for dashboard navigation tag")
32  
-    return DashboardNavigationNode(user)
  29
+    return DashboardNavigationNode()
33 30
 
34 31
 
35 32
 class DashboardNavigationNode(template.Node):
36  
-    def __init__(self, user):
37  
-        self.user = template.Variable(user)
38 33
 
39 34
     def render(self, context):
  35
+        user = context['user']
  36
+        context['nav_items'] = get_nodes(user)
  37
+        return ''
  38
+
  39
+    def asdf(sefl):
  40
+
40 41
         # This needs to be made dynamic, using the user to filter
41  
-        self.items = []
42 42
         self.add_item('See statistics', 'dashboard:order-summary')
43 43
         self.add_item('Manage orders', 'dashboard:order-list')
44 44
         self.add_item('View reports', 'dashboard:reports-index')
@@ -48,8 +48,5 @@ def render(self, context):
48 48
         context['nav_items'] = self.items
49 49
         return ''
50 50
 
51  
-    def add_item(self, text, url_name):
52  
-        self.items.append({'text': text,
53  
-                           'url': reverse(url_name)})
54  
-
55 51
 register.tag('dashboard_navigation', dashboard_navigation)
  52
+

0 notes on commit 5bf851d

Please sign in to comment.
Something went wrong with that request. Please try again.