Skip to main content

Show staff-only controls at website frontents

Project description

django-staff-toolbar
====================

Displaying staff-only controls at a webpage.

Features:

* Linking to the admin page of the current object.
* Full configuration of the displayed toolbar items.
* API for adding custom menu items.

.. image:: https://github.com/edoburu/django-staff-toolbar/raw/master/docs/images/staff_toolbar.png
:width: 142px
:height: 136px
:alt: django-staff-toolbar preview


Installation
============

First install the module, preferably in a virtual environment:

.. code-block:: bash

pip install django-staff-toolbar


Configuration
-------------

Add the application to ``settings.py``:

.. code-block:: python

INSTALLED_APPS += (
'staff_toolbar',
)

Make sure the ``django.core.context_processors.request`` is included in ``TEMPLATE_CONTEXT_PROCESSORS``.

Add the HTML widget to the template:

.. code-block:: html+django

{% load staff_toolbar_tags %}

{% staff_toolbar %}

Make sure the layout is loaded in the template:

.. code-block:: html+django

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}staff_toolbar/staff_toolbar.css" />

Layout
------

By default, a simple layout is included.
You can change this layout to your own liking.

The source SASS file is included, making it easier to
integrate this into your project stylesheets when needed.


Customizing the admin URL
=========================

The admin URL is auto-detected using:

* The ``object`` variable in the template.
* The ``view.object`` variable in the template.

In some cases, this is not sufficient. When the auto-detected "Change object"
link does not point to the right page, this can be resolved using two methods:

Using the view
--------------

When your class-based-view implements ``staff_toolbar.views.StaffUrlMixin``,
that information will be used to render the proper "Change object" link.

This requires Django 1.5, which exports the ``view`` variable to the template.

Using the template
------------------

In the template, you can include:

.. code-block:: html+django

{% set_staff_object page %}

When needed, the URL can also be set:

.. code-block:: html+django

{% set_staff_url %}{% url 'dashboard:catalogue-product' object.id %}{% end_set_staff_url %}


Customizing the menu
====================

The default menu settings are::

.. code-block:: python

STAFF_TOOLBAR_ITEMS = (
'staff_toolbar.items.AdminIndexLink',
'staff_toolbar.items.ChangeObjectLink',
'staff_toolbar.items.LogoutLink',
)

Each line represents a callable, which is called using ``(request, context)``.
When a tuple is included, this is converted into a new ``Group`` object,
causing an additional ``<ul>`` tag to appear in the output.

A more complex example:

.. code-block:: python

from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from staff_toolbar import toolbar_item, toolbar_link, toolbar_title, toolbar_literal

STAFF_TOOLBAR_ITEMS = (
'staff_toolbar.items.AdminIndexLink',
'staff_toolbar.items.ChangeObjectLink',
(
toolbar_title(_("User")),
toolbar_link(url=reverse_lazy('admin:password_change'), title=_("Change password")),
'staff_toolbar.items.LogoutLink',
)
)

The ``toolbar_title()`` and ``toolbar_item()`` functions allow to pass additional arguments
to the items, without having to load them already in the settings.

It's also perfectly possible to instantiate the actual classes directly,
however this may risk import errors as it causes your settings module to load a lot of other code.
The following is functionally equivalent to the previous example:

.. code-block:: python

from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from staff_toolbar.items import AdminIndexLink, ChangeObjectLink, Group, ToolbarTitle, Link, LogoutLink

STAFF_TOOLBAR_ITEMS = (
AdminIndexLink(),
ChangeObjectLink(),
Group(
ToolbarTitle(_("User")),
Link(url=reverse_lazy('admin:password_change'), title=_("Change password")),
LogoutLink(),
)
)


Caveats
=======

For HTTPS sites with ``SESSION_COOKIE_SECURE = True`` the toolbar obviously
won't show up in the standard pages that are served by HTTP.

Either display all pages on HTTPS (which is the Right Way™ after all),
or please provide a good pull request that solves this nicely for mixed sites.


Contributing
============

This module is designed to be generic, and easy to plug into your site.
Pull requests and improvements are welcome!

If you have any other valuable contribution, suggestion or idea, please let us know as well!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-staff-toolbar-1.1.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

django_staff_toolbar-1.1-py2-none-any.whl (14.5 kB view details)

Uploaded Python 2

File details

Details for the file django-staff-toolbar-1.1.tar.gz.

File metadata

File hashes

Hashes for django-staff-toolbar-1.1.tar.gz
Algorithm Hash digest
SHA256 63d48c3654c448c29e282cb14181c8c61610f039f00eef98dde8336090ffdd51
MD5 1684ce0cf8b0480817523260e57d1205
BLAKE2b-256 2266526ea9bff0e12e31a751abe6899ccada0dc5f9a133406aa784594fdb03d2

See more details on using hashes here.

File details

Details for the file django_staff_toolbar-1.1-py2-none-any.whl.

File metadata

File hashes

Hashes for django_staff_toolbar-1.1-py2-none-any.whl
Algorithm Hash digest
SHA256 06f50eb63c2643fa7d7115f7ac065a8752017c35cfa97e51c37c630977205f8d
MD5 4bc8a3414ddbcaae263aa547a4450611
BLAKE2b-256 a12621e024390731608249bff44b68793977dad1a96d782b68cecc6c4aa8820b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page