mediacms/files/serializers.py

245 lines
6.7 KiB
Python
Raw Normal View History

2020-12-15 22:33:43 +01:00
from rest_framework import serializers
2021-05-26 17:35:21 +02:00
from .models import Category, Comment, EncodeProfile, Media, Playlist, Tag
2020-12-15 22:33:43 +01:00
# TODO: put them in a more DRY way
class MediaSerializer(serializers.ModelSerializer):
# to be used in APIs as show related media
user = serializers.ReadOnlyField(source="user.username")
url = serializers.SerializerMethodField()
api_url = serializers.SerializerMethodField()
thumbnail_url = serializers.SerializerMethodField()
author_profile = serializers.SerializerMethodField()
author_thumbnail = serializers.SerializerMethodField()
def get_url(self, obj):
return self.context["request"].build_absolute_uri(obj.get_absolute_url())
def get_api_url(self, obj):
2021-05-26 17:35:21 +02:00
return self.context["request"].build_absolute_uri(obj.get_absolute_url(api=True))
2020-12-15 22:33:43 +01:00
def get_thumbnail_url(self, obj):
2021-01-07 14:33:02 +01:00
if obj.thumbnail_url:
return self.context["request"].build_absolute_uri(obj.thumbnail_url)
else:
return None
2020-12-15 22:33:43 +01:00
def get_author_profile(self, obj):
return self.context["request"].build_absolute_uri(obj.author_profile())
def get_author_thumbnail(self, obj):
return self.context["request"].build_absolute_uri(obj.author_thumbnail())
class Meta:
model = Media
read_only_fields = (
"friendly_token",
"user",
"add_date",
"media_type",
"state",
"duration",
"encoding_status",
"views",
"likes",
"dislikes",
"reported_times",
"size",
"is_reviewed",
2021-05-31 21:24:05 +02:00
"featured",
2020-12-15 22:33:43 +01:00
)
fields = (
"friendly_token",
"url",
"api_url",
"user",
"title",
"description",
"add_date",
"views",
"media_type",
"state",
"duration",
"thumbnail_url",
"is_reviewed",
"preview_url",
"author_name",
"author_profile",
"author_thumbnail",
"encoding_status",
"views",
"likes",
"dislikes",
"reported_times",
"featured",
"user_featured",
"size",
)
class SingleMediaSerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source="user.username")
url = serializers.SerializerMethodField()
def get_url(self, obj):
return self.context["request"].build_absolute_uri(obj.get_absolute_url())
class Meta:
model = Media
read_only_fields = (
"friendly_token",
"user",
"add_date",
"views",
"media_type",
"state",
"duration",
"encoding_status",
"views",
"likes",
"dislikes",
"reported_times",
"size",
"video_height",
"is_reviewed",
)
fields = (
"url",
"user",
"title",
"description",
"add_date",
"edit_date",
"media_type",
"state",
"duration",
"thumbnail_url",
"poster_url",
"thumbnail_time",
"url",
"sprites_url",
"preview_url",
"author_name",
"author_profile",
"author_thumbnail",
"encodings_info",
"encoding_status",
"views",
"likes",
"dislikes",
"reported_times",
"user_featured",
"original_media_url",
"size",
"video_height",
"enable_comments",
"categories_info",
"is_reviewed",
"edit_url",
"tags_info",
"hls_info",
"license",
"subtitles_info",
"ratings_info",
"add_subtitle_url",
"allow_download",
"slideshow_items"
2020-12-15 22:33:43 +01:00
)
class MediaSearchSerializer(serializers.ModelSerializer):
url = serializers.SerializerMethodField()
api_url = serializers.SerializerMethodField()
2023-06-12 16:22:39 +02:00
2020-12-15 22:33:43 +01:00
def get_url(self, obj):
return self.context["request"].build_absolute_uri(obj.get_absolute_url())
2023-06-12 16:13:44 +02:00
def get_api_url(self, obj):
return self.context["request"].build_absolute_uri(obj.get_absolute_url(api=True))
2020-12-15 22:33:43 +01:00
class Meta:
model = Media
fields = (
"title",
"author_name",
"author_profile",
"thumbnail_url",
"add_date",
"views",
"description",
"friendly_token",
"duration",
"url",
2023-06-12 16:13:44 +02:00
"api_url",
2020-12-15 22:33:43 +01:00
"media_type",
"preview_url",
"categories_info",
)
class EncodeProfileSerializer(serializers.ModelSerializer):
class Meta:
model = EncodeProfile
fields = ("name", "extension", "resolution", "codec", "description")
class CategorySerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source="user.username")
class Meta:
model = Category
fields = (
"title",
"description",
"is_global",
"media_count",
"user",
"thumbnail_url",
)
class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tag
fields = ("title", "media_count", "thumbnail_url")
class PlaylistSerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source="user.username")
class Meta:
model = Playlist
read_only_fields = ("add_date", "user")
2021-05-26 17:35:21 +02:00
fields = ("add_date", "title", "description", "user", "media_count", "url", "api_url", "thumbnail_url")
2020-12-15 22:33:43 +01:00
class PlaylistDetailSerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source="user.username")
class Meta:
model = Playlist
read_only_fields = ("add_date", "user")
2021-05-26 17:35:21 +02:00
fields = ("title", "add_date", "user_thumbnail_url", "description", "user", "media_count", "url", "thumbnail_url")
2020-12-15 22:33:43 +01:00
class CommentSerializer(serializers.ModelSerializer):
author_profile = serializers.ReadOnlyField(source="user.get_absolute_url")
author_name = serializers.ReadOnlyField(source="user.name")
author_thumbnail_url = serializers.ReadOnlyField(source="user.thumbnail_url")
class Meta:
model = Comment
read_only_fields = ("add_date", "uid")
fields = (
"add_date",
"text",
"parent",
"author_thumbnail_url",
"author_profile",
"author_name",
"media_url",
"uid",
)