Commit 5ee715e3 authored by Ilya Simonov's avatar Ilya Simonov

change playlist video from MTM to new model

parent 3c3c8c4f
......@@ -42,6 +42,7 @@ class VideoAdmin(admin.ModelAdmin):
]
list_display = ['id', 'title', 'slug', 'status', 'duration']
readonly_fields = ['s3_file', 'created', 'updated']
search_fields = ['title', 'slug']
list_filter = ('status',)
inlines = [TagInline]
......@@ -50,17 +51,25 @@ class VideoAdmin(admin.ModelAdmin):
super().save_model(request, obj, form, change)
class PlaylistVideoInline(admin.TabularInline):
model = models.PlaylistVideo
autocomplete_fields = ['video']
extra = 0
verbose_name = 'Video'
verbose_name_plural = 'Video'
fields = ('video',)
class PlayListAdmin(admin.ModelAdmin):
form = forms.PlaylistForm
list_display = ['id', 'title', 'type']
readonly_fields = ['created', 'updated']
list_filter = ('type', 'sort')
inlines = [TagInline]
inlines = [TagInline, PlaylistVideoInline]
def get_inlines(self, request, obj):
if obj and obj.type == models.DYNAMIC:
return [TagInline]
return []
return [TagInline, PlaylistVideoInline]
return [PlaylistVideoInline]
class CategoryAdmin(admin.ModelAdmin):
......
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.forms import ModelForm
from .models import Video, Playlist
from .models import Video
class VideoForm(ModelForm):
......@@ -11,12 +11,3 @@ class VideoForm(ModelForm):
widgets = {
'categories': FilteredSelectMultiple('Verbose name', False),
}
class PlaylistForm(ModelForm):
class Meta:
model = Playlist
fields = '__all__'
widgets = {
'videos': FilteredSelectMultiple('Verbose name', False),
}
# Generated by Django 4.1.7 on 2023-03-28 13:56
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('core', '0003_alter_playlist_videos_alter_video_local_file'),
]
operations = [
migrations.CreateModel(
name='PlaylistVideo',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('updated', models.DateTimeField(auto_now=True, db_index=True, verbose_name='updated')),
('created', models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='created')),
('playlist', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='playlist_video', to='core.playlist', verbose_name='playlist video')),
('video', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='video_playlist', to='core.video', verbose_name='video playlist')),
],
options={
'verbose_name': 'Playlist video',
'verbose_name_plural': 'Playlists video',
},
),
]
# Generated by Django 4.1.7 on 2023-03-28 14:20
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('core', '0004_playlistvideo'),
]
operations = [
migrations.RemoveField(
model_name='playlist',
name='videos',
),
]
......@@ -165,7 +165,6 @@ class Video(BaseModel):
class Playlist(BaseModel):
title = models.CharField(_('title'), max_length=255)
description = models.TextField(_('description'), blank=True, null=True)
videos = models.ManyToManyField(Video, related_name='playlist', blank=True)
sort = models.CharField(choices=SORT_CHOICES, max_length=255, blank=True)
type = models.CharField(choices=TYPE_CHOICES, max_length=255, blank=True)
mrss = models.CharField(_('mrss'), max_length=255, blank=True, null=True)
......@@ -179,3 +178,25 @@ class Playlist(BaseModel):
def __str__(self):
return self.title
class PlaylistVideo(BaseModel):
playlist = models.ForeignKey(
Playlist,
on_delete=models.CASCADE,
related_name='playlist_video',
verbose_name='playlist video',
)
video = models.ForeignKey(
Video,
on_delete=models.CASCADE,
related_name='video_playlist',
verbose_name='video playlist',
)
class Meta:
verbose_name = 'Playlist video'
verbose_name_plural = 'Playlists video'
def __str__(self):
return str(self.id)
from rest_framework import serializers
from .models import Video, Playlist, Category, TagToObject
from .models import Video, Playlist, Category, TagToObject, PlaylistVideo
class TagToObjectSerializer(serializers.ModelSerializer):
......@@ -36,8 +36,32 @@ class VideoSerializer(serializers.ModelSerializer):
]
class PlaylistVideoSerializer(serializers.ModelSerializer):
id = serializers.CharField(source='video.id')
title = serializers.CharField(source='video.title')
slug = serializers.CharField(source='video.slug')
description = serializers.CharField(source='video.description')
s3_file = serializers.CharField(source='video.s3_file')
thumbnail = serializers.CharField(source='video.thumbnail')
categories = CategorySerializer(source='video.categories', many=True)
tags = TagToObjectSerializer(source='video.tags', many=True)
class Meta:
model = PlaylistVideo
fields = [
'id',
'title',
'slug',
'description',
's3_file',
'thumbnail',
'categories',
'tags',
]
class PlaylistSerializer(serializers.ModelSerializer):
videos = VideoSerializer(many=True)
video = PlaylistVideoSerializer(source='playlist_video', many=True)
tags = TagToObjectSerializer(many=True)
class Meta:
......@@ -50,6 +74,6 @@ class PlaylistSerializer(serializers.ModelSerializer):
'type',
'mrss',
'json',
'videos',
'video',
'tags',
]
......@@ -5,6 +5,6 @@ from . import views
urlpatterns = [
path('video/', views.VideoAPIView.as_view({'get': 'list'})),
path('playlist/<int:pk>/', views.VideoRetrieveAPIView.as_view()),
path('playlist/<int:pk>/', views.PlaylistRetrieveAPIView.as_view()),
path('playlist/<int:pk>/mrss', views.playlist_mrss_view),
]
......@@ -3,7 +3,7 @@ from django.contrib.contenttypes.models import ContentType
from moviepy.editor import VideoFileClip
from .models import Video, Playlist, TagToObject, DYNAMIC
from .models import Video, Playlist, TagToObject, DYNAMIC, PlaylistVideo
def generate_thumbnail(video_path, video_name):
......@@ -36,7 +36,10 @@ def add_video_to_playlist(video):
playlists = Playlist.objects.filter(id__in=playlist_ids, type=DYNAMIC)
for playlist in playlists:
playlist.videos.add(video)
PlaylistVideo.objects.update_or_create(
playlist=playlist,
video=video,
)
def add_video_to_new_playlist(playlist):
......@@ -56,4 +59,7 @@ def add_video_to_new_playlist(playlist):
if video_ids:
for video in Video.objects.filter(id__in=video_ids):
playlist.videos.add(video)
PlaylistVideo.objects.update_or_create(
playlist=playlist,
video=video,
)
......@@ -22,7 +22,7 @@ class VideoAPIView(viewsets.ModelViewSet):
return queryset
class VideoRetrieveAPIView(generics.RetrieveAPIView):
class PlaylistRetrieveAPIView(generics.RetrieveAPIView):
queryset = models.Playlist.objects.all()
serializer_class = serializers.PlaylistSerializer
permission_classes = [permissions.AllowAny]
......@@ -33,7 +33,8 @@ def playlist_mrss_view(request, pk):
mrss_items = []
playlist = get_object_or_404(models.Playlist, pk=pk)
for video in playlist.videos.all():
for playlist_video in playlist.playlist_video.all():
video = playlist_video.video
item = RSSItem(
title=video.title,
link='link',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment