Permalink
Browse files

Added dynamic dashboard nav.

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

0 comments on commit 5bf851d

Please sign in to comment.