Ticket #6748: safe-queryset-debug-v2.patch
File safe-queryset-debug-v2.patch, 2.0 KB (added by , 17 years ago) |
---|
-
views/debug.py
474 474 {% for var in frame.vars|dictsort:"0" %} 475 475 <tr> 476 476 <td>{{ var.0|escape }}</td> 477 <td class="code"><div>{{ var.1| pprint|escape }}</div></td>477 <td class="code"><div>{{ var.1|safe_pprint|escape }}</div></td> 478 478 </tr> 479 479 {% endfor %} 480 480 </tbody> -
template/defaultfilters.py
793 793 return u"Error in formatting: %s" % force_unicode(e, errors="replace") 794 794 pprint.is_safe = True 795 795 796 from pprint import PrettyPrinter 797 class SafePrettyPrinter(PrettyPrinter): 798 '''A PrettyPrinter that will never evaluate an unevaluated QuerySet.''' 799 def format(self, object, *a, **kw): 800 from django.db.models.query import QuerySet 801 if isinstance(object, QuerySet): 802 object = object.query.as_sql() 803 return PrettyPrinter.format(self, object, *a, **kw) 804 805 def safe_pprint(value): 806 """A wrapper around pprint that ensures that QuerySets don't get evaluated. 807 If the QuerySet results have already been used, they will be cached and the 808 result will be identical to normal pprint. However, if the QuerySet has not 809 been used, this filter will return the SQL that the QuerySet would execute.""" 810 pp = SafePrettyPrinter() 811 return pp.pformat(value) 812 813 796 814 # Syntax: register.filter(name of filter, callback) 797 815 register.filter(add) 798 816 register.filter(addslashes) … … 831 849 register.filter(random) 832 850 register.filter(rjust) 833 851 register.filter(safe) 852 register.filter(safe_pprint) 834 853 register.filter('slice', slice_) 835 854 register.filter(slugify) 836 855 register.filter(stringformat)