prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Keyerror when sending password reset email
  When sending a password reset email, I'm getting an internal error
  
  I'll just share the raven error - hopefully that doesn't review all of the site secrets (probably does)
  
  https://app.getsentry.com/share/issue/37343334302e313233323439393235/
  
  
  </issue>
  <code>
  [start of wagtail/wagtailadmin/templatetags/wagtailuserbar.py]
  1 from __future__ import absolute_import, unicode_literals
  2 
  3 from django import template
  4 from django.template.loader import render_to_string
  5 
  6 from wagtail.wagtailadmin.userbar import (
  7     AddPageItem, AdminItem, ApproveModerationEditPageItem, EditPageItem, ExplorePageItem,
  8     RejectModerationEditPageItem)
  9 from wagtail.wagtailcore import hooks
  10 from wagtail.wagtailcore.models import PAGE_TEMPLATE_VAR, Page, PageRevision
  11 
  12 # from django.contrib.auth.decorators import permission_required
  13 
  14 
  15 register = template.Library()
  16 
  17 
  18 def get_page_instance(context):
  19     """
  20     Given a template context, try and find a Page variable in the common
  21     places. Returns None if a page can not be found.
  22     """
  23     possible_names = [PAGE_TEMPLATE_VAR, 'self']
  24     for name in possible_names:
  25         if name in context:
  26             page = context[name]
  27             if isinstance(page, Page):
  28                 return page
  29 
  30 
  31 @register.simple_tag(takes_context=True)
  32 def wagtailuserbar(context, position='bottom-right'):
  33     # Find request object
  34     request = context['request']
  35 
  36 
  37     # Don't render if user doesn't have permission to access the admin area
  38     if not request.user.has_perm('wagtailadmin.access_admin'):
  39         return ''
  40 
  41     # Only render if the context contains a variable referencing a saved page
  42     page = get_page_instance(context)
  43     if page is None:
  44         return ''
  45 
  46     # Dont render anything if the page has not been saved - i.e. a preview
  47     if page.pk is None:
  48         return ''
  49 
  50     try:
  51         revision_id = request.revision_id
  52     except AttributeError:
  53         revision_id = None
  54 
  55     if revision_id is None:
  56         items = [
  57             AdminItem(),
  58             ExplorePageItem(Page.objects.get(id=page.id)),
  59             EditPageItem(Page.objects.get(id=page.id)),
  60             AddPageItem(Page.objects.get(id=page.id)),
  61         ]
  62     else:
  63         items = [
  64             AdminItem(),
  65             ExplorePageItem(PageRevision.objects.get(id=revision_id).page),
  66             EditPageItem(PageRevision.objects.get(id=revision_id).page),
  67             AddPageItem(PageRevision.objects.get(id=revision_id).page),
  68             ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)),
  69             RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)),
  70         ]
  71 
  72     for fn in hooks.get_hooks('construct_wagtail_userbar'):
  73         fn(request, items)
  74 
  75     # Render the items
  76     rendered_items = [item.render(request) for item in items]
  77 
  78     # Remove any unrendered items
  79     rendered_items = [item for item in rendered_items if item]
  80 
  81     # Render the userbar items
  82     return render_to_string('wagtailadmin/userbar/base.html', {
  83         'request': request,
  84         'items': rendered_items,
  85         'position': position,
  86         'page': page,
  87         'revision_id': revision_id
  88     })
  89
  [end of wagtail/wagtailadmin/templatetags/wagtailuserbar.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py
  --- a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py
  +++ b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py
  @@ -31,8 +31,10 @@
   @register.simple_tag(takes_context=True)
   def wagtailuserbar(context, position='bottom-right'):
       # Find request object
  -    request = context['request']
  -
  +    try:
  +        request = context['request']
  +    except KeyError:
  +        return ''
   
       # Don't render if user doesn't have permission to access the admin area
       if not request.user.has_perm('wagtailadmin.access_admin'):
  
verification_info: {"golden_diff": "diff --git a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py\n--- a/wagtail/wagtailadmin/templatetags/wagtailuserbar.py\n+++ b/wagtail/wagtailadmin/templatetags/wagtailuserbar.py\n@@ -31,8 +31,10 @@\n @register.simple_tag(takes_context=True)\n def wagtailuserbar(context, position='bottom-right'):\n     # Find request object\n-    request = context['request']\n-\n+    try:\n+        request = context['request']\n+    except KeyError:\n+        return ''\n \n     # Don't render if user doesn't have permission to access the admin area\n     if not request.user.has_perm('wagtailadmin.access_admin'):\n", "issue": "Keyerror when sending password reset email\nWhen sending a password reset email, I'm getting an internal error\n\nI'll just share the raven error - hopefully that doesn't review all of the site secrets (probably does)\n\nhttps://app.getsentry.com/share/issue/37343334302e313233323439393235/\n\n", "before_files": [{"content": "from __future__ import absolute_import, unicode_literals\n\nfrom django import template\nfrom django.template.loader import render_to_string\n\nfrom wagtail.wagtailadmin.userbar import (\n    AddPageItem, AdminItem, ApproveModerationEditPageItem, EditPageItem, ExplorePageItem,\n    RejectModerationEditPageItem)\nfrom wagtail.wagtailcore import hooks\nfrom wagtail.wagtailcore.models import PAGE_TEMPLATE_VAR, Page, PageRevision\n\n# from django.contrib.auth.decorators import permission_required\n\n\nregister = template.Library()\n\n\ndef get_page_instance(context):\n    \"\"\"\n    Given a template context, try and find a Page variable in the common\n    places. Returns None if a page can not be found.\n    \"\"\"\n    possible_names = [PAGE_TEMPLATE_VAR, 'self']\n    for name in possible_names:\n        if name in context:\n            page = context[name]\n            if isinstance(page, Page):\n                return page\n\n\n@register.simple_tag(takes_context=True)\ndef wagtailuserbar(context, position='bottom-right'):\n    # Find request object\n    request = context['request']\n\n\n    # Don't render if user doesn't have permission to access the admin area\n    if not request.user.has_perm('wagtailadmin.access_admin'):\n        return ''\n\n    # Only render if the context contains a variable referencing a saved page\n    page = get_page_instance(context)\n    if page is None:\n        return ''\n\n    # Dont render anything if the page has not been saved - i.e. a preview\n    if page.pk is None:\n        return ''\n\n    try:\n        revision_id = request.revision_id\n    except AttributeError:\n        revision_id = None\n\n    if revision_id is None:\n        items = [\n            AdminItem(),\n            ExplorePageItem(Page.objects.get(id=page.id)),\n            EditPageItem(Page.objects.get(id=page.id)),\n            AddPageItem(Page.objects.get(id=page.id)),\n        ]\n    else:\n        items = [\n            AdminItem(),\n            ExplorePageItem(PageRevision.objects.get(id=revision_id).page),\n            EditPageItem(PageRevision.objects.get(id=revision_id).page),\n            AddPageItem(PageRevision.objects.get(id=revision_id).page),\n            ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)),\n            RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)),\n        ]\n\n    for fn in hooks.get_hooks('construct_wagtail_userbar'):\n        fn(request, items)\n\n    # Render the items\n    rendered_items = [item.render(request) for item in items]\n\n    # Remove any unrendered items\n    rendered_items = [item for item in rendered_items if item]\n\n    # Render the userbar items\n    return render_to_string('wagtailadmin/userbar/base.html', {\n        'request': request,\n        'items': rendered_items,\n        'position': position,\n        'page': page,\n        'revision_id': revision_id\n    })\n", "path": "wagtail/wagtailadmin/templatetags/wagtailuserbar.py"}]}