Add: "type" filed from mime type to PostPreviewSerializer

This commit is contained in:
Aroy-Art 2025-02-20 22:20:21 +01:00
parent 23c563b1f2
commit 319bb8e0f5
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -58,7 +58,19 @@ class PostPreviewSerializer(serializers.ModelSerializer):
} }
def get_media(self, obj: PostModel) -> List[Dict[str, str]]: def get_media(self, obj: PostModel) -> List[Dict[str, str]]:
return [{"type": i.mimetype, "src": i.file.url} for i in obj.files.all()] data = []
for i in obj.files.all():
if i.mimetype.startswith("video/"):
f_type = "video"
elif i.mimetype.startswith("image/gif"):
f_type = "gif"
elif i.mimetype.startswith("image/"):
f_type = "image"
else:
f_type = "other"
data.append({"type": f_type, "mimetype": i.mimetype, "src": i.file.url})
return data
def get_tags(self, obj: PostModel) -> List[str]: def get_tags(self, obj: PostModel) -> List[str]:
return [tag.slug for tag in obj.tags.all()] return [tag.slug for tag in obj.tags.all()]