prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  More 504s: on the results framework page
  @Geerts reports on Skype: 504 Gateway timeout hunter strikes again: http://rsr.test.akvo.org/rest/v1/indicator_period_data_framework/?format=json&period__indicator__result__project=2780
  
  Via: http://rsr.test.akvo.org/en/myrsr/results/2780/#results,13323,5679
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `akvo/rest/views/indicator_period_data.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 
  3 # Akvo RSR is covered by the GNU Affero General Public License.
  4 # See more details in the license.txt file located at the root folder of the Akvo RSR module.
  5 # For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
  6 
  7 
  8 from akvo.rsr.models import IndicatorPeriodData, IndicatorPeriodDataComment
  9 
  10 from ..serializers import (IndicatorPeriodDataSerializer, IndicatorPeriodDataFrameworkSerializer,
  11                            IndicatorPeriodDataCommentSerializer)
  12 from ..viewsets import PublicProjectViewSet
  13 
  14 from django.http import HttpResponseForbidden
  15 
  16 from rest_framework import status
  17 from rest_framework.decorators import api_view, permission_classes
  18 from rest_framework.response import Response
  19 
  20 
  21 class IndicatorPeriodDataViewSet(PublicProjectViewSet):
  22     """
  23     """
  24     queryset = IndicatorPeriodData.objects.all()
  25     serializer_class = IndicatorPeriodDataSerializer
  26 
  27     project_relation = 'period__indicator__result__project__'
  28 
  29 
  30 class IndicatorPeriodDataFrameworkViewSet(PublicProjectViewSet):
  31     """
  32     """
  33     queryset = IndicatorPeriodData.objects.all()
  34     serializer_class = IndicatorPeriodDataFrameworkSerializer
  35     project_relation = 'period__indicator__result__project__'
  36 
  37 
  38 class IndicatorPeriodDataCommentViewSet(PublicProjectViewSet):
  39     """
  40     """
  41     queryset = IndicatorPeriodDataComment.objects.all()
  42     serializer_class = IndicatorPeriodDataCommentSerializer
  43     project_relation = 'data__period__indicator__result__project__'
  44 
  45 
  46 @api_view(['POST'])
  47 def indicator_upload_file(request, pk=None):
  48     """
  49     Special API call for directly uploading a file.
  50 
  51     :param request; A Django request object.
  52     :param pk; The primary key of an IndicatorPeriodData instance.
  53     """
  54     update = IndicatorPeriodData.objects.get(pk=pk)
  55     upload_file = request.data['file']
  56 
  57     # Permissions
  58     user = getattr(request, 'user', None)
  59     if not user:
  60         return Response({'error': 'User is not logged in'}, status=status.HTTP_403_FORBIDDEN)
  61 
  62     # TODO: Check if user is allowed to upload a file
  63     # if not user.has_perm('rsr.change_project', update.period.indicator.result.project):
  64     #     return Response({'error': 'User has no permission to place an update'},
  65     #                     status=status.HTTP_403_FORBIDDEN)
  66 
  67     try:
  68         file_type = request.POST.copy()['type']
  69         if file_type == 'photo':
  70             update.photo = upload_file
  71             update.save(update_fields=['photo'])
  72             return Response({'file': update.photo.url})
  73         elif file_type == 'file':
  74             update.file = upload_file
  75             update.save(update_fields=['file'])
  76             return Response({'file': update.file.url})
  77     except Exception as e:
  78         return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
  79
  ```
  
  Path: `akvo/rest/views/partnership.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 
  3 # Akvo RSR is covered by the GNU Affero General Public License.
  4 # See more details in the license.txt file located at the root folder of the Akvo RSR module.
  5 # For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.
  6 
  7 
  8 from akvo.rsr.models import Partnership
  9 
  10 from ..serializers import PartnershipSerializer, PartnershipBasicSerializer
  11 from ..viewsets import PublicProjectViewSet
  12 
  13 
  14 class PartnershipViewSet(PublicProjectViewSet):
  15     """
  16     """
  17     queryset = Partnership.objects.all()
  18     serializer_class = PartnershipSerializer
  19 
  20     def get_queryset(self):
  21         """Allow filtering on partner_type."""
  22         partner_type = self.request.query_params.get('partner_type', None)
  23         if partner_type and partner_type in Partnership.PARTNER_TYPES_TO_ROLES_MAP.keys():
  24             self.queryset = self.queryset.filter(
  25                 iati_organisation_role=Partnership.PARTNER_TYPES_TO_ROLES_MAP[partner_type]
  26             ).distinct()
  27         return super(PartnershipViewSet, self).get_queryset()
  28 
  29 
  30 class PartnershipMoreLinkViewSet(PublicProjectViewSet):
  31     """
  32     Specific endpoint for the '+X partners' links in RSR. Contains the name, long name and logo of
  33     an organisation and the partnership role.
  34     """
  35     queryset = Partnership.objects.all()
  36     serializer_class = PartnershipBasicSerializer
  37
  ```
  --- 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/akvo/rest/views/indicator_period_data.py b/akvo/rest/views/indicator_period_data.py
  --- a/akvo/rest/views/indicator_period_data.py
  +++ b/akvo/rest/views/indicator_period_data.py
  @@ -30,7 +30,13 @@
   class IndicatorPeriodDataFrameworkViewSet(PublicProjectViewSet):
       """
       """
  -    queryset = IndicatorPeriodData.objects.all()
  +    queryset = IndicatorPeriodData.objects.select_related(
  +        'period',
  +        'user'
  +    ).prefetch_related(
  +        'comments',
  +        'comments__user'
  +    ).all()
       serializer_class = IndicatorPeriodDataFrameworkSerializer
       project_relation = 'period__indicator__result__project__'
   
  diff --git a/akvo/rest/views/partnership.py b/akvo/rest/views/partnership.py
  --- a/akvo/rest/views/partnership.py
  +++ b/akvo/rest/views/partnership.py
  @@ -14,7 +14,7 @@
   class PartnershipViewSet(PublicProjectViewSet):
       """
       """
  -    queryset = Partnership.objects.all()
  +    queryset = Partnership.objects.select_related('organisation', 'project').all()
       serializer_class = PartnershipSerializer
   
       def get_queryset(self):
  
verification_info: {"golden_diff": "diff --git a/akvo/rest/views/indicator_period_data.py b/akvo/rest/views/indicator_period_data.py\n--- a/akvo/rest/views/indicator_period_data.py\n+++ b/akvo/rest/views/indicator_period_data.py\n@@ -30,7 +30,13 @@\n class IndicatorPeriodDataFrameworkViewSet(PublicProjectViewSet):\n     \"\"\"\n     \"\"\"\n-    queryset = IndicatorPeriodData.objects.all()\n+    queryset = IndicatorPeriodData.objects.select_related(\n+        'period',\n+        'user'\n+    ).prefetch_related(\n+        'comments',\n+        'comments__user'\n+    ).all()\n     serializer_class = IndicatorPeriodDataFrameworkSerializer\n     project_relation = 'period__indicator__result__project__'\n \ndiff --git a/akvo/rest/views/partnership.py b/akvo/rest/views/partnership.py\n--- a/akvo/rest/views/partnership.py\n+++ b/akvo/rest/views/partnership.py\n@@ -14,7 +14,7 @@\n class PartnershipViewSet(PublicProjectViewSet):\n     \"\"\"\n     \"\"\"\n-    queryset = Partnership.objects.all()\n+    queryset = Partnership.objects.select_related('organisation', 'project').all()\n     serializer_class = PartnershipSerializer\n \n     def get_queryset(self):\n", "issue": "More 504s: on the results framework page\n@Geerts reports on Skype: 504 Gateway timeout hunter strikes again: http://rsr.test.akvo.org/rest/v1/indicator_period_data_framework/?format=json&period__indicator__result__project=2780\r\n\r\nVia: http://rsr.test.akvo.org/en/myrsr/results/2780/#results,13323,5679\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Akvo RSR is covered by the GNU Affero General Public License.\n# See more details in the license.txt file located at the root folder of the Akvo RSR module.\n# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\n\nfrom akvo.rsr.models import IndicatorPeriodData, IndicatorPeriodDataComment\n\nfrom ..serializers import (IndicatorPeriodDataSerializer, IndicatorPeriodDataFrameworkSerializer,\n                           IndicatorPeriodDataCommentSerializer)\nfrom ..viewsets import PublicProjectViewSet\n\nfrom django.http import HttpResponseForbidden\n\nfrom rest_framework import status\nfrom rest_framework.decorators import api_view, permission_classes\nfrom rest_framework.response import Response\n\n\nclass IndicatorPeriodDataViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = IndicatorPeriodData.objects.all()\n    serializer_class = IndicatorPeriodDataSerializer\n\n    project_relation = 'period__indicator__result__project__'\n\n\nclass IndicatorPeriodDataFrameworkViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = IndicatorPeriodData.objects.all()\n    serializer_class = IndicatorPeriodDataFrameworkSerializer\n    project_relation = 'period__indicator__result__project__'\n\n\nclass IndicatorPeriodDataCommentViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = IndicatorPeriodDataComment.objects.all()\n    serializer_class = IndicatorPeriodDataCommentSerializer\n    project_relation = 'data__period__indicator__result__project__'\n\n\n@api_view(['POST'])\ndef indicator_upload_file(request, pk=None):\n    \"\"\"\n    Special API call for directly uploading a file.\n\n    :param request; A Django request object.\n    :param pk; The primary key of an IndicatorPeriodData instance.\n    \"\"\"\n    update = IndicatorPeriodData.objects.get(pk=pk)\n    upload_file = request.data['file']\n\n    # Permissions\n    user = getattr(request, 'user', None)\n    if not user:\n        return Response({'error': 'User is not logged in'}, status=status.HTTP_403_FORBIDDEN)\n\n    # TODO: Check if user is allowed to upload a file\n    # if not user.has_perm('rsr.change_project', update.period.indicator.result.project):\n    #     return Response({'error': 'User has no permission to place an update'},\n    #                     status=status.HTTP_403_FORBIDDEN)\n\n    try:\n        file_type = request.POST.copy()['type']\n        if file_type == 'photo':\n            update.photo = upload_file\n            update.save(update_fields=['photo'])\n            return Response({'file': update.photo.url})\n        elif file_type == 'file':\n            update.file = upload_file\n            update.save(update_fields=['file'])\n            return Response({'file': update.file.url})\n    except Exception as e:\n        return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)\n", "path": "akvo/rest/views/indicator_period_data.py"}, {"content": "# -*- coding: utf-8 -*-\n\n# Akvo RSR is covered by the GNU Affero General Public License.\n# See more details in the license.txt file located at the root folder of the Akvo RSR module.\n# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\n\nfrom akvo.rsr.models import Partnership\n\nfrom ..serializers import PartnershipSerializer, PartnershipBasicSerializer\nfrom ..viewsets import PublicProjectViewSet\n\n\nclass PartnershipViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = Partnership.objects.all()\n    serializer_class = PartnershipSerializer\n\n    def get_queryset(self):\n        \"\"\"Allow filtering on partner_type.\"\"\"\n        partner_type = self.request.query_params.get('partner_type', None)\n        if partner_type and partner_type in Partnership.PARTNER_TYPES_TO_ROLES_MAP.keys():\n            self.queryset = self.queryset.filter(\n                iati_organisation_role=Partnership.PARTNER_TYPES_TO_ROLES_MAP[partner_type]\n            ).distinct()\n        return super(PartnershipViewSet, self).get_queryset()\n\n\nclass PartnershipMoreLinkViewSet(PublicProjectViewSet):\n    \"\"\"\n    Specific endpoint for the '+X partners' links in RSR. Contains the name, long name and logo of\n    an organisation and the partnership role.\n    \"\"\"\n    queryset = Partnership.objects.all()\n    serializer_class = PartnershipBasicSerializer\n", "path": "akvo/rest/views/partnership.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Akvo RSR is covered by the GNU Affero General Public License.\n# See more details in the license.txt file located at the root folder of the Akvo RSR module.\n# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\n\nfrom akvo.rsr.models import IndicatorPeriodData, IndicatorPeriodDataComment\n\nfrom ..serializers import (IndicatorPeriodDataSerializer, IndicatorPeriodDataFrameworkSerializer,\n                           IndicatorPeriodDataCommentSerializer)\nfrom ..viewsets import PublicProjectViewSet\n\nfrom django.http import HttpResponseForbidden\n\nfrom rest_framework import status\nfrom rest_framework.decorators import api_view, permission_classes\nfrom rest_framework.response import Response\n\n\nclass IndicatorPeriodDataViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = IndicatorPeriodData.objects.all()\n    serializer_class = IndicatorPeriodDataSerializer\n\n    project_relation = 'period__indicator__result__project__'\n\n\nclass IndicatorPeriodDataFrameworkViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = IndicatorPeriodData.objects.select_related(\n        'period',\n        'user'\n    ).prefetch_related(\n        'comments',\n        'comments__user'\n    ).all()\n    serializer_class = IndicatorPeriodDataFrameworkSerializer\n    project_relation = 'period__indicator__result__project__'\n\n\nclass IndicatorPeriodDataCommentViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = IndicatorPeriodDataComment.objects.all()\n    serializer_class = IndicatorPeriodDataCommentSerializer\n    project_relation = 'data__period__indicator__result__project__'\n\n\n@api_view(['POST'])\ndef indicator_upload_file(request, pk=None):\n    \"\"\"\n    Special API call for directly uploading a file.\n\n    :param request; A Django request object.\n    :param pk; The primary key of an IndicatorPeriodData instance.\n    \"\"\"\n    update = IndicatorPeriodData.objects.get(pk=pk)\n    upload_file = request.data['file']\n\n    # Permissions\n    user = getattr(request, 'user', None)\n    if not user:\n        return Response({'error': 'User is not logged in'}, status=status.HTTP_403_FORBIDDEN)\n\n    # TODO: Check if user is allowed to upload a file\n    # if not user.has_perm('rsr.change_project', update.period.indicator.result.project):\n    #     return Response({'error': 'User has no permission to place an update'},\n    #                     status=status.HTTP_403_FORBIDDEN)\n\n    try:\n        file_type = request.POST.copy()['type']\n        if file_type == 'photo':\n            update.photo = upload_file\n            update.save(update_fields=['photo'])\n            return Response({'file': update.photo.url})\n        elif file_type == 'file':\n            update.file = upload_file\n            update.save(update_fields=['file'])\n            return Response({'file': update.file.url})\n    except Exception as e:\n        return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)\n", "path": "akvo/rest/views/indicator_period_data.py"}, {"content": "# -*- coding: utf-8 -*-\n\n# Akvo RSR is covered by the GNU Affero General Public License.\n# See more details in the license.txt file located at the root folder of the Akvo RSR module.\n# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.\n\n\nfrom akvo.rsr.models import Partnership\n\nfrom ..serializers import PartnershipSerializer, PartnershipBasicSerializer\nfrom ..viewsets import PublicProjectViewSet\n\n\nclass PartnershipViewSet(PublicProjectViewSet):\n    \"\"\"\n    \"\"\"\n    queryset = Partnership.objects.select_related('organisation', 'project').all()\n    serializer_class = PartnershipSerializer\n\n    def get_queryset(self):\n        \"\"\"Allow filtering on partner_type.\"\"\"\n        partner_type = self.request.query_params.get('partner_type', None)\n        if partner_type and partner_type in Partnership.PARTNER_TYPES_TO_ROLES_MAP.keys():\n            self.queryset = self.queryset.filter(\n                iati_organisation_role=Partnership.PARTNER_TYPES_TO_ROLES_MAP[partner_type]\n            ).distinct()\n        return super(PartnershipViewSet, self).get_queryset()\n\n\nclass PartnershipMoreLinkViewSet(PublicProjectViewSet):\n    \"\"\"\n    Specific endpoint for the '+X partners' links in RSR. Contains the name, long name and logo of\n    an organisation and the partnership role.\n    \"\"\"\n    queryset = Partnership.objects.all()\n    serializer_class = PartnershipBasicSerializer\n", "path": "akvo/rest/views/partnership.py"}]}