prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Application for extending membership marks field of study as guest
  When someone applies to get their membership prolonged their field of study is set to "guest" instead of what they previously were registered as. 
  
  
  </issue>
  <code>
  [start of apps/approval/views.py]
  1 # -*- encoding: utf-8 -*-
  2 
  3 import datetime
  4 
  5 from django.contrib import messages
  6 from django.contrib.auth.decorators import login_required
  7 from django.http import Http404
  8 from django.shortcuts import get_object_or_404, redirect
  9 from django.utils import timezone
  10 from django.utils.translation import ugettext as _
  11 
  12 from apps.approval.forms import FieldOfStudyApplicationForm
  13 from apps.approval.models import MembershipApproval
  14 from apps.authentication.models import AllowedUsername, get_length_of_field_of_study
  15 
  16 
  17 @login_required
  18 def create_fos_application(request):
  19     if request.method == 'POST':
  20         if not request.user.ntnu_username:
  21             messages.error(request, _("Du må knytte et NTNU-brukernavn til kontoen din."))
  22             return redirect('profiles_active', active_tab='membership')
  23 
  24         form = FieldOfStudyApplicationForm(request.POST)
  25         if form.is_valid():
  26             cleaned = form.cleaned_data
  27 
  28             field_of_study = int(cleaned['field_of_study'])
  29 
  30             if field_of_study == 0:
  31                 messages.warning(request, _("Denne studieretningen (Gjest) er ikke et gyldig alternativ."))
  32                 return redirect('profiles_active', active_tab='membership')
  33 
  34             started_day = 1
  35             started_month = 0
  36             started_year = int(cleaned['started_year'])
  37 
  38             if cleaned['started_semester'] == "h":
  39                 started_month = 7
  40             if cleaned['started_semester'] == "v":
  41                 started_month = 1
  42 
  43             started_date = datetime.date(started_year, started_month, started_day)
  44 
  45             # Does the user already have a field of study and started date?
  46             if request.user.started_date and request.user.field_of_study:
  47                 # If there is no change from the current settings, ignore the request
  48                 if request.user.started_date == started_date and request.user.field_of_study == field_of_study:
  49                     messages.error(
  50                         request,
  51                         _("Du er allerede registrert med denne studieretningen og denne startdatoen.")
  52                     )
  53                     return redirect('profiles_active', active_tab='membership')
  54 
  55             application = MembershipApproval(
  56                 applicant=request.user,
  57                 field_of_study=field_of_study,
  58                 started_date=started_date
  59             )
  60 
  61             length_of_fos = get_length_of_field_of_study(field_of_study)
  62             if length_of_fos > 0:
  63                 application.new_expiry_date = get_expiry_date(started_year, length_of_fos)
  64             application.save()
  65 
  66             messages.success(request, _("Søknad om bytte av studieretning er sendt."))
  67 
  68         return redirect('profiles_active', active_tab='membership')
  69     raise Http404
  70 
  71 
  72 def get_expiry_date(started_year, length_of_fos):
  73     today = timezone.now().date()
  74     # Expiry dates should be 15th September, so that we have time to get new lists from NTNU
  75     new_expiry_date = datetime.date(
  76         started_year, 9, 16) + datetime.timedelta(days=365*length_of_fos)
  77     # Expiry dates in the past sets the expiry date to next september
  78     if new_expiry_date < today:
  79         if today < datetime.date(today.year, 9, 15):
  80             new_expiry_date = datetime.date(today.year, 9, 15)
  81         else:
  82             new_expiry_date = datetime.date(
  83                 today.year, 9, 16) + datetime.timedelta(days=365)
  84     return new_expiry_date
  85 
  86 
  87 @login_required
  88 def create_membership_application(request):
  89     if request.method == 'POST':
  90         if not request.user.has_expiring_membership:
  91             messages.error(request, _("Din bruker har ikke et utløpende medlemskap."))
  92             return redirect('profiles_active', active_tab='membership')
  93 
  94         if not request.user.ntnu_username:
  95             messages.error(request, _("Du må knytte et NTNU-brukernavn til kontoen din."))
  96             return redirect('profiles_active', active_tab='membership')
  97 
  98         # Extend length of membership by 1 year
  99         membership = AllowedUsername.objects.get(username=request.user.ntnu_username)
  100         new_expiration_date = datetime.date(membership.expiration_date.year + 1, 9, 16)
  101 
  102         application = MembershipApproval(
  103             applicant=request.user,
  104             new_expiry_date=new_expiration_date,
  105         )
  106         application.save()
  107 
  108         messages.success(request, _("Søknad om ett års forlenget medlemskap er sendt."))
  109 
  110         return redirect('profiles_active', active_tab='membership')
  111     raise Http404
  112 
  113 
  114 @login_required
  115 def cancel_application(request, application_id):
  116     app = get_object_or_404(MembershipApproval, pk=application_id)
  117 
  118     if app.applicant != request.user:
  119         messages.error(request, _("Bare søkeren selv kan slette en søknad."))
  120         return redirect('profiles_active', active_tab='membership')
  121 
  122     if app.processed:
  123         messages.error(request, _("Denne søknaden er behandlet og kan ikke slettes."))
  124         return redirect('profiles_active', active_tab='membership')
  125 
  126     app.delete()
  127 
  128     return redirect('profiles_active', active_tab='membership')
  129
  [end of apps/approval/views.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/apps/approval/views.py b/apps/approval/views.py
  --- a/apps/approval/views.py
  +++ b/apps/approval/views.py
  @@ -101,6 +101,7 @@
   
           application = MembershipApproval(
               applicant=request.user,
  +            field_of_study=request.user.field_of_study,
               new_expiry_date=new_expiration_date,
           )
           application.save()
  
verification_info: {"golden_diff": "diff --git a/apps/approval/views.py b/apps/approval/views.py\n--- a/apps/approval/views.py\n+++ b/apps/approval/views.py\n@@ -101,6 +101,7 @@\n \n         application = MembershipApproval(\n             applicant=request.user,\n+            field_of_study=request.user.field_of_study,\n             new_expiry_date=new_expiration_date,\n         )\n         application.save()\n", "issue": "Application for extending membership marks field of study as guest\nWhen someone applies to get their membership prolonged their field of study is set to \"guest\" instead of what they previously were registered as. \n\n", "before_files": [{"content": "# -*- encoding: utf-8 -*-\n\nimport datetime\n\nfrom django.contrib import messages\nfrom django.contrib.auth.decorators import login_required\nfrom django.http import Http404\nfrom django.shortcuts import get_object_or_404, redirect\nfrom django.utils import timezone\nfrom django.utils.translation import ugettext as _\n\nfrom apps.approval.forms import FieldOfStudyApplicationForm\nfrom apps.approval.models import MembershipApproval\nfrom apps.authentication.models import AllowedUsername, get_length_of_field_of_study\n\n\n@login_required\ndef create_fos_application(request):\n    if request.method == 'POST':\n        if not request.user.ntnu_username:\n            messages.error(request, _(\"Du m\u00e5 knytte et NTNU-brukernavn til kontoen din.\"))\n            return redirect('profiles_active', active_tab='membership')\n\n        form = FieldOfStudyApplicationForm(request.POST)\n        if form.is_valid():\n            cleaned = form.cleaned_data\n\n            field_of_study = int(cleaned['field_of_study'])\n\n            if field_of_study == 0:\n                messages.warning(request, _(\"Denne studieretningen (Gjest) er ikke et gyldig alternativ.\"))\n                return redirect('profiles_active', active_tab='membership')\n\n            started_day = 1\n            started_month = 0\n            started_year = int(cleaned['started_year'])\n\n            if cleaned['started_semester'] == \"h\":\n                started_month = 7\n            if cleaned['started_semester'] == \"v\":\n                started_month = 1\n\n            started_date = datetime.date(started_year, started_month, started_day)\n\n            # Does the user already have a field of study and started date?\n            if request.user.started_date and request.user.field_of_study:\n                # If there is no change from the current settings, ignore the request\n                if request.user.started_date == started_date and request.user.field_of_study == field_of_study:\n                    messages.error(\n                        request,\n                        _(\"Du er allerede registrert med denne studieretningen og denne startdatoen.\")\n                    )\n                    return redirect('profiles_active', active_tab='membership')\n\n            application = MembershipApproval(\n                applicant=request.user,\n                field_of_study=field_of_study,\n                started_date=started_date\n            )\n\n            length_of_fos = get_length_of_field_of_study(field_of_study)\n            if length_of_fos > 0:\n                application.new_expiry_date = get_expiry_date(started_year, length_of_fos)\n            application.save()\n\n            messages.success(request, _(\"S\u00f8knad om bytte av studieretning er sendt.\"))\n\n        return redirect('profiles_active', active_tab='membership')\n    raise Http404\n\n\ndef get_expiry_date(started_year, length_of_fos):\n    today = timezone.now().date()\n    # Expiry dates should be 15th September, so that we have time to get new lists from NTNU\n    new_expiry_date = datetime.date(\n        started_year, 9, 16) + datetime.timedelta(days=365*length_of_fos)\n    # Expiry dates in the past sets the expiry date to next september\n    if new_expiry_date < today:\n        if today < datetime.date(today.year, 9, 15):\n            new_expiry_date = datetime.date(today.year, 9, 15)\n        else:\n            new_expiry_date = datetime.date(\n                today.year, 9, 16) + datetime.timedelta(days=365)\n    return new_expiry_date\n\n\n@login_required\ndef create_membership_application(request):\n    if request.method == 'POST':\n        if not request.user.has_expiring_membership:\n            messages.error(request, _(\"Din bruker har ikke et utl\u00f8pende medlemskap.\"))\n            return redirect('profiles_active', active_tab='membership')\n\n        if not request.user.ntnu_username:\n            messages.error(request, _(\"Du m\u00e5 knytte et NTNU-brukernavn til kontoen din.\"))\n            return redirect('profiles_active', active_tab='membership')\n\n        # Extend length of membership by 1 year\n        membership = AllowedUsername.objects.get(username=request.user.ntnu_username)\n        new_expiration_date = datetime.date(membership.expiration_date.year + 1, 9, 16)\n\n        application = MembershipApproval(\n            applicant=request.user,\n            new_expiry_date=new_expiration_date,\n        )\n        application.save()\n\n        messages.success(request, _(\"S\u00f8knad om ett \u00e5rs forlenget medlemskap er sendt.\"))\n\n        return redirect('profiles_active', active_tab='membership')\n    raise Http404\n\n\n@login_required\ndef cancel_application(request, application_id):\n    app = get_object_or_404(MembershipApproval, pk=application_id)\n\n    if app.applicant != request.user:\n        messages.error(request, _(\"Bare s\u00f8keren selv kan slette en s\u00f8knad.\"))\n        return redirect('profiles_active', active_tab='membership')\n\n    if app.processed:\n        messages.error(request, _(\"Denne s\u00f8knaden er behandlet og kan ikke slettes.\"))\n        return redirect('profiles_active', active_tab='membership')\n\n    app.delete()\n\n    return redirect('profiles_active', active_tab='membership')\n", "path": "apps/approval/views.py"}]}