From e9cff5a57f1c27c623aa3ffddf292f26b5d9fe22 Mon Sep 17 00:00:00 2001 From: petercat12 Date: Tue, 2 May 2017 11:41:26 -0500 Subject: [PATCH] add toolbar feature. override Index.html so as to better integrate with DjangoCMS --- README.md | 51 ++++++++++++------- django_version_viewer/cms_toolbars.py | 32 ++++++++++++ .../templates/{test.html => toolbar.html} | 6 +-- django_version_viewer/urls.py | 2 +- django_version_viewer/views.py | 2 +- example18/example18/urls.py | 4 ++ example18/templates/admin/base_site.html | 21 -------- .../templates/admin/base_site_regular.html | 21 -------- example18/templates/admin/custom_index.html | 8 +++ 9 files changed, 81 insertions(+), 66 deletions(-) create mode 100644 django_version_viewer/cms_toolbars.py rename django_version_viewer/templates/{test.html => toolbar.html} (90%) delete mode 100644 example18/templates/admin/base_site.html delete mode 100644 example18/templates/admin/base_site_regular.html create mode 100644 example18/templates/admin/custom_index.html diff --git a/README.md b/README.md index 4566a0e..192ab0e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,13 @@ 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 = [ ... @@ -20,34 +26,41 @@ Add `django_version_viewer` include to `urls.py` ... ] -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' + + {% 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. + + {% block extrahead %} + + + + {% 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. + + -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' diff --git a/django_version_viewer/cms_toolbars.py b/django_version_viewer/cms_toolbars.py new file mode 100644 index 0000000..975b342 --- /dev/null +++ b/django_version_viewer/cms_toolbars.py @@ -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 + ) diff --git a/django_version_viewer/templates/test.html b/django_version_viewer/templates/toolbar.html similarity index 90% rename from django_version_viewer/templates/test.html rename to django_version_viewer/templates/toolbar.html index 488a5a6..42e6c07 100644 --- a/django_version_viewer/templates/test.html +++ b/django_version_viewer/templates/toolbar.html @@ -1,6 +1,6 @@ +{% extends "admin/base_site.html" %} - - +{% endblock %} diff --git a/django_version_viewer/urls.py b/django_version_viewer/urls.py index 3bba326..076fd2f 100644 --- a/django_version_viewer/urls.py +++ b/django_version_viewer/urls.py @@ -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'), ] diff --git a/django_version_viewer/views.py b/django_version_viewer/views.py index 96912d2..871de73 100644 --- a/django_version_viewer/views.py +++ b/django_version_viewer/views.py @@ -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) diff --git a/example18/example18/urls.py b/example18/example18/urls.py index 4009dec..41c76df 100644 --- a/example18/example18/urls.py +++ b/example18/example18/urls.py @@ -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')), diff --git a/example18/templates/admin/base_site.html b/example18/templates/admin/base_site.html deleted file mode 100644 index fa47a37..0000000 --- a/example18/templates/admin/base_site.html +++ /dev/null @@ -1,21 +0,0 @@ -{% extends "admin/base.html" %} -{% load i18n staticfiles admin_style_tags pip_version_viewer_tags %} - -{% block extrastyle %}{% include 'admin/inc/extrastyle.html' %}{% endblock %} - -{% block extrahead %} - - -{% if request.user.is_superuser %}{% render_update_notification %}{% endif %} - -{% include 'admin/inc/extrahead.html' %}{% endblock %} - -{% block pretitle %}{% show_pip_package_versions %}{% endblock %} - -{% block title %}{% include 'admin/inc/title.html' %}{% endblock %} - -{% block bodyclass %}{{ block.super }} djangocms-admin-style{% endblock %} - -{% block branding %}{% include 'admin/inc/branding.html' %}{% endblock %} - -{% block userlinks %}{% include 'admin/inc/userlinks.html' %}{{ block.super }}{% endblock %} diff --git a/example18/templates/admin/base_site_regular.html b/example18/templates/admin/base_site_regular.html deleted file mode 100644 index ca6d228..0000000 --- a/example18/templates/admin/base_site_regular.html +++ /dev/null @@ -1,21 +0,0 @@ -{% extends "admin/base.html" %} - -{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} - -{% block branding %} -

{{ site_header|default:_('Django administration') }}

-{% endblock %} - -{% load pip_version_viewer_tags %} - -{% block extrahead %} - -{% endblock %} - - - -{% block pretitle %} - {% show_pip_package_versions %} -{% endblock %} - -{% block nav-global %}{% endblock %} diff --git a/example18/templates/admin/custom_index.html b/example18/templates/admin/custom_index.html new file mode 100644 index 0000000..640e98c --- /dev/null +++ b/example18/templates/admin/custom_index.html @@ -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 %}