Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Use HyperlinkedIdentityField to automatically generate related URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Sep 1, 2021
1 parent ceafca2 commit 9d120d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
33 changes: 13 additions & 20 deletions openverse-api/catalog/api/serializers/audio_serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.urls import reverse
from rest_framework import serializers

from catalog.api.controllers.search_controller import get_sources
Expand Down Expand Up @@ -96,12 +95,6 @@ class AudioSerializer(MediaSerializer):
used to generate Swagger documentation.
"""

thumbnail = serializers.SerializerMethodField(
help_text="A direct link to the miniature artwork."
)
waveform = serializers.SerializerMethodField(
help_text='A direct link to the waveform peaks.'
)
audio_set = serializers.PrimaryKeyRelatedField(
required=False,
help_text='Reference to set of which this track is a part.',
Expand Down Expand Up @@ -135,31 +128,31 @@ class AudioSerializer(MediaSerializer):
)

# Hyperlinks
thumbnail = serializers.HyperlinkedIdentityField(
read_only=True,
view_name='audio-thumb',
lookup_field='identifier',
help_text="A direct link to the miniature artwork."
)
waveform = serializers.HyperlinkedIdentityField(
read_only=True,
view_name='audio-waveform',
lookup_field='identifier',
help_text='A direct link to the waveform peaks.'
)
detail_url = serializers.HyperlinkedIdentityField(
read_only=True,
view_name='audio-detail',
lookup_field='identifier',
help_text="A direct link to the detail view of this audio file."
)
related_url = serializers.HyperlinkedIdentityField(
read_only=True,
view_name='audio-related',
lookup_field='identifier',
read_only=True,
help_text="A link to an endpoint that provides similar audio files."
)

def get_thumbnail(self, obj):
request = self.context['request']
host = request.get_host()
path = reverse('audio-thumb', kwargs={'identifier': obj.identifier})
return f'https://{host}{path}'

def get_waveform(self, obj):
request = self.context['request']
host = request.get_host()
path = reverse('audio-waveform', kwargs={'identifier': obj.identifier})
return f'https://{host}{path}'


class AudioSearchSerializer(MediaSearchSerializer):
"""
Expand Down
15 changes: 6 additions & 9 deletions openverse-api/catalog/api/serializers/image_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ class ImageSerializer(MediaSerializer):
used to generate Swagger documentation.
"""

thumbnail = serializers.SerializerMethodField(
help_text="A direct link to the miniature image."
)
height = serializers.IntegerField(
required=False,
help_text="The height of the image in pixels. Not always available."
Expand All @@ -110,6 +107,12 @@ class ImageSerializer(MediaSerializer):
)

# Hyperlinks
thumbnail = serializers.HyperlinkedIdentityField(
read_only=True,
view_name='image-thumb',
lookup_field='identifier',
help_text="A direct link to the miniature image."
)
detail_url = serializers.HyperlinkedIdentityField(
read_only=True,
view_name='image-detail',
Expand All @@ -123,12 +126,6 @@ class ImageSerializer(MediaSerializer):
help_text="A link to an endpoint that provides similar images."
)

def get_thumbnail(self, obj):
request = self.context['request']
host = request.get_host()
path = reverse('image-thumb', kwargs={'identifier': obj.identifier})
return f'https://{host}{path}'


class ImageSearchSerializer(MediaSearchSerializer):
"""
Expand Down

0 comments on commit 9d120d9

Please sign in to comment.