We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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. 


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `apps/approval/views.py`
Content:
```
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 FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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()

{"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"}], "after_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            field_of_study=request.user.field_of_study,\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"}]}