prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Updated membership "fails" for users with membership > 1 year old
  Currrently the new expiration date for memberships is determined like this:
  
  ``` python
  new_expiration_date = datetime.date(membership.expiration_date.year + 1, 9, 16)
  ```
  
  Source: https://github.com/dotKom/onlineweb4/blob/develop/apps/approval/views.py#L86
  
  If a user has membership that expired in 2014, they will get 2015 as the new date, which is still an expired date.
  
  
  --- 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 gettext as _
  11 
  12 from apps.approval.forms import FieldOfStudyApplicationForm
  13 from apps.approval.models import MembershipApproval
  14 from apps.authentication.models import Membership, get_length_of_membership
  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(
  22                 request, _("Du må knytte et NTNU-brukernavn til kontoen din.")
  23             )
  24             return redirect("profiles_active", active_tab="membership")
  25 
  26         form = FieldOfStudyApplicationForm(request.POST, request.FILES)
  27         if form.is_valid():
  28             cleaned = form.cleaned_data
  29 
  30             field_of_study = int(cleaned["field_of_study"])
  31 
  32             if field_of_study == 0:
  33                 messages.warning(
  34                     request,
  35                     _("Denne studieretningen (Gjest) er ikke et gyldig alternativ."),
  36                 )
  37                 return redirect("profiles_active", active_tab="membership")
  38 
  39             started_day = 1
  40             started_month = 0
  41             started_year = int(cleaned["started_year"])
  42 
  43             if cleaned["started_semester"] == "h":
  44                 started_month = 7
  45             if cleaned["started_semester"] == "v":
  46                 started_month = 1
  47 
  48             started_date = datetime.date(started_year, started_month, started_day)
  49 
  50             # Does the user already have a field of study and started date?
  51             if request.user.started_date and request.user.field_of_study:
  52                 # If there is no change from the current settings, ignore the
  53                 # request
  54                 if (
  55                     request.user.started_date == started_date
  56                     and request.user.field_of_study == field_of_study
  57                 ):
  58                     messages.error(
  59                         request,
  60                         _(
  61                             "Du er allerede registrert med denne studieretningen og denne startdatoen."
  62                         ),
  63                     )
  64                     return redirect("profiles_active", active_tab="membership")
  65 
  66             documentation = cleaned["documentation"]
  67 
  68             application = MembershipApproval(
  69                 applicant=request.user,
  70                 field_of_study=field_of_study,
  71                 started_date=started_date,
  72                 documentation=documentation,
  73             )
  74 
  75             length_of_fos = get_length_of_membership(field_of_study)
  76             if length_of_fos > 0:
  77                 application.new_expiry_date = get_expiry_date(
  78                     started_year, length_of_fos
  79                 )
  80             application.save()
  81 
  82             messages.success(request, _("Søknad om bytte av studieretning er sendt."))
  83 
  84         return redirect("profiles_active", active_tab="membership")
  85     raise Http404
  86 
  87 
  88 def get_expiry_date(started_year, length_of_fos):
  89     today = timezone.now().date()
  90     # Expiry dates should be 15th September, so that we have time to get new
  91     # lists from NTNU
  92     new_expiry_date = datetime.date(started_year, 9, 16) + datetime.timedelta(
  93         days=365 * length_of_fos
  94     )
  95     # Expiry dates in the past sets the expiry date to next september
  96     if new_expiry_date < today:
  97         if today < datetime.date(today.year, 9, 15):
  98             new_expiry_date = datetime.date(today.year, 9, 15)
  99         else:
  100             new_expiry_date = datetime.date(today.year, 9, 16) + datetime.timedelta(
  101                 days=365
  102             )
  103     return new_expiry_date
  104 
  105 
  106 @login_required
  107 def create_membership_application(request):
  108     if request.method == "POST":
  109         if not request.user.has_expiring_membership:
  110             messages.error(request, _("Din bruker har ikke et utløpende medlemskap."))
  111             return redirect("profiles_active", active_tab="membership")
  112 
  113         if not request.user.ntnu_username:
  114             messages.error(
  115                 request, _("Du må knytte et NTNU-brukernavn til kontoen din.")
  116             )
  117             return redirect("profiles_active", active_tab="membership")
  118 
  119         # Extend length of membership by 1 year
  120         membership = Membership.objects.get(username=request.user.ntnu_username)
  121         new_expiration_date = datetime.date(membership.expiration_date.year + 1, 9, 16)
  122 
  123         application = MembershipApproval(
  124             applicant=request.user,
  125             field_of_study=request.user.field_of_study,
  126             new_expiry_date=new_expiration_date,
  127         )
  128         application.save()
  129 
  130         messages.success(request, _("Søknad om ett års forlenget medlemskap er sendt."))
  131 
  132         return redirect("profiles_active", active_tab="membership")
  133     raise Http404
  134 
  135 
  136 @login_required
  137 def cancel_application(request, application_id):
  138     app = get_object_or_404(MembershipApproval, pk=application_id)
  139 
  140     if app.applicant != request.user:
  141         messages.error(request, _("Bare søkeren selv kan slette en søknad."))
  142         return redirect("profiles_active", active_tab="membership")
  143 
  144     if app.processed:
  145         messages.error(request, _("Denne søknaden er behandlet og kan ikke slettes."))
  146         return redirect("profiles_active", active_tab="membership")
  147 
  148     app.delete()
  149 
  150     return redirect("profiles_active", active_tab="membership")
  151
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/apps/approval/views.py b/apps/approval/views.py
  --- a/apps/approval/views.py
  +++ b/apps/approval/views.py
  @@ -11,7 +11,7 @@
   
   from apps.approval.forms import FieldOfStudyApplicationForm
   from apps.approval.models import MembershipApproval
  -from apps.authentication.models import Membership, get_length_of_membership
  +from apps.authentication.models import get_length_of_membership
   
   
   @login_required
  @@ -116,9 +116,14 @@
               )
               return redirect("profiles_active", active_tab="membership")
   
  -        # Extend length of membership by 1 year
  -        membership = Membership.objects.get(username=request.user.ntnu_username)
  -        new_expiration_date = datetime.date(membership.expiration_date.year + 1, 9, 16)
  +        # Grant membership until 16th of September this year if the request was sent previous to 1st of July,
  +        # or until 16th of September next year if the request was sent after 1st of July
  +        if timezone.now().date().month < 7:
  +            new_expiration_date = datetime.date(timezone.now().year, 9, 16)
  +        else:
  +            new_expiration_date = datetime.date(
  +                timezone.now().year, 9, 16
  +            ) + datetime.timedelta(days=365)
   
           application = MembershipApproval(
               applicant=request.user,
  
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@@ -11,7 +11,7 @@\n \n from apps.approval.forms import FieldOfStudyApplicationForm\n from apps.approval.models import MembershipApproval\n-from apps.authentication.models import Membership, get_length_of_membership\n+from apps.authentication.models import get_length_of_membership\n \n \n @login_required\n@@ -116,9 +116,14 @@\n             )\n             return redirect(\"profiles_active\", active_tab=\"membership\")\n \n-        # Extend length of membership by 1 year\n-        membership = Membership.objects.get(username=request.user.ntnu_username)\n-        new_expiration_date = datetime.date(membership.expiration_date.year + 1, 9, 16)\n+        # Grant membership until 16th of September this year if the request was sent previous to 1st of July,\n+        # or until 16th of September next year if the request was sent after 1st of July\n+        if timezone.now().date().month < 7:\n+            new_expiration_date = datetime.date(timezone.now().year, 9, 16)\n+        else:\n+            new_expiration_date = datetime.date(\n+                timezone.now().year, 9, 16\n+            ) + datetime.timedelta(days=365)\n \n         application = MembershipApproval(\n             applicant=request.user,\n", "issue": "Updated membership \"fails\" for users with membership > 1 year old\nCurrrently the new expiration date for memberships is determined like this:\n\n``` python\nnew_expiration_date = datetime.date(membership.expiration_date.year + 1, 9, 16)\n```\n\nSource: https://github.com/dotKom/onlineweb4/blob/develop/apps/approval/views.py#L86\n\nIf a user has membership that expired in 2014, they will get 2015 as the new date, which is still an expired date.\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 gettext as _\n\nfrom apps.approval.forms import FieldOfStudyApplicationForm\nfrom apps.approval.models import MembershipApproval\nfrom apps.authentication.models import Membership, get_length_of_membership\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(\n                request, _(\"Du m\u00e5 knytte et NTNU-brukernavn til kontoen din.\")\n            )\n            return redirect(\"profiles_active\", active_tab=\"membership\")\n\n        form = FieldOfStudyApplicationForm(request.POST, request.FILES)\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(\n                    request,\n                    _(\"Denne studieretningen (Gjest) er ikke et gyldig alternativ.\"),\n                )\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\n                # request\n                if (\n                    request.user.started_date == started_date\n                    and request.user.field_of_study == field_of_study\n                ):\n                    messages.error(\n                        request,\n                        _(\n                            \"Du er allerede registrert med denne studieretningen og denne startdatoen.\"\n                        ),\n                    )\n                    return redirect(\"profiles_active\", active_tab=\"membership\")\n\n            documentation = cleaned[\"documentation\"]\n\n            application = MembershipApproval(\n                applicant=request.user,\n                field_of_study=field_of_study,\n                started_date=started_date,\n                documentation=documentation,\n            )\n\n            length_of_fos = get_length_of_membership(field_of_study)\n            if length_of_fos > 0:\n                application.new_expiry_date = get_expiry_date(\n                    started_year, length_of_fos\n                )\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\n    # lists from NTNU\n    new_expiry_date = datetime.date(started_year, 9, 16) + datetime.timedelta(\n        days=365 * length_of_fos\n    )\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(today.year, 9, 16) + datetime.timedelta(\n                days=365\n            )\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(\n                request, _(\"Du m\u00e5 knytte et NTNU-brukernavn til kontoen din.\")\n            )\n            return redirect(\"profiles_active\", active_tab=\"membership\")\n\n        # Extend length of membership by 1 year\n        membership = Membership.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"}], "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 gettext as _\n\nfrom apps.approval.forms import FieldOfStudyApplicationForm\nfrom apps.approval.models import MembershipApproval\nfrom apps.authentication.models import get_length_of_membership\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(\n                request, _(\"Du m\u00e5 knytte et NTNU-brukernavn til kontoen din.\")\n            )\n            return redirect(\"profiles_active\", active_tab=\"membership\")\n\n        form = FieldOfStudyApplicationForm(request.POST, request.FILES)\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(\n                    request,\n                    _(\"Denne studieretningen (Gjest) er ikke et gyldig alternativ.\"),\n                )\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\n                # request\n                if (\n                    request.user.started_date == started_date\n                    and request.user.field_of_study == field_of_study\n                ):\n                    messages.error(\n                        request,\n                        _(\n                            \"Du er allerede registrert med denne studieretningen og denne startdatoen.\"\n                        ),\n                    )\n                    return redirect(\"profiles_active\", active_tab=\"membership\")\n\n            documentation = cleaned[\"documentation\"]\n\n            application = MembershipApproval(\n                applicant=request.user,\n                field_of_study=field_of_study,\n                started_date=started_date,\n                documentation=documentation,\n            )\n\n            length_of_fos = get_length_of_membership(field_of_study)\n            if length_of_fos > 0:\n                application.new_expiry_date = get_expiry_date(\n                    started_year, length_of_fos\n                )\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\n    # lists from NTNU\n    new_expiry_date = datetime.date(started_year, 9, 16) + datetime.timedelta(\n        days=365 * length_of_fos\n    )\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(today.year, 9, 16) + datetime.timedelta(\n                days=365\n            )\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(\n                request, _(\"Du m\u00e5 knytte et NTNU-brukernavn til kontoen din.\")\n            )\n            return redirect(\"profiles_active\", active_tab=\"membership\")\n\n        # Grant membership until 16th of September this year if the request was sent previous to 1st of July,\n        # or until 16th of September next year if the request was sent after 1st of July\n        if timezone.now().date().month < 7:\n            new_expiration_date = datetime.date(timezone.now().year, 9, 16)\n        else:\n            new_expiration_date = datetime.date(\n                timezone.now().year, 9, 16\n            ) + datetime.timedelta(days=365)\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"}]}