Skip to content

Commit

Permalink
add toolbar feature. override Index.html so as to better integrate wi…
Browse files Browse the repository at this point in the history
…th DjangoCMS
  • Loading branch information
PeterCat12 committed May 2, 2017
1 parent e24526f commit e9cff5a
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 66 deletions.
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,55 @@ Add the following to `INSTALLED_APPS` in `settings.py`
'django_version_viewer'
]

Add `django_version_viewer` include to `urls.py`
## Add django_version_viewer urls and extend `admin/index.html`


Django Version Viewer needs to extend the `admin/index.html` and append it's urls to your `urls.py`. In your `urls.py` add:

admin.site.index_template = 'admin/custom_index.html'
admin.autodiscover()

urlpatterns = [
...
url(r'^django_version_viewer/', include('django_version_viewer.urls')),
...
]

You can set your own access permissions on the template tag and route by defining your own
`Accessor` class. This class must have a `allow_access` method that returns a `boolean`. By defualt,
django_version_viewer only allows superusers access to the route and template tag.
In your `templates` dir, create a `custom_index.html`.

# Django Version Viewer settings:
# default class only allows superusers access
ACCESSOR_CLASS_PATH = 'mypathto.my.AccessorClass'
<!-- custom_index.html -->
{% extends "admin/index.html" %}

{% load i18n admin_static pip_version_viewer_tags %}

## Your Project is NOT using DjangoCMS
{% block content %}
{% show_pip_package_versions %}
{{ block.super }}
{% endblock %}

Override the `base_site.html` django Admin template (or the template of your choosing) by creating a `base.html` file inside a `templates/admin` directory in your project (Refer to the example project's
`base_site_regular.html`).

Make sure you insert the necessary `src` and `link` blocks so that the popup modal works properly.
<!-- only add this if you are NOT using djangoCMS -->
{% block extrahead %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
{% endblock %}

If you are using DjangoCMS the added style sheet and js might interfere with DjangoCMS's styling. In this case you should
create a file called `admin/inc/userlinks.html` inside your `templates` directory.

<!-- userlinks.html -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Finally, load the template tags file and insert the template tag where ever you want the "Installed Versions" link to show up:

{% load pip_version_viewer_tags %}
{% show_pip_package_versions %}

## Your Project IS using DjangoCMS
## Permissions

Override the `base_site.html` django Admin template (or the template of your choosing) by creating a `base.html` file inside a `templates/admin` directory in your project (Refer to the example project's
`base_site.html`. This file is based off of DjangoCMS's `base_site.html` file).
You can set your own access permissions on the template tag and route by defining your own
`Accessor` class. This class must have a `allow_access` method that returns a `boolean`. By defualt,
django_version_viewer only allows superusers access to the route and template tag.

Make sure you insert the necessary `src` and `link` blocks inside `admin/inc/userlinks.html`so that the popup modal works properly.
# Django Version Viewer settings:
# default class only allows superusers access
ACCESSOR_CLASS_PATH = 'mypathto.my.AccessorClass'
32 changes: 32 additions & 0 deletions django_version_viewer/cms_toolbars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.core.urlresolvers import reverse
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from cms.api import get_page_draft
from cms.toolbar_pool import toolbar_pool
from cms.toolbar_base import CMSToolbar

from pydoc import locate

accessor_class = locate(
getattr(settings, 'ACCESSOR_CLASS_PATH', 'django_version_viewer.mixins.Accessor'))
accessor = accessor_class()


@toolbar_pool.register
class DjangoViewerToolbar(CMSToolbar):
def populate(self):
# always use draft if we have a page
self.page = get_page_draft(self.request.current_page)

if not self.page:
return

disabled = not accessor.allow_access(self.request)
current_page_menu = self.toolbar.get_or_create_menu('page')

current_page_menu.add_modal_item(
_('View Pip Packages'),
url=reverse('django_version_viewer_toolbar'),
disabled=disabled
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "admin/base_site.html" %}


<!-- {% block content %}
{% block content %}
{% if success %}
{% comment %}
Need to use js reload since Django doesn't provide a good
Expand All @@ -23,4 +23,4 @@ <h4 class="modal-title">Versions</h4>

</div>
</div>
{% endblock %} -->
{% endblock %}
2 changes: 1 addition & 1 deletion django_version_viewer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
urlpatterns = [
url(r'^$', views.DjangoVersionViewer.as_view(), name='django_version_viewer'),
url(r'^csv/$', views.DjangoVersionViewerCSV.as_view(), name='django_version_viewer_csv'),
# url(r'^toolbar/$', views.DjangoVersionViewerToolBar.as_view(), name='django_version_viewer_toolbar'),
url(r'^toolbar/$', views.DjangoVersionViewerToolBar.as_view(), name='django_version_viewer_toolbar'),
]
2 changes: 1 addition & 1 deletion django_version_viewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class DjangoVersionViewerToolBar(TemplateView):
template_name = "test.html"
template_name = "toolbar.html"

def get_context_data(self, **kwargs):
context = super(DjangoVersionViewerToolBar, self).get_context_data(**kwargs)
Expand Down
4 changes: 4 additions & 0 deletions example18/example18/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from django.conf.urls import include, url
from django.contrib import admin

admin.site.index_template = 'admin/custom_index.html'
admin.autodiscover()


urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^django_version_viewer/', include('django_version_viewer.urls')),
Expand Down
21 changes: 0 additions & 21 deletions example18/templates/admin/base_site.html

This file was deleted.

21 changes: 0 additions & 21 deletions example18/templates/admin/base_site_regular.html

This file was deleted.

8 changes: 8 additions & 0 deletions example18/templates/admin/custom_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "admin/index.html" %}

{% load i18n admin_static pip_version_viewer_tags %}

{% block content %}
{% show_pip_package_versions %}
{{ block.super }}
{% endblock %}

0 comments on commit e9cff5a

Please sign in to comment.