Skip to content

Commit 8989ecd

Browse files
committed
make media URL relative and un nest serialisers
1 parent 37b9748 commit 8989ecd

3 files changed

Lines changed: 13 additions & 28 deletions

File tree

server/api/settings.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@
148148
# The directory to store images and other media
149149
MEDIA_ROOT = BASE_DIR/"media"
150150

151-
# The url to serve images and other media
152-
if DEBUG:
153-
MEDIA_URL = "http://localhost:8000/media/"
154-
else:
155-
MEDIA_URL = f"https://{ALLOWED_HOSTS[0]}/media/"
151+
MEDIA_URL = "/media/"
156152

157153
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
158154

server/game_dev/serializers.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,7 @@ def get_contributors(self, obj):
100100
return ShowcaseContributorSerializer(contributors, many=True).data
101101

102102

103-
class ContributorGameDataSerializer(serializers.ModelSerializer):
104-
# Serializes data in Game model to display on a contributor's profile.
105-
106-
class Meta:
107-
model = Game
108-
fields = ('name', 'thumbnail',
109-
'description')
110-
111-
112103
class ContributorGameSerializer(serializers.ModelSerializer):
113-
# Matches games in the GameContributor model to the information about them in the Game model.
114104
game_id = serializers.IntegerField(source='game.id', read_only=True)
115105
role = serializers.CharField(read_only=True)
116106
game_data = serializers.SerializerMethodField()
@@ -120,8 +110,13 @@ class Meta:
120110
fields = ['game_id', 'role', 'game_data']
121111

122112
def get_game_data(self, obj):
123-
game_data = Game.objects.get(id=obj.game_id)
124-
return ContributorGameDataSerializer(game_data).data
113+
game = obj.game
114+
request = self.context.get('request')
115+
return {
116+
'name': game.name,
117+
'description': game.description,
118+
'thumbnail': request.build_absolute_uri(game.thumbnail.url) if game.thumbnail and request else None
119+
}
125120

126121

127122
class SocialMediaSerializer(serializers.ModelSerializer):

server/game_dev/views.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,12 @@ def get(self, request):
7070
return Response(serializer.data)
7171

7272

73-
class ContributorGamesListAPIView(APIView):
74-
"""
75-
GET /api/games/contributor/<member>/
76-
Returns the games a particular member has contributed to.
77-
"""
78-
lookup_url_kwarg = "member"
73+
class ContributorGamesListAPIView(generics.ListAPIView):
74+
serializer_class = ContributorGameSerializer
7975

80-
def get(self, request, member):
81-
contributions = GameContributor.objects.filter(
82-
member=self.kwargs["member"])
83-
serializer = ContributorGameSerializer(contributions, many=True)
84-
return Response(serializer.data)
76+
def get_queryset(self):
77+
member_id = self.kwargs.get("member")
78+
return GameContributor.objects.filter(member=member_id)
8579

8680

8781
class MemberAPIView(generics.RetrieveAPIView):

0 commit comments

Comments
 (0)