Repository: city-of-helsinki/linkedevents
Issue: Do not list deleted events in inline event super and sub event listings

Currently, some weird interplay between `queryset=Event.objects.filter(deleted=False)` and django-mptt results in also deleted objects being listed in event `sub_events` field of the API.

So, these tests should pass, but don't. Add tests and investigate.

```
@pytest.mark.django_db
def test_get_event_list_sub_events(api_client, event, event2):
    event.super_event_type = Event.SuperEventType.RECURRING
    event.save()
    event2.super_event = event
    event2.save()

    # fetch event with sub event
    detail_url = reverse('event-detail', version='v1', kwargs={'pk': event.pk})
    response = get(api_client, detail_url)
    assert_event_fields_exist(response.data)
    assert response.data['sub_events']
    print(response.data['sub_events'])


@pytest.mark.django_db
def test_get_event_list_deleted_sub_events(api_client, event, event2):
    event.super_event_type = Event.SuperEventType.RECURRING
    event.save()
    event2.super_event = event
    event2.deleted = True
    event2.save()

    # fetch event with sub event deleted
    detail_url = reverse('event-detail', version='v1', kwargs={'pk': event.pk})
    response = get(api_client, detail_url)
    assert_event_fields_exist(response.data)
    assert not response.data['sub_events']
    print(response.data['sub_events'])

```

Changed files:
events/tests/test_event_get.py, events/api.py

diff --git a/events/api.py b/events/api.py
index 9d2c9df..47f3a78 100644
--- a/events/api.py
+++ b/events/api.py
@@ -1487,6 +1487,14 @@ class EventSerializer(BulkSerializerMixin, LinkedEventsSerializer, GeoModelAPIVi
         if request:
             if not request.user.is_authenticated:
                 del ret['publication_status']
+
+        if ret['sub_events']:
+            sub_events_relation = self.fields['sub_events'].child_relation
+            undeleted_sub_events = []
+            for sub_event in obj.sub_events.filter(deleted=False):
+                undeleted_sub_events.append(sub_events_relation.to_representation(sub_event))
+            ret['sub_events'] = undeleted_sub_events
+
         return ret
 
     class Meta:
diff --git a/events/tests/test_event_get.py b/events/tests/test_event_get.py
index 23d1be2..e1f72e9 100644
--- a/events/tests/test_event_get.py
+++ b/events/tests/test_event_get.py
@@ -486,6 +486,35 @@ def test_redirect_to_end_of_replace_chain(api_client, event, event2, event3, use
     assert response2.data['id'] == event3.pk
 
 
+@pytest.mark.django_db
+def test_get_event_list_sub_events(api_client, event, event2):
+    event.super_event_type = Event.SuperEventType.RECURRING
+    event.save()
+    event2.super_event = event
+    event2.save()
+
+    # fetch event with sub event
+    detail_url = reverse('event-detail', version='v1', kwargs={'pk': event.pk})
+    response = get(api_client, detail_url)
+    assert_event_fields_exist(response.data)
+    assert response.data['sub_events']
+
+
+@pytest.mark.django_db
+def test_get_event_list_deleted_sub_events(api_client, event, event2):
+    event.super_event_type = Event.SuperEventType.RECURRING
+    event.save()
+    event2.super_event = event
+    event2.deleted = True
+    event2.save()
+
+    # fetch event with sub event deleted
+    detail_url = reverse('event-detail', version='v1', kwargs={'pk': event.pk})
+    response = get(api_client, detail_url)
+    assert_event_fields_exist(response.data)
+    assert not response.data['sub_events']
+
+
 @pytest.mark.django_db
 def test_event_list_show_deleted_param(api_client, event, event2, user):
     api_client.force_authenticate(user=user)