Skip to content

Commit 4d0ec38

Browse files
author
Evan Borgstrom
committed
Release 1.2.0
1 parent 5d71abf commit 4d0ec38

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
django-simple-menu changelog
22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33

4+
Version 1.2.0 - Released January 15, 2016
5+
- Require Django 1.6+ now (this is because we now use slugify instead of a regex)
6+
- Ensure thread safety #42
7+
- Allow complex checks to be built by subclassing MenuItem #43
8+
49
Version 1.1.2 - Released November 21, 2015
510
- Fix bug introduced in 1.1.1 surrounding child sorting #38
611
- Improve tests

README.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ Please ensure that you have ``django.core.context_processors.request`` listed in
3232
``TEMPLATE_CONTEXT_PROCESSORS`` setting.
3333

3434
For each of your own apps that you want to expose a menu create a new file named ``menus.py`` and
35-
define your menus using the ``Menu`` and ``MenuItem`` classes.
35+
define your menus using the ``Menu`` and ``MenuItem`` classes you can import from the ``menu``
36+
namespace.
3637

37-
In a template you want to render a menu first ``{% load menu %}`` then inside a block call
38-
``{% generate_menu %}`` and a new varaible named ``menus`` will be added to the context. You can
39-
now iterate over this ``menus`` object to render your menus.
38+
In a template you want to render a menu first ``{% load menu %}`` then call ``{% generate_menu %}``
39+
inside a block and a new varaible named ``menus`` will be added to the context. You can now iterate
40+
over this ``menus`` object to render your menus.
4041

4142
To quickly see everything in action and evaluate django-simple-menut please check out the
4243
`example project`_.

docs/usage.rst

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ access from within your templates. This way you can have a menu for your main
99
navigation, another menu for logged in users, another menu for anonymous users,
1010
etc.
1111

12+
.. note::
13+
14+
Since we use the ``menu`` namespace you will get ``ImportError`` if you
15+
have any files named ``menu.py``, ensure you use a plural version:
16+
``menus.py``
17+
1218
To define your menus you need to create a file named ``menus.py`` inside of the
1319
app that you wish to hook menus up to. In the ``menus.py`` file you should
1420
import the ``Menu`` and ``MenuItem`` classes from the ``menu`` package::
@@ -116,5 +122,58 @@ You can use it like::
116122

117123
{% with menu=menus.main %}{% include "bootstrap-navbar.html" %}{% endwith %}
118124

119-
.. _menu __init__.py source file: https://github.com/fatbox/django-simple-menu/blob/master/menu/__init__.py
125+
126+
Check generalizations
127+
---------------------
128+
129+
If your application is dynamic enough, or complex enough, you may find that you
130+
want to generalize your check logic based on a permissions model, or something
131+
similar. To accomplish this you can create your own custom ``MenuItem``
132+
implementation with a ``check`` method.
133+
134+
This assumes you have a ``utils`` package.
135+
136+
``utils/menus.py``::
137+
138+
from django.core.urlresolvers import resolve
139+
140+
from menu import MenuItem
141+
142+
143+
class ViewMenuItem(MenuItem):
144+
"""Custom MenuItem that checks permissions based on the view associated
145+
with a URL"""
146+
147+
def check(self, request):
148+
"""Check permissions based on our view"""
149+
is_visible = True
150+
match = resolve(self.url)
151+
152+
# do something with match, and possibly change is_visible...
153+
154+
self.visible = is_visible
155+
156+
157+
``reports/menus.py``::
158+
159+
from utils.menus import ViewMenuItem
160+
161+
from menu import Menu, MenuItem
162+
163+
from django.core.urlresolvers import reverse
164+
165+
# Since we use ViewMenuItem here we do not need to define checks, instead
166+
# the check logic will change their visibility based on the permissions
167+
# attached to the views we reverse here.
168+
reports_children = (
169+
ViewMenuItem("Staff Only", reverse("reports.views.staff")),
170+
ViewMenuItem("Superuser Only", reverse("reports.views.superuser"))
171+
)
172+
173+
Menu.add_item("main", MenuItem("Reports Index",
174+
reverse("reports.views.index"),
175+
children=reports_children))
176+
177+
178+
.. _menu __init__.py source file: https://github.com/borgstrom/django-simple-menu/blob/master/menu/__init__.py
120179
.. _Twitter Bootstrap Navbar Component: http://twitter.github.com/bootstrap/components.html#navbar

menu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .menu import Menu, MenuItem
22

3-
__version__ = '1.1.2'
3+
__version__ = '1.2.0'
44
__url__ = 'https://github.com/borgstrom/django-simple-menu'

0 commit comments

Comments
 (0)