You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
5.6 KiB
145 lines
5.6 KiB
from rest_framework import serializers
|
|
|
|
from .models import Video, VideoCategory, VideoCollection, VideoPlaylist, PlaylistItem
|
|
from .serializers_admin import AbsoluteFileField, AbsoluteImageField
|
|
|
|
|
|
class DovodiVideoCategorySerializer(serializers.ModelSerializer):
|
|
slug = serializers.CharField(required=False, allow_blank=True)
|
|
|
|
class Meta:
|
|
model = VideoCategory
|
|
fields = ["id", "title", "slug", "status", "order"]
|
|
|
|
|
|
class DovodiVideoCollectionSerializer(serializers.ModelSerializer):
|
|
slug = serializers.CharField(required=False, allow_blank=True)
|
|
thumbnail = AbsoluteImageField(required=False, allow_null=True)
|
|
remove_thumbnail = serializers.BooleanField(write_only=True, required=False, default=False)
|
|
|
|
class Meta:
|
|
model = VideoCollection
|
|
fields = [
|
|
"id",
|
|
"title",
|
|
"slug",
|
|
"summary",
|
|
"display_position",
|
|
"status",
|
|
"order",
|
|
"pin_top",
|
|
"thumbnail",
|
|
"remove_thumbnail",
|
|
]
|
|
|
|
def create(self, validated_data):
|
|
validated_data.pop("remove_thumbnail", False)
|
|
return super().create(validated_data)
|
|
|
|
def update(self, instance, validated_data):
|
|
remove_thumbnail = validated_data.pop("remove_thumbnail", False)
|
|
if remove_thumbnail and instance.thumbnail:
|
|
instance.thumbnail.delete(save=False)
|
|
instance.thumbnail = None
|
|
return super().update(instance, validated_data)
|
|
|
|
|
|
class DovodiVideoItemSerializer(serializers.ModelSerializer):
|
|
slug = serializers.CharField(required=False, allow_blank=True)
|
|
thumbnail = AbsoluteImageField(required=False, allow_null=True)
|
|
video_file = AbsoluteFileField(required=False, allow_null=True)
|
|
remove_thumbnail = serializers.BooleanField(write_only=True, required=False, default=False)
|
|
remove_video_file = serializers.BooleanField(write_only=True, required=False, default=False)
|
|
playlist = serializers.IntegerField(required=False, allow_null=True, write_only=True)
|
|
playlist_id = serializers.SerializerMethodField(read_only=True)
|
|
|
|
class Meta:
|
|
model = Video
|
|
fields = [
|
|
"id",
|
|
"title",
|
|
"slug",
|
|
"thumbnail",
|
|
"description",
|
|
"video_type",
|
|
"video_file",
|
|
"video_url",
|
|
"video_time",
|
|
"status",
|
|
"view_count",
|
|
"playlist",
|
|
"playlist_id",
|
|
"created_at",
|
|
"updated_at",
|
|
"remove_thumbnail",
|
|
"remove_video_file",
|
|
]
|
|
read_only_fields = ["id", "view_count", "playlist_id", "created_at", "updated_at"]
|
|
|
|
def get_playlist_id(self, obj):
|
|
appearance = obj.playlist_appearances.first()
|
|
return appearance.playlist_id if appearance else None
|
|
|
|
def validate(self, attrs):
|
|
video_type = attrs.get("video_type", getattr(self.instance, "video_type", None))
|
|
video_url = attrs.get("video_url", getattr(self.instance, "video_url", None))
|
|
video_file = attrs.get("video_file", getattr(self.instance, "video_file", None))
|
|
remove_video_file = attrs.get("remove_video_file", False)
|
|
|
|
if video_type == Video.VedioTypeChoices.YOUTUBE_LINK and not video_url:
|
|
raise serializers.ValidationError({
|
|
"video_url": "This field is required when video type is link.",
|
|
})
|
|
|
|
if video_type == Video.VedioTypeChoices.VIDEO_FILE and not video_file and not getattr(self.instance, "video_file", None):
|
|
raise serializers.ValidationError({
|
|
"video_file": "This field is required when video type is file.",
|
|
})
|
|
|
|
if video_type == Video.VedioTypeChoices.VIDEO_FILE and remove_video_file and "video_file" not in attrs:
|
|
raise serializers.ValidationError({
|
|
"video_file": "Please upload a new file before removing the current one.",
|
|
})
|
|
|
|
return attrs
|
|
|
|
def create(self, validated_data):
|
|
playlist_id = validated_data.pop("playlist", None)
|
|
validated_data.pop("remove_thumbnail", False)
|
|
validated_data.pop("remove_video_file", False)
|
|
instance = super().create(validated_data)
|
|
if playlist_id:
|
|
try:
|
|
playlist = VideoPlaylist.objects.get(id=playlist_id)
|
|
PlaylistItem.objects.create(playlist=playlist, video=instance, priority=playlist.playlist_items.count() + 1)
|
|
except VideoPlaylist.DoesNotExist:
|
|
pass
|
|
return instance
|
|
|
|
def update(self, instance, validated_data):
|
|
remove_thumbnail = validated_data.pop("remove_thumbnail", False)
|
|
remove_video_file = validated_data.pop("remove_video_file", False)
|
|
|
|
if remove_thumbnail and instance.thumbnail:
|
|
instance.thumbnail.delete(save=False)
|
|
instance.thumbnail = None
|
|
|
|
if remove_video_file and instance.video_file:
|
|
instance.video_file.delete(save=False)
|
|
instance.video_file = None
|
|
|
|
playlist_in_payload = "playlist" in self.initial_data
|
|
playlist_id = validated_data.pop("playlist", None)
|
|
|
|
instance = super().update(instance, validated_data)
|
|
|
|
if playlist_in_payload:
|
|
instance.playlist_appearances.all().delete()
|
|
if playlist_id:
|
|
try:
|
|
playlist = VideoPlaylist.objects.get(id=playlist_id)
|
|
PlaylistItem.objects.create(playlist=playlist, video=instance, priority=playlist.playlist_items.count() + 1)
|
|
except VideoPlaylist.DoesNotExist:
|
|
pass
|
|
|
|
return instance
|