Code

Ticket #4786: views_debug_py.4.diff

File views_debug_py.4.diff, 3.7 KB (added by guettli, 6 years ago)
Line 
1Index: django/views/debug.py
2===================================================================
3--- django/views/debug.py       (Revision 9014)
4+++ django/views/debug.py       (Arbeitskopie)
5@@ -8,6 +8,7 @@
6 from django.utils.html import escape
7 from django.http import HttpResponse, HttpResponseServerError, HttpResponseNotFound
8 from django.utils.encoding import smart_unicode, smart_str
9+from django.db.models.loading import get_apps
10 
11 HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD|PROFANITIES_LIST')
12 
13@@ -186,10 +187,26 @@
14         post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
15 
16         return lower_bound, pre_context, context_line, post_context
17+       
18+    def _is_installed_app_file(self, app_module_paths, filename):
19+        for path in app_module_paths:
20+            if filename.startswith(path):
21+                return True
22+        return False
23 
24     def get_traceback_frames(self):
25         frames = []
26         tb = self.tb
27+        app_module_paths=[]
28+        for mod in get_apps():
29+            if mod.__name__.startswith('django.'):
30+                continue
31+            dirname=os.path.dirname(os.path.normpath(mod.__file__))
32+            if os.path.basename(dirname)=='models':
33+                # models directory instead of app/models.py
34+                dirname=os.path.dirname(dirname)
35+            app_module_paths.append(dirname)
36+       
37         while tb is not None:
38             # support for __traceback_hide__ which is used by a few libraries
39             # to hide internal frames.
40@@ -207,6 +224,7 @@
41                     'tb': tb,
42                     'filename': filename,
43                     'function': function,
44+                    'app_code': self._is_installed_app_file(app_module_paths, filename),
45                     'lineno': lineno + 1,
46                     'vars': tb.tb_frame.f_locals.items(),
47                     'id': id(tb),
48@@ -313,6 +331,7 @@
49     div.context ol.context-line li span { float: right; }
50     div.commands { margin-left: 40px; }
51     div.commands a { color:black; text-decoration:none; }
52+    .app-code { font-weight: bold; }
53     #summary { background: #ffc; }
54     #summary h2 { font-weight: normal; color: #666; }
55     #explanation { background:#eee; }
56@@ -477,14 +496,14 @@
57     <ul class="traceback">
58       {% for frame in frames %}
59         <li class="frame">
60-          <code>{{ frame.filename|escape }}</code> in <code>{{ frame.function|escape }}</code>
61+          <code{% if frame.app_code %} class="app-code" {% endif %}>{{ frame.filename|escape }}</code> in <code>{{ frame.function|escape }}</code>
62 
63           {% if frame.context_line %}
64             <div class="context" id="c{{ frame.id }}">
65               {% if frame.pre_context %}
66                 <ol start="{{ frame.pre_context_lineno }}" class="pre-context" id="pre{{ frame.id }}">{% for line in frame.pre_context %}<li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')">{{ line|escape }}</li>{% endfor %}</ol>
67               {% endif %}
68-              <ol start="{{ frame.lineno }}" class="context-line"><li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')">{{ frame.context_line|escape }} <span>...</span></li></ol>
69+              <ol start="{{ frame.lineno }}" class="context-line {% if frame.app_code %} app-code {% endif %}"><li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')">{{ frame.context_line|escape }} <span>...</span></li></ol>
70               {% if frame.post_context %}
71                 <ol start='{{ frame.lineno|add:"1" }}' class="post-context" id="post{{ frame.id }}">{% for line in frame.post_context %}<li onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')">{{ line|escape }}</li>{% endfor %}</ol>
72               {% endif %}