Skip to content

Commit

Permalink
Added dynamic dashboard nav.
Browse files Browse the repository at this point in the history
Use the fns in app.dashboard.nav to edit
  • Loading branch information
codeinthehole committed Feb 24, 2012
1 parent fdee8c4 commit 5bf851d
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 14 deletions.
1 change: 1 addition & 0 deletions oscar/apps/dashboard/app.py
Expand Up @@ -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):
Expand Down
5 changes: 5 additions & 0 deletions oscar/apps/dashboard/catalogue/app.py
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions oscar/apps/dashboard/models.py
@@ -0,0 +1 @@

58 changes: 58 additions & 0 deletions oscar/apps/dashboard/nav.py
@@ -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


6 changes: 6 additions & 0 deletions oscar/apps/dashboard/orders/app.py
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions oscar/apps/dashboard/orders/views.py
Expand Up @@ -27,6 +27,8 @@
EventHandler = get_class('order.processing', 'EventHandler')




class OrderSummaryView(TemplateView):
template_name = 'dashboard/orders/summary.html'

Expand Down
6 changes: 6 additions & 0 deletions oscar/apps/dashboard/promotions/app.py
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions oscar/apps/dashboard/reports/app.py
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions oscar/apps/dashboard/users/app.py
Expand Up @@ -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):
Expand Down
17 changes: 15 additions & 2 deletions oscar/templates/dashboard/layout.html
Expand Up @@ -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>

Expand Down
21 changes: 9 additions & 12 deletions oscar/templatetags/dashboard_tags.py
Expand Up @@ -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):
Expand All @@ -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')
Expand All @@ -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.