From c82e17d5fd2895d9c0429a64d21377f8a8ac9e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Fri, 22 Jun 2018 21:28:29 +0000 Subject: [PATCH] Add support for django-debug-toolbar --- CHANGELOG.md | 24 +++++++++++++++++++++++- re2o/middleware.py | 43 +++++++++++++++++++++++++++++++++++++++++++ re2o/settings.py | 10 ++++++++++ re2o/urls.py | 6 ++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 re2o/middleware.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 41982264..01b9b8ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,4 +71,26 @@ OPTIONAL_APPS = ( ... 'api', ... -) \ No newline at end of file +) +``` + + + +## MR 177: Add django-debug-toolbar support + +Add the possibility to enable `django-debug-toolbar` in debug mode. First install the APT package: +``` +apt install pyhton3-django-debug-toolbar +``` +And then activate it for Re2o by adding the app to the `OPTIONAL_APPS` in `re2o/settings_local.py`: +```python +OPTIONAL_APPS = ( + # ... + 'debug_toolbar', + # ... +) +``` +If you to restrict the IP which can see the debug, use the `INTERNAL_IPS` options in `re2o/settings_local.py`: +``` +INTERNAL_IPS = ["10.0.0.1", "10.0.0.2"] +``` diff --git a/re2o/middleware.py b/re2o/middleware.py new file mode 100644 index 00000000..9a884fad --- /dev/null +++ b/re2o/middleware.py @@ -0,0 +1,43 @@ +# Re2o est un logiciel d'administration développé initiallement au rezometz. Il +# se veut agnostique au réseau considéré, de manière à être installable en +# quelques clics. +# +# Copyright © 2018 Maël Kervella +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +"""Defines the middlewares used in all apps of re2o. +""" + +from django.conf import settings + + +def show_debug_toolbar(request): + """Middleware to determine wether to show the toolbar. + + Compared to `django-debug-toolbar`'s default, add the possibility to allow + any IP to see the debug panel by not setting the `INTERNAL_IPS` options + + Args: + requests: The request object that must be checked. + + Returns: + The boolean indicating if the debug toolbar should be shown. + """ + if hasattr(settings, 'INTERNAL_IPS') and settings.INTERNAL_IPS and \ + request.META.get('REMOTE_ADDR', None) not in settings.INTERNAL_IPS: + return False + + return bool(settings.DEBUG) diff --git a/re2o/settings.py b/re2o/settings.py index 7c119a55..1117dd77 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -94,6 +94,16 @@ MIDDLEWARE_CLASSES = ( 'django.middleware.security.SecurityMiddleware', 'reversion.middleware.RevisionMiddleware', ) +# Include debug_toolbar middleware if activated +if 'debug_toolbar' in INSTALLED_APPS: + # Include this middleware at the beggining + MIDDLEWARE_CLASSES = ( + 'debug_toolbar.middleware.DebugToolbarMiddleware', + ) + MIDDLEWARE_CLASSES + # Change the default show_toolbar middleware + DEBUG_TOOLBAR_CONFIG = { + 'SHOW_TOOLBAR_CALLBACK': 're2o.middleware.show_debug_toolbar' + } # The root url module to define the project URLs ROOT_URLCONF = 're2o.urls' diff --git a/re2o/urls.py b/re2o/urls.py index 34cb0b15..3322e82b 100644 --- a/re2o/urls.py +++ b/re2o/urls.py @@ -72,6 +72,12 @@ urlpatterns = [ include('preferences.urls', namespace='preferences') ), ] +# Add debug_toolbar URLs if activated +if 'debug_toolbar' in settings.INSTALLED_APPS: + import debug_toolbar + urlpatterns += [ + url(r'^__debug__/', include(debug_toolbar.urls)), + ] if 'api' in settings.INSTALLED_APPS: urlpatterns += [ url(r'^api/', include('api.urls', namespace='api')),