From 348e582645465b319e4f7cf3b2bd4f3853cebad1 Mon Sep 17 00:00:00 2001 From: Ritik Soni <47344024+ritiksoni00@users.noreply.github.com> Date: Sun, 25 Sep 2022 21:20:05 +0530 Subject: [PATCH] added functionality to keep unsort the session dict (#1673) * added functionality to keep unsort the session dict * Update tests/panels/test_request.py Co-authored-by: Matthias Kestenholz * Add mention of session sorting change to changes.rst. Co-authored-by: Matthias Kestenholz Co-authored-by: tschilling --- debug_toolbar/panels/request.py | 19 +++++++++---------- docs/changes.rst | 2 ++ tests/panels/test_request.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/debug_toolbar/panels/request.py b/debug_toolbar/panels/request.py index 966301d97..bfb485ae7 100644 --- a/debug_toolbar/panels/request.py +++ b/debug_toolbar/panels/request.py @@ -59,13 +59,12 @@ def generate_stats(self, request, response): self.record_stats(view_info) if hasattr(request, "session"): - self.record_stats( - { - "session": { - "list": [ - (k, request.session.get(k)) - for k in sorted(request.session.keys()) - ] - } - } - ) + try: + session_list = [ + (k, request.session.get(k)) for k in sorted(request.session.keys()) + ] + except TypeError: + session_list = [ + (k, request.session.get(k)) for k in request.session.keys() + ] + self.record_stats({"session": {"list": session_list}}) diff --git a/docs/changes.rst b/docs/changes.rst index 1c45177b0..e049aa3a5 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -16,6 +16,8 @@ Pending ``settings.BASE_DIR``. * The toolbar's font stack now prefers system UI fonts. Tweaked paddings, margins and alignments a bit in the CSS code. +* Only sort the session dictionary when the keys are all strings. Fixes a + bug that causes the toolbar to crash when non-strings are used as keys. 3.6.0 (2022-08-17) ------------------ diff --git a/tests/panels/test_request.py b/tests/panels/test_request.py index 8087203c3..ea7f1681a 100644 --- a/tests/panels/test_request.py +++ b/tests/panels/test_request.py @@ -104,3 +104,32 @@ def test_namespaced_url(self): self.panel.generate_stats(self.request, response) panel_stats = self.panel.get_stats() self.assertEqual(panel_stats["view_urlname"], "admin:login") + + def test_session_list_sorted_or_not(self): + """ + Verify the session is sorted when all keys are strings. + + See https://github.com/jazzband/django-debug-toolbar/issues/1668 + """ + self.request.session = { + 1: "value", + "data": ["foo", "bar", 1], + (2, 3): "tuple_key", + } + data = { + "list": [(1, "value"), ("data", ["foo", "bar", 1]), ((2, 3), "tuple_key")] + } + response = self.panel.process_request(self.request) + self.panel.generate_stats(self.request, response) + panel_stats = self.panel.get_stats() + self.assertEqual(panel_stats["session"], data) + + self.request.session = { + "b": "b-value", + "a": "a-value", + } + data = {"list": [("a", "a-value"), ("b", "b-value")]} + response = self.panel.process_request(self.request) + self.panel.generate_stats(self.request, response) + panel_stats = self.panel.get_stats() + self.assertEqual(panel_stats["session"], data)