Created
August 7, 2015 17:51
-
-
Save davidmarquis/3ecd34b062b1799512cf to your computer and use it in GitHub Desktop.
Django CMS disable toolbar for specific URLs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Hi-jacked version of Django CMS's toolbar middleware that | |
excludes the toolbar from specific URLs (as defined in settings). | |
To use it, replace 'cms.middleware.ToolbarMiddleware' with a | |
reference to this class. | |
Then set `CMS_TOOLBAR_EXCLUDED_URLS` in your Django settings: | |
``` | |
CMS_TOOLBAR_EXCLUDED_URLS = [ | |
'/shop/', | |
] | |
``` | |
""" | |
from cms.middleware.toolbar import ToolbarMiddleware | |
from django.conf import settings | |
class URLFilteringToolbarMiddleware(ToolbarMiddleware): | |
def process_request(self, request): | |
if self._is_matched(request): | |
return | |
super().process_request(request) | |
def process_view(self, request, view_func, view_args, view_kwarg): | |
if self._is_matched(request): | |
return | |
return super().process_view(request, view_func, view_args, view_kwarg) | |
def process_response(self, request, response): | |
if self._is_matched(request): | |
return response | |
return super().process_response(request, response) | |
def _is_matched(self, request): | |
path = request.path | |
for url in settings.CMS_TOOLBAR_EXCLUDED_URLS: | |
if path.startswith(url): | |
return True | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment