Skip to content

Commit

Permalink
Move if/else logic for Django 1.8's Template Engine into compat
Browse files Browse the repository at this point in the history
Created a few methods that will handle doing the if/else based on
what's been imported successfully. This cleans up the logic in the
toolbar itself.
  • Loading branch information
tim-schilling authored and aaugustin committed Mar 8, 2015
1 parent c8a5cf1 commit aec63b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
32 changes: 31 additions & 1 deletion debug_toolbar/compat.py
Expand Up @@ -5,6 +5,7 @@
debug_toolbar.
"""

from django.conf import settings

try:
from django.core.cache import CacheHandler, caches
Expand All @@ -16,7 +17,7 @@
from django.template.engine import Engine
except ImportError: # < Django 1.8
Engine = None
from django.template.context import get_standard_processors #NOQA
from django.template.context import get_standard_processors # NOQA
from django.template.loader import find_template_loader # NOQA

try:
Expand Down Expand Up @@ -45,3 +46,32 @@
except ImportError: # >= Django 1.7
import weakref
WEAKREF_TYPES = weakref.ReferenceType,


def get_template_dirs():
"""Compatibility method to fetch the template directories."""
if Engine:
template_dirs = Engine.get_default().dirs
else: # < Django 1.8
template_dirs = settings.TEMPLATE_DIRS
return template_dirs


def get_template_loaders():
"""Compatibility method to fetch the template loaders."""
if Engine:
loaders = Engine.get_default().template_loaders
else: # < Django 1.8
loaders = [
find_template_loader(loader_name)
for loader_name in settings.TEMPLATE_LOADERS]
return loaders


def get_template_context_processors():
"""Compatibility method to fetch the template context processors."""
if Engine:
context_processors = Engine.get_default().template_context_processors
else: # < Django 1.8
context_processors = get_standard_processors()
return context_processors
14 changes: 4 additions & 10 deletions debug_toolbar/panels/templates/panel.py
Expand Up @@ -5,7 +5,6 @@

import django
from django import http
from django.conf import settings
from django.conf.urls import url
from django.db.models.query import QuerySet, RawQuerySet
from django.template import Context, RequestContext, Template
Expand All @@ -15,7 +14,8 @@
from django.utils import six
from django.utils.translation import ugettext_lazy as _

from debug_toolbar.compat import OrderedDict, Engine, get_standard_processors
from debug_toolbar.compat import (
OrderedDict, get_template_dirs, get_template_context_processors)
from debug_toolbar.panels import Panel
from debug_toolbar.panels.sql.tracking import recording, SQLQueryTriggered
from debug_toolbar.panels.templates import views
Expand Down Expand Up @@ -47,10 +47,7 @@ def _request_context__init__(
processors = tuple(processors)
self.context_processors = OrderedDict()
updates = dict()
if Engine:
std_processors = Engine.get_default().template_context_processors
else:
std_processors = get_standard_processors()
std_processors = get_template_context_processors()
for processor in std_processors + processors:
name = '%s.%s' % (processor.__module__, processor.__name__)
context = processor(request)
Expand Down Expand Up @@ -191,10 +188,7 @@ def process_response(self, request, response):
else:
context_processors = None

if Engine:
template_dirs = Engine.get_default().dirs
else:
template_dirs = settings.TEMPLATE_DIRS
template_dirs = get_template_dirs()

self.record_stats({
'templates': template_context,
Expand Down
8 changes: 2 additions & 6 deletions debug_toolbar/panels/templates/views.py
@@ -1,12 +1,11 @@
from __future__ import absolute_import, unicode_literals

from django.http import HttpResponseBadRequest
from django.conf import settings
from django.shortcuts import render_to_response
from django.template import TemplateDoesNotExist
from django.utils.safestring import mark_safe

from debug_toolbar.compat import Engine, find_template_loader
from debug_toolbar.compat import get_template_loaders


def template_source(request):
Expand All @@ -19,10 +18,7 @@ def template_source(request):
return HttpResponseBadRequest('"template" key is required')

final_loaders = []
if Engine:
loaders = Engine.get_default().template_loaders
else:
loaders = [find_template_loader(loader_name) for loader_name in settings.TEMPLATE_LOADERS]
loaders = get_template_loaders()

for loader in loaders:
if loader is not None:
Expand Down

0 comments on commit aec63b9

Please sign in to comment.