web/lib/django/views/debug.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
       
     1 import datetime
     1 import os
     2 import os
     2 import re
     3 import re
     3 import sys
     4 import sys
     4 import datetime
       
     5 
     5 
     6 from django.conf import settings
     6 from django.conf import settings
     7 from django.template import Template, Context, TemplateDoesNotExist
     7 from django.http import HttpResponse, HttpResponseServerError, HttpResponseNotFound
       
     8 from django.template import (Template, Context, TemplateDoesNotExist,
       
     9     TemplateSyntaxError)
     8 from django.utils.html import escape
    10 from django.utils.html import escape
     9 from django.utils.importlib import import_module
    11 from django.utils.importlib import import_module
    10 from django.http import HttpResponse, HttpResponseServerError, HttpResponseNotFound
       
    11 from django.utils.encoding import smart_unicode, smart_str
    12 from django.utils.encoding import smart_unicode, smart_str
       
    13 
    12 
    14 
    13 HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD|PROFANITIES_LIST')
    15 HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD|PROFANITIES_LIST')
    14 
    16 
    15 def linebreak_iter(template_source):
    17 def linebreak_iter(template_source):
    16     yield 0
    18     yield 0
    18     while p >= 0:
    20     while p >= 0:
    19         yield p+1
    21         yield p+1
    20         p = template_source.find('\n', p+1)
    22         p = template_source.find('\n', p+1)
    21     yield len(template_source) + 1
    23     yield len(template_source) + 1
    22 
    24 
       
    25 def cleanse_setting(key, value):
       
    26     """Cleanse an individual setting key/value of sensitive content.
       
    27 
       
    28     If the value is a dictionary, recursively cleanse the keys in
       
    29     that dictionary.
       
    30     """
       
    31     try:
       
    32         if HIDDEN_SETTINGS.search(key):
       
    33             cleansed = '********************'
       
    34         else:
       
    35             if isinstance(value, dict):
       
    36                 cleansed = dict((k, cleanse_setting(k, v)) for k,v in value.items())
       
    37             else:
       
    38                 cleansed = value
       
    39     except TypeError:
       
    40         # If the key isn't regex-able, just return as-is.
       
    41         cleansed = value
       
    42     return cleansed
       
    43 
    23 def get_safe_settings():
    44 def get_safe_settings():
    24     "Returns a dictionary of the settings module, with sensitive settings blurred out."
    45     "Returns a dictionary of the settings module, with sensitive settings blurred out."
    25     settings_dict = {}
    46     settings_dict = {}
    26     for k in dir(settings):
    47     for k in dir(settings):
    27         if k.isupper():
    48         if k.isupper():
    28             if HIDDEN_SETTINGS.search(k):
    49             settings_dict[k] = cleanse_setting(k, getattr(settings, k))
    29                 settings_dict[k] = '********************'
       
    30             else:
       
    31                 settings_dict[k] = getattr(settings, k)
       
    32     return settings_dict
    50     return settings_dict
    33 
    51 
    34 def technical_500_response(request, exc_type, exc_value, tb):
    52 def technical_500_response(request, exc_type, exc_value, tb):
    35     """
    53     """
    36     Create a technical server error response. The last three arguments are
    54     Create a technical server error response. The last three arguments are
    74                     # the loader attempted to load.
    92                     # the loader attempted to load.
    75                     template_list = [{'name': t, 'exists': os.path.exists(t)} \
    93                     template_list = [{'name': t, 'exists': os.path.exists(t)} \
    76                         for t in source_list_func(str(self.exc_value))]
    94                         for t in source_list_func(str(self.exc_value))]
    77                 except (ImportError, AttributeError):
    95                 except (ImportError, AttributeError):
    78                     template_list = []
    96                     template_list = []
       
    97                 if hasattr(loader, '__class__'):
       
    98                     loader_name = loader.__module__ + '.' + loader.__class__.__name__
       
    99                 else:
       
   100                     loader_name = loader.__module__ + '.' + loader.__name__
    79                 self.loader_debug_info.append({
   101                 self.loader_debug_info.append({
    80                     'loader': loader.__module__ + '.' + loader.__name__,
   102                     'loader': loader_name,
    81                     'templates': template_list,
   103                     'templates': template_list,
    82                 })
   104                 })
    83         if settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source'):
   105         if (settings.TEMPLATE_DEBUG and hasattr(self.exc_value, 'source') and
       
   106             isinstance(self.exc_value, TemplateSyntaxError)):
    84             self.get_template_exception_info()
   107             self.get_template_exception_info()
    85 
   108 
    86         frames = self.get_traceback_frames()
   109         frames = self.get_traceback_frames()
    87 
   110 
    88         unicode_hint = ''
   111         unicode_hint = ''
   243 
   266 
   244 def technical_404_response(request, exception):
   267 def technical_404_response(request, exception):
   245     "Create a technical 404 error response. The exception should be the Http404."
   268     "Create a technical 404 error response. The exception should be the Http404."
   246     try:
   269     try:
   247         tried = exception.args[0]['tried']
   270         tried = exception.args[0]['tried']
   248     except (IndexError, TypeError):
   271     except (IndexError, TypeError, KeyError):
   249         tried = []
   272         tried = []
   250     else:
   273     else:
   251         if not tried:
   274         if not tried:
   252             # tried exists but is an empty list. The URLconf must've been empty.
   275             # tried exists but is an empty list. The URLconf must've been empty.
   253             return empty_urlconf(request)
   276             return empty_urlconf(request)
   398     <tr>
   421     <tr>
   399       <th>Request URL:</th>
   422       <th>Request URL:</th>
   400       <td>{{ request.build_absolute_uri|escape }}</td>
   423       <td>{{ request.build_absolute_uri|escape }}</td>
   401     </tr>
   424     </tr>
   402     <tr>
   425     <tr>
       
   426       <th>Django Version:</th>
       
   427       <td>{{ django_version_info }}</td>
       
   428     </tr>
       
   429     <tr>
   403       <th>Exception Type:</th>
   430       <th>Exception Type:</th>
   404       <td>{{ exception_type }}</td>
   431       <td>{{ exception_type }}</td>
   405     </tr>
   432     </tr>
   406     <tr>
   433     <tr>
   407       <th>Exception Value:</th>
   434       <th>Exception Value:</th>
   807 </div>
   834 </div>
   808 
   835 
   809 <div id="instructions">
   836 <div id="instructions">
   810   <p>Of course, you haven't actually done any work yet. Here's what to do next:</p>
   837   <p>Of course, you haven't actually done any work yet. Here's what to do next:</p>
   811   <ul>
   838   <ul>
   812     <li>If you plan to use a database, edit the <code>DATABASE_*</code> settings in <code>{{ project_name }}/settings.py</code>.</li>
   839     <li>If you plan to use a database, edit the <code>DATABASES</code> setting in <code>{{ project_name }}/settings.py</code>.</li>
   813     <li>Start your first app by running <code>python {{ project_name }}/manage.py startapp [appname]</code>.</li>
   840     <li>Start your first app by running <code>python {{ project_name }}/manage.py startapp [appname]</code>.</li>
   814   </ul>
   841   </ul>
   815 </div>
   842 </div>
   816 
   843 
   817 <div id="explanation">
   844 <div id="explanation">