prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  403 attempting to login
  ![6A0CC90F-F750-4128-83AA-BB02D4E649BD](https://user-images.githubusercontent.com/918105/109430197-f2d43600-79b4-11eb-8855-a58decfda03e.png)
  
  It looks like if I open another tab I am logged into my account though. 
  
  </issue>
  <code>
  [start of bookwyrm/views/authentication.py]
  1 ''' class views for login/register views '''
  2 from django.contrib.auth import authenticate, login, logout
  3 from django.contrib.auth.decorators import login_required
  4 from django.core.exceptions import PermissionDenied
  5 from django.shortcuts import get_object_or_404, redirect
  6 from django.template.response import TemplateResponse
  7 from django.utils import timezone
  8 from django.utils.decorators import method_decorator
  9 from django.views import View
  10 
  11 from bookwyrm import forms, models
  12 from bookwyrm.settings import DOMAIN
  13 
  14 
  15 # pylint: disable= no-self-use
  16 class Login(View):
  17     ''' authenticate an existing user '''
  18     def get(self, request):
  19         ''' login page '''
  20         if request.user.is_authenticated:
  21             return redirect('/')
  22         # sene user to the login page
  23         data = {
  24             'title': 'Login',
  25             'login_form': forms.LoginForm(),
  26             'register_form': forms.RegisterForm(),
  27         }
  28         return TemplateResponse(request, 'login.html', data)
  29 
  30     def post(self, request):
  31         ''' authentication action '''
  32         login_form = forms.LoginForm(request.POST)
  33 
  34         localname = login_form.data['localname']
  35         if '@' in localname: # looks like an email address to me
  36             email = localname
  37             try:
  38                 username = models.User.objects.get(email=email)
  39             except models.User.DoesNotExist: # maybe it's a full username?
  40                 username = localname
  41         else:
  42             username = '%s@%s' % (localname, DOMAIN)
  43         password = login_form.data['password']
  44         user = authenticate(request, username=username, password=password)
  45         if user is not None:
  46             # successful login
  47             login(request, user)
  48             user.last_active_date = timezone.now()
  49             user.save(broadcast=False)
  50             return redirect(request.GET.get('next', '/'))
  51 
  52         # login errors
  53         login_form.non_field_errors = 'Username or password are incorrect'
  54         register_form = forms.RegisterForm()
  55         data = {
  56             'login_form': login_form,
  57             'register_form': register_form
  58         }
  59         return TemplateResponse(request, 'login.html', data)
  60 
  61 
  62 class Register(View):
  63     ''' register a user '''
  64     def post(self, request):
  65         ''' join the server '''
  66         if not models.SiteSettings.get().allow_registration:
  67             invite_code = request.POST.get('invite_code')
  68 
  69             if not invite_code:
  70                 raise PermissionDenied
  71 
  72             invite = get_object_or_404(models.SiteInvite, code=invite_code)
  73             if not invite.valid():
  74                 raise PermissionDenied
  75         else:
  76             invite = None
  77 
  78         form = forms.RegisterForm(request.POST)
  79         errors = False
  80         if not form.is_valid():
  81             errors = True
  82 
  83         localname = form.data['localname'].strip()
  84         email = form.data['email']
  85         password = form.data['password']
  86 
  87         # check localname and email uniqueness
  88         if models.User.objects.filter(localname=localname).first():
  89             form.errors['localname'] = [
  90                 'User with this username already exists']
  91             errors = True
  92 
  93         if errors:
  94             data = {
  95                 'login_form': forms.LoginForm(),
  96                 'register_form': form,
  97                 'invite': invite,
  98                 'valid': invite.valid() if invite else True,
  99             }
  100             if invite:
  101                 return TemplateResponse(request, 'invite.html', data)
  102             return TemplateResponse(request, 'login.html', data)
  103 
  104         username = '%s@%s' % (localname, DOMAIN)
  105         user = models.User.objects.create_user(
  106             username, email, password, localname=localname, local=True)
  107         if invite:
  108             invite.times_used += 1
  109             invite.save()
  110 
  111         login(request, user)
  112         return redirect('/')
  113 
  114 
  115 @method_decorator(login_required, name='dispatch')
  116 class Logout(View):
  117     ''' log out '''
  118     def get(self, request):
  119         ''' done with this place! outa here! '''
  120         logout(request)
  121         return redirect('/')
  122
  [end of bookwyrm/views/authentication.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/bookwyrm/views/authentication.py b/bookwyrm/views/authentication.py
  --- a/bookwyrm/views/authentication.py
  +++ b/bookwyrm/views/authentication.py
  @@ -6,6 +6,7 @@
   from django.template.response import TemplateResponse
   from django.utils import timezone
   from django.utils.decorators import method_decorator
  +from django.views.decorators.csrf import csrf_exempt
   from django.views import View
   
   from bookwyrm import forms, models
  @@ -13,6 +14,7 @@
   
   
   # pylint: disable= no-self-use
  +@method_decorator(csrf_exempt, name='dispatch')
   class Login(View):
       ''' authenticate an existing user '''
       def get(self, request):
  @@ -29,6 +31,8 @@
   
       def post(self, request):
           ''' authentication action '''
  +        if request.user.is_authenticated:
  +            return redirect('/')
           login_form = forms.LoginForm(request.POST)
   
           localname = login_form.data['localname']
  
verification_info: {"golden_diff": "diff --git a/bookwyrm/views/authentication.py b/bookwyrm/views/authentication.py\n--- a/bookwyrm/views/authentication.py\n+++ b/bookwyrm/views/authentication.py\n@@ -6,6 +6,7 @@\n from django.template.response import TemplateResponse\n from django.utils import timezone\n from django.utils.decorators import method_decorator\n+from django.views.decorators.csrf import csrf_exempt\n from django.views import View\n \n from bookwyrm import forms, models\n@@ -13,6 +14,7 @@\n \n \n # pylint: disable= no-self-use\n+@method_decorator(csrf_exempt, name='dispatch')\n class Login(View):\n     ''' authenticate an existing user '''\n     def get(self, request):\n@@ -29,6 +31,8 @@\n \n     def post(self, request):\n         ''' authentication action '''\n+        if request.user.is_authenticated:\n+            return redirect('/')\n         login_form = forms.LoginForm(request.POST)\n \n         localname = login_form.data['localname']\n", "issue": "403 attempting to login\n![6A0CC90F-F750-4128-83AA-BB02D4E649BD](https://user-images.githubusercontent.com/918105/109430197-f2d43600-79b4-11eb-8855-a58decfda03e.png)\r\n\r\nIt looks like if I open another tab I am logged into my account though. \n", "before_files": [{"content": "''' class views for login/register views '''\nfrom django.contrib.auth import authenticate, login, logout\nfrom django.contrib.auth.decorators import login_required\nfrom django.core.exceptions import PermissionDenied\nfrom django.shortcuts import get_object_or_404, redirect\nfrom django.template.response import TemplateResponse\nfrom django.utils import timezone\nfrom django.utils.decorators import method_decorator\nfrom django.views import View\n\nfrom bookwyrm import forms, models\nfrom bookwyrm.settings import DOMAIN\n\n\n# pylint: disable= no-self-use\nclass Login(View):\n    ''' authenticate an existing user '''\n    def get(self, request):\n        ''' login page '''\n        if request.user.is_authenticated:\n            return redirect('/')\n        # sene user to the login page\n        data = {\n            'title': 'Login',\n            'login_form': forms.LoginForm(),\n            'register_form': forms.RegisterForm(),\n        }\n        return TemplateResponse(request, 'login.html', data)\n\n    def post(self, request):\n        ''' authentication action '''\n        login_form = forms.LoginForm(request.POST)\n\n        localname = login_form.data['localname']\n        if '@' in localname: # looks like an email address to me\n            email = localname\n            try:\n                username = models.User.objects.get(email=email)\n            except models.User.DoesNotExist: # maybe it's a full username?\n                username = localname\n        else:\n            username = '%s@%s' % (localname, DOMAIN)\n        password = login_form.data['password']\n        user = authenticate(request, username=username, password=password)\n        if user is not None:\n            # successful login\n            login(request, user)\n            user.last_active_date = timezone.now()\n            user.save(broadcast=False)\n            return redirect(request.GET.get('next', '/'))\n\n        # login errors\n        login_form.non_field_errors = 'Username or password are incorrect'\n        register_form = forms.RegisterForm()\n        data = {\n            'login_form': login_form,\n            'register_form': register_form\n        }\n        return TemplateResponse(request, 'login.html', data)\n\n\nclass Register(View):\n    ''' register a user '''\n    def post(self, request):\n        ''' join the server '''\n        if not models.SiteSettings.get().allow_registration:\n            invite_code = request.POST.get('invite_code')\n\n            if not invite_code:\n                raise PermissionDenied\n\n            invite = get_object_or_404(models.SiteInvite, code=invite_code)\n            if not invite.valid():\n                raise PermissionDenied\n        else:\n            invite = None\n\n        form = forms.RegisterForm(request.POST)\n        errors = False\n        if not form.is_valid():\n            errors = True\n\n        localname = form.data['localname'].strip()\n        email = form.data['email']\n        password = form.data['password']\n\n        # check localname and email uniqueness\n        if models.User.objects.filter(localname=localname).first():\n            form.errors['localname'] = [\n                'User with this username already exists']\n            errors = True\n\n        if errors:\n            data = {\n                'login_form': forms.LoginForm(),\n                'register_form': form,\n                'invite': invite,\n                'valid': invite.valid() if invite else True,\n            }\n            if invite:\n                return TemplateResponse(request, 'invite.html', data)\n            return TemplateResponse(request, 'login.html', data)\n\n        username = '%s@%s' % (localname, DOMAIN)\n        user = models.User.objects.create_user(\n            username, email, password, localname=localname, local=True)\n        if invite:\n            invite.times_used += 1\n            invite.save()\n\n        login(request, user)\n        return redirect('/')\n\n\n@method_decorator(login_required, name='dispatch')\nclass Logout(View):\n    ''' log out '''\n    def get(self, request):\n        ''' done with this place! outa here! '''\n        logout(request)\n        return redirect('/')\n", "path": "bookwyrm/views/authentication.py"}]}