From 875d2fcbd465b143f67154f2a2a90fdd8b9e55f3 Mon Sep 17 00:00:00 2001 From: digia Date: Fri, 2 Sep 2016 17:58:06 -0400 Subject: [PATCH] Fix bug with forcing a user to exist on request Allow for testing views without having to mock a user on the request. --- CONTRIBUTORS.rst | 1 + wagtail/wagtailadmin/templatetags/wagtailuserbar.py | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 90af4ec829..a5c0b0142b 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -163,6 +163,7 @@ Contributors * Jesse Legg * Tim Leguijt * Luiz Boaretto +* Jonathon Moore Translators =========== diff --git a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py index a8e62cdd0f..ab31e72fe0 100644 --- a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py +++ b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py @@ -36,8 +36,14 @@ def wagtailuserbar(context, position='bottom-right'): except KeyError: return '' + # Don't render without a user because we can't check their permissions + try: + user = request.user + except AttributeError: + return '' + # Don't render if user doesn't have permission to access the admin area - if not request.user.has_perm('wagtailadmin.access_admin'): + if not user.has_perm('wagtailadmin.access_admin'): return '' # Don't render if this is a preview. Since some routes can render the userbar without going through Page.serve(),