prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Tri des évènements touristiques dans l'API
  Par défaut les évènement ne sont pas triés par date de départ, ce qui implique qu'ils apparaissent dans le désordre dans Geotrek-Rando.
  
  Il faut modifier l'API pour changer cela.
  
  https://github.com/GeotrekCE/Geotrek-admin/blob/master/geotrek/api/v2/views/tourism.py#LL103C30-L103C30
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `geotrek/api/v2/views/tourism.py`
  Content:
  ```
  1 from django.conf import settings
  2 from django.contrib.gis.db.models.functions import Transform
  3 from django.db.models import F, Case, When
  4 from django.db.models.query import Prefetch
  5 from django.shortcuts import get_object_or_404
  6 from django.utils.translation import activate
  7 
  8 from rest_framework.response import Response
  9 
  10 from geotrek.api.v2 import serializers as api_serializers, \
  11     filters as api_filters, viewsets as api_viewsets
  12 from geotrek.api.v2.decorators import cache_response_detail
  13 from geotrek.common.models import Attachment
  14 from geotrek.tourism import models as tourism_models
  15 
  16 
  17 class LabelAccessibilityViewSet(api_viewsets.GeotrekViewSet):
  18     serializer_class = api_serializers.LabelAccessibilitySerializer
  19     queryset = tourism_models.LabelAccessibility.objects.order_by('pk')  # Required for reliable pagination
  20 
  21 
  22 class TouristicContentCategoryViewSet(api_viewsets.GeotrekViewSet):
  23     filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TouristicContentRelatedPortalFilter,)
  24     serializer_class = api_serializers.TouristicContentCategorySerializer
  25     queryset = tourism_models.TouristicContentCategory.objects \
  26         .prefetch_related('types') \
  27         .order_by('pk')  # Required for reliable pagination
  28 
  29     @cache_response_detail()
  30     def retrieve(self, request, pk=None, format=None):
  31         # Allow to retrieve objects even if not visible in list view
  32         elem = get_object_or_404(tourism_models.TouristicContentCategory, pk=pk)
  33         serializer = api_serializers.TouristicContentCategorySerializer(elem, many=False, context={'request': request})
  34         return Response(serializer.data)
  35 
  36 
  37 class TouristicContentViewSet(api_viewsets.GeotrekGeometricViewset):
  38     filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (
  39         api_filters.GeotrekTouristicContentFilter,
  40         api_filters.NearbyContentFilter,
  41         api_filters.UpdateOrCreateDateFilter
  42     )
  43     serializer_class = api_serializers.TouristicContentSerializer
  44 
  45     def get_queryset(self):
  46         activate(self.request.GET.get('language'))
  47         return tourism_models.TouristicContent.objects.existing()\
  48             .select_related('category', 'reservation_system', 'label_accessibility') \
  49             .prefetch_related('source', 'themes', 'type1', 'type2',
  50                               Prefetch('attachments',
  51                                        queryset=Attachment.objects.select_related('license', 'filetype__structure').order_by('starred', '-date_insert'))
  52                               ) \
  53             .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \
  54             .order_by('name')  # Required for reliable pagination
  55 
  56 
  57 class InformationDeskTypeViewSet(api_viewsets.GeotrekViewSet):
  58     serializer_class = api_serializers.InformationDeskTypeSerializer
  59     queryset = tourism_models.InformationDeskType.objects.order_by('pk')
  60 
  61 
  62 class InformationDeskViewSet(api_viewsets.GeotrekViewSet):
  63     filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TreksAndSitesRelatedPortalFilter,
  64                                                                      api_filters.GeotrekInformationDeskFilter)
  65     serializer_class = api_serializers.InformationDeskSerializer
  66 
  67     def get_queryset(self):
  68         activate(self.request.GET.get('language'))
  69         return tourism_models.InformationDesk.objects.select_related('label_accessibility', 'type').order_by('name')
  70 
  71     @cache_response_detail()
  72     def retrieve(self, request, pk=None, format=None):
  73         # Allow to retrieve objects even if not visible in list view
  74         elem = get_object_or_404(tourism_models.InformationDesk, pk=pk)
  75         serializer = api_serializers.InformationDeskSerializer(elem, many=False, context={'request': request})
  76         return Response(serializer.data)
  77 
  78 
  79 class TouristicEventTypeViewSet(api_viewsets.GeotrekViewSet):
  80     filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TouristicEventRelatedPortalFilter, )
  81     serializer_class = api_serializers.TouristicEventTypeSerializer
  82     queryset = tourism_models.TouristicEventType.objects.order_by('pk')  # Required for reliable pagination
  83 
  84 
  85 class TouristicEventViewSet(api_viewsets.GeotrekGeometricViewset):
  86     filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (
  87         api_filters.GeotrekTouristicEventFilter,
  88         api_filters.NearbyContentFilter,
  89         api_filters.UpdateOrCreateDateFilter,
  90     )
  91     filterset_class = api_filters.TouristicEventFilterSet
  92     serializer_class = api_serializers.TouristicEventSerializer
  93 
  94     def get_queryset(self):
  95         activate(self.request.GET.get('language'))
  96         return tourism_models.TouristicEvent.objects.existing()\
  97             .select_related('type') \
  98             .prefetch_related('themes', 'source', 'portal',
  99                               Prefetch('attachments',
  100                                        queryset=Attachment.objects.select_related('license', 'filetype', 'filetype__structure'))
  101                               ) \
  102             .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \
  103             .annotate(ordering_date=Case(
  104                 When(end_date__isnull=True, then=F('begin_date')),
  105                 default=F('end_date'),
  106             )) \
  107             .order_by('ordering_date')  # Required for reliable pagination
  108 
  109 
  110 class TouristicEventPlaceViewSet(api_viewsets.GeotrekGeometricViewset):
  111     filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (
  112         api_filters.UpdateOrCreateDateFilter,
  113         api_filters.TouristicEventsRelatedPortalFilter
  114     )
  115     serializer_class = api_serializers.TouristicEventPlaceSerializer
  116 
  117     def get_queryset(self):
  118         return tourism_models.TouristicEventPlace.objects.prefetch_related('touristicevents').annotate(
  119             geom_transformed=Transform('geom', settings.API_SRID)
  120         ).order_by('name')
  121 
  122 
  123 class TouristicEventOrganizerViewSet(api_viewsets.GeotrekGeometricViewset):
  124     filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (
  125         api_filters.UpdateOrCreateDateFilter,
  126         api_filters.TouristicEventRelatedPortalFilter
  127     )
  128     serializer_class = api_serializers.TouristicEventOrganizerSerializer
  129     queryset = tourism_models.TouristicEventOrganizer.objects.order_by('label')
  130
  ```
  --- 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/geotrek/api/v2/views/tourism.py b/geotrek/api/v2/views/tourism.py
  --- a/geotrek/api/v2/views/tourism.py
  +++ b/geotrek/api/v2/views/tourism.py
  @@ -1,6 +1,6 @@
   from django.conf import settings
   from django.contrib.gis.db.models.functions import Transform
  -from django.db.models import F, Case, When
  +from django.db.models import F
   from django.db.models.query import Prefetch
   from django.shortcuts import get_object_or_404
   from django.utils.translation import activate
  @@ -100,11 +100,7 @@
                                          queryset=Attachment.objects.select_related('license', 'filetype', 'filetype__structure'))
                                 ) \
               .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \
  -            .annotate(ordering_date=Case(
  -                When(end_date__isnull=True, then=F('begin_date')),
  -                default=F('end_date'),
  -            )) \
  -            .order_by('ordering_date')  # Required for reliable pagination
  +            .order_by('begin_date')  # Required for reliable pagination
   
   
   class TouristicEventPlaceViewSet(api_viewsets.GeotrekGeometricViewset):
  
verification_info: {"golden_diff": "diff --git a/geotrek/api/v2/views/tourism.py b/geotrek/api/v2/views/tourism.py\n--- a/geotrek/api/v2/views/tourism.py\n+++ b/geotrek/api/v2/views/tourism.py\n@@ -1,6 +1,6 @@\n from django.conf import settings\n from django.contrib.gis.db.models.functions import Transform\n-from django.db.models import F, Case, When\n+from django.db.models import F\n from django.db.models.query import Prefetch\n from django.shortcuts import get_object_or_404\n from django.utils.translation import activate\n@@ -100,11 +100,7 @@\n                                        queryset=Attachment.objects.select_related('license', 'filetype', 'filetype__structure'))\n                               ) \\\n             .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \\\n-            .annotate(ordering_date=Case(\n-                When(end_date__isnull=True, then=F('begin_date')),\n-                default=F('end_date'),\n-            )) \\\n-            .order_by('ordering_date')  # Required for reliable pagination\n+            .order_by('begin_date')  # Required for reliable pagination\n \n \n class TouristicEventPlaceViewSet(api_viewsets.GeotrekGeometricViewset):\n", "issue": "Tri des \u00e9v\u00e8nements touristiques dans l'API\nPar d\u00e9faut les \u00e9v\u00e8nement ne sont pas tri\u00e9s par date de d\u00e9part, ce qui implique qu'ils apparaissent dans le d\u00e9sordre dans Geotrek-Rando.\r\n\r\nIl faut modifier l'API pour changer cela.\r\n\r\nhttps://github.com/GeotrekCE/Geotrek-admin/blob/master/geotrek/api/v2/views/tourism.py#LL103C30-L103C30\n", "before_files": [{"content": "from django.conf import settings\nfrom django.contrib.gis.db.models.functions import Transform\nfrom django.db.models import F, Case, When\nfrom django.db.models.query import Prefetch\nfrom django.shortcuts import get_object_or_404\nfrom django.utils.translation import activate\n\nfrom rest_framework.response import Response\n\nfrom geotrek.api.v2 import serializers as api_serializers, \\\n    filters as api_filters, viewsets as api_viewsets\nfrom geotrek.api.v2.decorators import cache_response_detail\nfrom geotrek.common.models import Attachment\nfrom geotrek.tourism import models as tourism_models\n\n\nclass LabelAccessibilityViewSet(api_viewsets.GeotrekViewSet):\n    serializer_class = api_serializers.LabelAccessibilitySerializer\n    queryset = tourism_models.LabelAccessibility.objects.order_by('pk')  # Required for reliable pagination\n\n\nclass TouristicContentCategoryViewSet(api_viewsets.GeotrekViewSet):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TouristicContentRelatedPortalFilter,)\n    serializer_class = api_serializers.TouristicContentCategorySerializer\n    queryset = tourism_models.TouristicContentCategory.objects \\\n        .prefetch_related('types') \\\n        .order_by('pk')  # Required for reliable pagination\n\n    @cache_response_detail()\n    def retrieve(self, request, pk=None, format=None):\n        # Allow to retrieve objects even if not visible in list view\n        elem = get_object_or_404(tourism_models.TouristicContentCategory, pk=pk)\n        serializer = api_serializers.TouristicContentCategorySerializer(elem, many=False, context={'request': request})\n        return Response(serializer.data)\n\n\nclass TouristicContentViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (\n        api_filters.GeotrekTouristicContentFilter,\n        api_filters.NearbyContentFilter,\n        api_filters.UpdateOrCreateDateFilter\n    )\n    serializer_class = api_serializers.TouristicContentSerializer\n\n    def get_queryset(self):\n        activate(self.request.GET.get('language'))\n        return tourism_models.TouristicContent.objects.existing()\\\n            .select_related('category', 'reservation_system', 'label_accessibility') \\\n            .prefetch_related('source', 'themes', 'type1', 'type2',\n                              Prefetch('attachments',\n                                       queryset=Attachment.objects.select_related('license', 'filetype__structure').order_by('starred', '-date_insert'))\n                              ) \\\n            .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \\\n            .order_by('name')  # Required for reliable pagination\n\n\nclass InformationDeskTypeViewSet(api_viewsets.GeotrekViewSet):\n    serializer_class = api_serializers.InformationDeskTypeSerializer\n    queryset = tourism_models.InformationDeskType.objects.order_by('pk')\n\n\nclass InformationDeskViewSet(api_viewsets.GeotrekViewSet):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TreksAndSitesRelatedPortalFilter,\n                                                                     api_filters.GeotrekInformationDeskFilter)\n    serializer_class = api_serializers.InformationDeskSerializer\n\n    def get_queryset(self):\n        activate(self.request.GET.get('language'))\n        return tourism_models.InformationDesk.objects.select_related('label_accessibility', 'type').order_by('name')\n\n    @cache_response_detail()\n    def retrieve(self, request, pk=None, format=None):\n        # Allow to retrieve objects even if not visible in list view\n        elem = get_object_or_404(tourism_models.InformationDesk, pk=pk)\n        serializer = api_serializers.InformationDeskSerializer(elem, many=False, context={'request': request})\n        return Response(serializer.data)\n\n\nclass TouristicEventTypeViewSet(api_viewsets.GeotrekViewSet):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TouristicEventRelatedPortalFilter, )\n    serializer_class = api_serializers.TouristicEventTypeSerializer\n    queryset = tourism_models.TouristicEventType.objects.order_by('pk')  # Required for reliable pagination\n\n\nclass TouristicEventViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (\n        api_filters.GeotrekTouristicEventFilter,\n        api_filters.NearbyContentFilter,\n        api_filters.UpdateOrCreateDateFilter,\n    )\n    filterset_class = api_filters.TouristicEventFilterSet\n    serializer_class = api_serializers.TouristicEventSerializer\n\n    def get_queryset(self):\n        activate(self.request.GET.get('language'))\n        return tourism_models.TouristicEvent.objects.existing()\\\n            .select_related('type') \\\n            .prefetch_related('themes', 'source', 'portal',\n                              Prefetch('attachments',\n                                       queryset=Attachment.objects.select_related('license', 'filetype', 'filetype__structure'))\n                              ) \\\n            .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \\\n            .annotate(ordering_date=Case(\n                When(end_date__isnull=True, then=F('begin_date')),\n                default=F('end_date'),\n            )) \\\n            .order_by('ordering_date')  # Required for reliable pagination\n\n\nclass TouristicEventPlaceViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (\n        api_filters.UpdateOrCreateDateFilter,\n        api_filters.TouristicEventsRelatedPortalFilter\n    )\n    serializer_class = api_serializers.TouristicEventPlaceSerializer\n\n    def get_queryset(self):\n        return tourism_models.TouristicEventPlace.objects.prefetch_related('touristicevents').annotate(\n            geom_transformed=Transform('geom', settings.API_SRID)\n        ).order_by('name')\n\n\nclass TouristicEventOrganizerViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (\n        api_filters.UpdateOrCreateDateFilter,\n        api_filters.TouristicEventRelatedPortalFilter\n    )\n    serializer_class = api_serializers.TouristicEventOrganizerSerializer\n    queryset = tourism_models.TouristicEventOrganizer.objects.order_by('label')\n", "path": "geotrek/api/v2/views/tourism.py"}], "after_files": [{"content": "from django.conf import settings\nfrom django.contrib.gis.db.models.functions import Transform\nfrom django.db.models import F\nfrom django.db.models.query import Prefetch\nfrom django.shortcuts import get_object_or_404\nfrom django.utils.translation import activate\n\nfrom rest_framework.response import Response\n\nfrom geotrek.api.v2 import serializers as api_serializers, \\\n    filters as api_filters, viewsets as api_viewsets\nfrom geotrek.api.v2.decorators import cache_response_detail\nfrom geotrek.common.models import Attachment\nfrom geotrek.tourism import models as tourism_models\n\n\nclass LabelAccessibilityViewSet(api_viewsets.GeotrekViewSet):\n    serializer_class = api_serializers.LabelAccessibilitySerializer\n    queryset = tourism_models.LabelAccessibility.objects.order_by('pk')  # Required for reliable pagination\n\n\nclass TouristicContentCategoryViewSet(api_viewsets.GeotrekViewSet):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TouristicContentRelatedPortalFilter,)\n    serializer_class = api_serializers.TouristicContentCategorySerializer\n    queryset = tourism_models.TouristicContentCategory.objects \\\n        .prefetch_related('types') \\\n        .order_by('pk')  # Required for reliable pagination\n\n    @cache_response_detail()\n    def retrieve(self, request, pk=None, format=None):\n        # Allow to retrieve objects even if not visible in list view\n        elem = get_object_or_404(tourism_models.TouristicContentCategory, pk=pk)\n        serializer = api_serializers.TouristicContentCategorySerializer(elem, many=False, context={'request': request})\n        return Response(serializer.data)\n\n\nclass TouristicContentViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (\n        api_filters.GeotrekTouristicContentFilter,\n        api_filters.NearbyContentFilter,\n        api_filters.UpdateOrCreateDateFilter\n    )\n    serializer_class = api_serializers.TouristicContentSerializer\n\n    def get_queryset(self):\n        activate(self.request.GET.get('language'))\n        return tourism_models.TouristicContent.objects.existing()\\\n            .select_related('category', 'reservation_system', 'label_accessibility') \\\n            .prefetch_related('source', 'themes', 'type1', 'type2',\n                              Prefetch('attachments',\n                                       queryset=Attachment.objects.select_related('license', 'filetype__structure').order_by('starred', '-date_insert'))\n                              ) \\\n            .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \\\n            .order_by('name')  # Required for reliable pagination\n\n\nclass InformationDeskTypeViewSet(api_viewsets.GeotrekViewSet):\n    serializer_class = api_serializers.InformationDeskTypeSerializer\n    queryset = tourism_models.InformationDeskType.objects.order_by('pk')\n\n\nclass InformationDeskViewSet(api_viewsets.GeotrekViewSet):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TreksAndSitesRelatedPortalFilter,\n                                                                     api_filters.GeotrekInformationDeskFilter)\n    serializer_class = api_serializers.InformationDeskSerializer\n\n    def get_queryset(self):\n        activate(self.request.GET.get('language'))\n        return tourism_models.InformationDesk.objects.select_related('label_accessibility', 'type').order_by('name')\n\n    @cache_response_detail()\n    def retrieve(self, request, pk=None, format=None):\n        # Allow to retrieve objects even if not visible in list view\n        elem = get_object_or_404(tourism_models.InformationDesk, pk=pk)\n        serializer = api_serializers.InformationDeskSerializer(elem, many=False, context={'request': request})\n        return Response(serializer.data)\n\n\nclass TouristicEventTypeViewSet(api_viewsets.GeotrekViewSet):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (api_filters.TouristicEventRelatedPortalFilter, )\n    serializer_class = api_serializers.TouristicEventTypeSerializer\n    queryset = tourism_models.TouristicEventType.objects.order_by('pk')  # Required for reliable pagination\n\n\nclass TouristicEventViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (\n        api_filters.GeotrekTouristicEventFilter,\n        api_filters.NearbyContentFilter,\n        api_filters.UpdateOrCreateDateFilter,\n    )\n    filterset_class = api_filters.TouristicEventFilterSet\n    serializer_class = api_serializers.TouristicEventSerializer\n\n    def get_queryset(self):\n        activate(self.request.GET.get('language'))\n        return tourism_models.TouristicEvent.objects.existing()\\\n            .select_related('type') \\\n            .prefetch_related('themes', 'source', 'portal',\n                              Prefetch('attachments',\n                                       queryset=Attachment.objects.select_related('license', 'filetype', 'filetype__structure'))\n                              ) \\\n            .annotate(geom_transformed=Transform(F('geom'), settings.API_SRID)) \\\n            .order_by('begin_date')  # Required for reliable pagination\n\n\nclass TouristicEventPlaceViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekGeometricViewset.filter_backends + (\n        api_filters.UpdateOrCreateDateFilter,\n        api_filters.TouristicEventsRelatedPortalFilter\n    )\n    serializer_class = api_serializers.TouristicEventPlaceSerializer\n\n    def get_queryset(self):\n        return tourism_models.TouristicEventPlace.objects.prefetch_related('touristicevents').annotate(\n            geom_transformed=Transform('geom', settings.API_SRID)\n        ).order_by('name')\n\n\nclass TouristicEventOrganizerViewSet(api_viewsets.GeotrekGeometricViewset):\n    filter_backends = api_viewsets.GeotrekViewSet.filter_backends + (\n        api_filters.UpdateOrCreateDateFilter,\n        api_filters.TouristicEventRelatedPortalFilter\n    )\n    serializer_class = api_serializers.TouristicEventOrganizerSerializer\n    queryset = tourism_models.TouristicEventOrganizer.objects.order_by('label')\n", "path": "geotrek/api/v2/views/tourism.py"}]}