Verified Commit ad9a0030 authored by Agate's avatar Agate 💬

Implemented AP-to-HTML redirections for tracks, albums, artists and uploads

parent 4f06a433
Pipeline #9908 passed with stages
in 4 minutes and 44 seconds
......@@ -232,7 +232,6 @@ class MusicLibraryViewSet(
def retrieve(self, request, *args, **kwargs):
lb = self.get_object()
if utils.should_redirect_ap_to_html(request.headers.get("accept")):
# XXX: implement this for albums, tracks, artists
return redirect_to_html(lb.get_absolute_url())
conf = {
"id": lb.get_federation_id(),
......@@ -318,6 +317,14 @@ class MusicUploadViewSet(
serializer_class = serializers.UploadSerializer
lookup_field = "uuid"
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
if utils.should_redirect_ap_to_html(request.headers.get("accept")):
return redirect_to_html(instance.track.get_absolute_url())
serializer = self.get_serializer(instance)
return response.Response(serializer.data)
def get_queryset(self):
queryset = super().get_queryset()
actor = music_utils.get_actor_from_request(self.request)
......@@ -340,6 +347,14 @@ class MusicArtistViewSet(
serializer_class = serializers.ArtistSerializer
lookup_field = "uuid"
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
if utils.should_redirect_ap_to_html(request.headers.get("accept")):
return redirect_to_html(instance.get_absolute_url())
serializer = self.get_serializer(instance)
return response.Response(serializer.data)
class MusicAlbumViewSet(
FederationMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
......@@ -352,6 +367,14 @@ class MusicAlbumViewSet(
serializer_class = serializers.AlbumSerializer
lookup_field = "uuid"
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
if utils.should_redirect_ap_to_html(request.headers.get("accept")):
return redirect_to_html(instance.get_absolute_url())
serializer = self.get_serializer(instance)
return response.Response(serializer.data)
class MusicTrackViewSet(
FederationMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
......@@ -370,3 +393,11 @@ class MusicTrackViewSet(
)
serializer_class = serializers.TrackSerializer
lookup_field = "uuid"
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
if utils.should_redirect_ap_to_html(request.headers.get("accept")):
return redirect_to_html(instance.get_absolute_url())
serializer = self.get_serializer(instance)
return response.Response(serializer.data)
......@@ -427,3 +427,63 @@ def test_channel_actor_retrieve_redirects_to_html_if_header_set(
)
assert response.status_code == 302
assert response["Location"] == expected_url
def test_upload_retrieve_redirects_to_html_if_header_set(
factories, api_client, settings
):
upload = factories["music.Upload"](library__local=True, playable=True)
url = reverse("federation:music:uploads-detail", kwargs={"uuid": upload.uuid},)
response = api_client.get(url, HTTP_ACCEPT="text/html")
expected_url = utils.join_url(
settings.FUNKWHALE_URL,
utils.spa_reverse("library_track", kwargs={"pk": upload.track.pk}),
)
assert response.status_code == 302
assert response["Location"] == expected_url
def test_track_retrieve_redirects_to_html_if_header_set(
factories, api_client, settings
):
track = factories["music.Track"](local=True)
url = reverse("federation:music:tracks-detail", kwargs={"uuid": track.uuid},)
response = api_client.get(url, HTTP_ACCEPT="text/html")
expected_url = utils.join_url(
settings.FUNKWHALE_URL,
utils.spa_reverse("library_track", kwargs={"pk": track.pk}),
)
assert response.status_code == 302
assert response["Location"] == expected_url
def test_album_retrieve_redirects_to_html_if_header_set(
factories, api_client, settings
):
album = factories["music.Album"](local=True)
url = reverse("federation:music:albums-detail", kwargs={"uuid": album.uuid},)
response = api_client.get(url, HTTP_ACCEPT="text/html")
expected_url = utils.join_url(
settings.FUNKWHALE_URL,
utils.spa_reverse("library_album", kwargs={"pk": album.pk}),
)
assert response.status_code == 302
assert response["Location"] == expected_url
def test_artist_retrieve_redirects_to_html_if_header_set(
factories, api_client, settings
):
artist = factories["music.Artist"](local=True)
url = reverse("federation:music:artists-detail", kwargs={"uuid": artist.uuid},)
response = api_client.get(url, HTTP_ACCEPT="text/html")
expected_url = utils.join_url(
settings.FUNKWHALE_URL,
utils.spa_reverse("library_artist", kwargs={"pk": artist.pk}),
)
assert response.status_code == 302
assert response["Location"] == expected_url
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment