6 changed files with 336 additions and 4 deletions
-
91apps/podcast/serializers_dovodi.py
-
6apps/podcast/urls.py
-
61apps/podcast/views.py
-
115apps/video/serializers_dovodi.py
-
6apps/video/urls.py
-
61apps/video/views.py
@ -0,0 +1,91 @@ |
|||
from rest_framework import serializers |
|||
|
|||
from .models import Podcast, PodcastCategory, PodcastCollection |
|||
from .serializers_admin import AbsoluteFileField, AbsoluteImageField |
|||
|
|||
|
|||
class DovodiPodcastCategorySerializer(serializers.ModelSerializer): |
|||
slug = serializers.CharField(required=False, allow_blank=True) |
|||
|
|||
class Meta: |
|||
model = PodcastCategory |
|||
fields = ["id", "title", "slug", "status", "order"] |
|||
|
|||
|
|||
class DovodiPodcastCollectionSerializer(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 = PodcastCollection |
|||
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 DovodiPodcastItemSerializer(serializers.ModelSerializer): |
|||
slug = serializers.CharField(required=False, allow_blank=True) |
|||
thumbnail = AbsoluteImageField(required=False, allow_null=True) |
|||
audio_file = AbsoluteFileField(required=False, allow_null=True) |
|||
remove_thumbnail = serializers.BooleanField(write_only=True, required=False, default=False) |
|||
remove_audio_file = serializers.BooleanField(write_only=True, required=False, default=False) |
|||
|
|||
class Meta: |
|||
model = Podcast |
|||
fields = [ |
|||
"id", |
|||
"title", |
|||
"slug", |
|||
"thumbnail", |
|||
"description", |
|||
"audio_file", |
|||
"audio_time", |
|||
"status", |
|||
"view_count", |
|||
"download_count", |
|||
"created_at", |
|||
"updated_at", |
|||
"remove_thumbnail", |
|||
"remove_audio_file", |
|||
] |
|||
read_only_fields = ["id", "view_count", "download_count", "created_at", "updated_at"] |
|||
|
|||
def create(self, validated_data): |
|||
validated_data.pop("remove_thumbnail", False) |
|||
validated_data.pop("remove_audio_file", False) |
|||
return super().create(validated_data) |
|||
|
|||
def update(self, instance, validated_data): |
|||
remove_thumbnail = validated_data.pop("remove_thumbnail", False) |
|||
remove_audio_file = validated_data.pop("remove_audio_file", False) |
|||
|
|||
if remove_thumbnail and instance.thumbnail: |
|||
instance.thumbnail.delete(save=False) |
|||
instance.thumbnail = None |
|||
|
|||
if remove_audio_file and instance.audio_file: |
|||
instance.audio_file.delete(save=False) |
|||
instance.audio_file = None |
|||
|
|||
return super().update(instance, validated_data) |
|||
@ -0,0 +1,115 @@ |
|||
from rest_framework import serializers |
|||
|
|||
from .models import Video, VideoCategory, VideoCollection |
|||
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) |
|||
|
|||
class Meta: |
|||
model = Video |
|||
fields = [ |
|||
"id", |
|||
"title", |
|||
"slug", |
|||
"thumbnail", |
|||
"description", |
|||
"video_type", |
|||
"video_file", |
|||
"video_url", |
|||
"video_time", |
|||
"status", |
|||
"view_count", |
|||
"created_at", |
|||
"updated_at", |
|||
"remove_thumbnail", |
|||
"remove_video_file", |
|||
] |
|||
read_only_fields = ["id", "view_count", "created_at", "updated_at"] |
|||
|
|||
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): |
|||
validated_data.pop("remove_thumbnail", False) |
|||
validated_data.pop("remove_video_file", False) |
|||
return super().create(validated_data) |
|||
|
|||
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 |
|||
|
|||
return super().update(instance, validated_data) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue