We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Expand/improve document part of event endpoint
<!--

Please add the appropriate label for what change should be made:
docs: changes to the documentation)
refactor: refactoring production code, eg. renaming a variable or rewriting a function
test: adding missing tests, refactoring tests; no production code change
chore: updating poetry etc; no production code change

-->

### Describe the change
Change the current URL or add a URL to a link to the actual document (that is, one that ends with `/[name].pdf`).

### Motivation
It's very nice for the app to be able to just fetch files directly.
Also, I don't think the current URL is even necessary because you also have the PK with which you can create the URL.

### Current implementation
There is a URL that leads to the file, but only after a redirect to `cdn`.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `website/documents/api/v2/serializers/document.py`
Content:
```
1 from rest_framework.fields import SerializerMethodField
2 from rest_framework.reverse import reverse
3 
4 from documents.models import Document
5 from thaliawebsite.api.v2.serializers.cleaned_model_serializer import (
6     CleanedModelSerializer,
7 )
8 
9 
10 class DocumentSerializer(CleanedModelSerializer):
11     class Meta:
12         model = Document
13         fields = ("pk", "name", "url", "category", "members_only")
14 
15     url = SerializerMethodField("_url")
16 
17     def _url(self, instance):
18         return self.context["request"].build_absolute_uri(
19             reverse("documents:document", kwargs={"pk": instance.pk})
20         )
21
```
--- 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/website/documents/api/v2/serializers/document.py b/website/documents/api/v2/serializers/document.py
--- a/website/documents/api/v2/serializers/document.py
+++ b/website/documents/api/v2/serializers/document.py
@@ -1,10 +1,10 @@
 from rest_framework.fields import SerializerMethodField
-from rest_framework.reverse import reverse
 
 from documents.models import Document
 from thaliawebsite.api.v2.serializers.cleaned_model_serializer import (
     CleanedModelSerializer,
 )
+from utils.media.services import get_media_url
 
 
 class DocumentSerializer(CleanedModelSerializer):
@@ -15,6 +15,12 @@
     url = SerializerMethodField("_url")
 
     def _url(self, instance):
-        return self.context["request"].build_absolute_uri(
-            reverse("documents:document", kwargs={"pk": instance.pk})
-        )
+        if instance.members_only and (
+            not self.context["request"].user.is_authenticated
+            or not self.context["request"].member.has_active_membership()
+        ):
+            return self.context["request"].build_absolute_uri(
+                instance.get_absolute_url()
+            )
+
+        return get_media_url(instance.file, absolute_url=True)

{"golden_diff": "diff --git a/website/documents/api/v2/serializers/document.py b/website/documents/api/v2/serializers/document.py\n--- a/website/documents/api/v2/serializers/document.py\n+++ b/website/documents/api/v2/serializers/document.py\n@@ -1,10 +1,10 @@\n from rest_framework.fields import SerializerMethodField\n-from rest_framework.reverse import reverse\n \n from documents.models import Document\n from thaliawebsite.api.v2.serializers.cleaned_model_serializer import (\n     CleanedModelSerializer,\n )\n+from utils.media.services import get_media_url\n \n \n class DocumentSerializer(CleanedModelSerializer):\n@@ -15,6 +15,12 @@\n     url = SerializerMethodField(\"_url\")\n \n     def _url(self, instance):\n-        return self.context[\"request\"].build_absolute_uri(\n-            reverse(\"documents:document\", kwargs={\"pk\": instance.pk})\n-        )\n+        if instance.members_only and (\n+            not self.context[\"request\"].user.is_authenticated\n+            or not self.context[\"request\"].member.has_active_membership()\n+        ):\n+            return self.context[\"request\"].build_absolute_uri(\n+                instance.get_absolute_url()\n+            )\n+\n+        return get_media_url(instance.file, absolute_url=True)\n", "issue": "Expand/improve document part of event endpoint\n<!--\r\n\r\nPlease add the appropriate label for what change should be made:\r\ndocs: changes to the documentation)\r\nrefactor: refactoring production code, eg. renaming a variable or rewriting a function\r\ntest: adding missing tests, refactoring tests; no production code change\r\nchore: updating poetry etc; no production code change\r\n\r\n-->\r\n\r\n### Describe the change\r\nChange the current URL or add a URL to a link to the actual document (that is, one that ends with `/[name].pdf`).\r\n\r\n### Motivation\r\nIt's very nice for the app to be able to just fetch files directly.\r\nAlso, I don't think the current URL is even necessary because you also have the PK with which you can create the URL.\r\n\r\n### Current implementation\r\nThere is a URL that leads to the file, but only after a redirect to `cdn`.\n", "before_files": [{"content": "from rest_framework.fields import SerializerMethodField\nfrom rest_framework.reverse import reverse\n\nfrom documents.models import Document\nfrom thaliawebsite.api.v2.serializers.cleaned_model_serializer import (\n    CleanedModelSerializer,\n)\n\n\nclass DocumentSerializer(CleanedModelSerializer):\n    class Meta:\n        model = Document\n        fields = (\"pk\", \"name\", \"url\", \"category\", \"members_only\")\n\n    url = SerializerMethodField(\"_url\")\n\n    def _url(self, instance):\n        return self.context[\"request\"].build_absolute_uri(\n            reverse(\"documents:document\", kwargs={\"pk\": instance.pk})\n        )\n", "path": "website/documents/api/v2/serializers/document.py"}], "after_files": [{"content": "from rest_framework.fields import SerializerMethodField\n\nfrom documents.models import Document\nfrom thaliawebsite.api.v2.serializers.cleaned_model_serializer import (\n    CleanedModelSerializer,\n)\nfrom utils.media.services import get_media_url\n\n\nclass DocumentSerializer(CleanedModelSerializer):\n    class Meta:\n        model = Document\n        fields = (\"pk\", \"name\", \"url\", \"category\", \"members_only\")\n\n    url = SerializerMethodField(\"_url\")\n\n    def _url(self, instance):\n        if instance.members_only and (\n            not self.context[\"request\"].user.is_authenticated\n            or not self.context[\"request\"].member.has_active_membership()\n        ):\n            return self.context[\"request\"].build_absolute_uri(\n                instance.get_absolute_url()\n            )\n\n        return get_media_url(instance.file, absolute_url=True)\n", "path": "website/documents/api/v2/serializers/document.py"}]}