8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-28 01:43:46 +00:00

Merge branch 'debugtoolbar' into 'master'

Add django-debug-toolbar support

Closes #138

See merge request federez/re2o!177
This commit is contained in:
chirac 2018-06-25 13:00:19 +02:00
commit fdfd562959
4 changed files with 82 additions and 1 deletions

View file

@ -71,4 +71,26 @@ OPTIONAL_APPS = (
...
'api',
...
)
)
```
## 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"]
```

43
re2o/middleware.py Normal file
View file

@ -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)

View file

@ -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'

View file

@ -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')),