Commit 173868ae authored by Ilya Simonov's avatar Ilya Simonov

add function add_video_to_playlist

parent 41e72cae
...@@ -2,11 +2,10 @@ import requests ...@@ -2,11 +2,10 @@ import requests
import logging import logging
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from .models import Video, Playlist, TagToObject, READY, DYNAMIC, FAIL from .models import Video, READY, FAIL
from .s3_uploader import upload_file from .s3_uploader import upload_file
from .utils import asset_upload from .utils import asset_upload, add_video_to_playlist
from cp_video.celery import app from cp_video.celery import app
...@@ -15,9 +14,6 @@ log = logging.getLogger('send_video_to_s3') ...@@ -15,9 +14,6 @@ log = logging.getLogger('send_video_to_s3')
@app.task(soft_time_limit=600) @app.task(soft_time_limit=600)
def send_video_to_s3(video_id=None): def send_video_to_s3(video_id=None):
video_content_type = ContentType.objects.get_for_model(Video)
playlist_content_type = ContentType.objects.get_for_model(Playlist)
local_video = Video.objects.get(id=video_id) local_video = Video.objects.get(id=video_id)
try: try:
...@@ -39,25 +35,8 @@ def send_video_to_s3(video_id=None): ...@@ -39,25 +35,8 @@ def send_video_to_s3(video_id=None):
log.info(f'Video id {local_video.id} uploaded to S3...') log.info(f'Video id {local_video.id} uploaded to S3...')
tag_ids = list(TagToObject.objects.filter( add_video_to_playlist(local_video)
content_type=video_content_type,
object_id=video_id,
).values_list('tag_id', flat=True))
if tag_ids:
playlist_ids = list(TagToObject.objects.filter(
content_type=playlist_content_type,
tag_id__in=tag_ids,
).values_list('object_id', flat=True))
if playlist_ids:
playlists = Playlist.objects.filter(
id__in=playlist_ids,
type=DYNAMIC,
)
for playlist in playlists:
playlist.videos.add(local_video)
except Exception as e: except Exception as e:
log.error(e) log.error(e)
local_video.status = FAIL local_video.status = FAIL
......
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from moviepy.editor import VideoFileClip from moviepy.editor import VideoFileClip
from .models import Video, Playlist, TagToObject, DYNAMIC
def asset_upload(instance, filename): def asset_upload(instance, filename):
return f'videos/{filename}' return f'videos/{filename}'
...@@ -16,3 +19,25 @@ def generate_thumbnail(video_path, video_name): ...@@ -16,3 +19,25 @@ def generate_thumbnail(video_path, video_name):
thumbnail_name = thumbnail_name.split('/')[-1] thumbnail_name = thumbnail_name.split('/')[-1]
return thumbnail_name return thumbnail_name
def add_video_to_playlist(video):
video_content_type = ContentType.objects.get_for_model(Video)
playlist_content_type = ContentType.objects.get_for_model(Playlist)
tag_ids = list(TagToObject.objects.filter(
content_type=video_content_type,
object_id=video.id,
).values_list('tag_id', flat=True))
if tag_ids:
playlist_ids = list(TagToObject.objects.filter(
content_type=playlist_content_type,
tag_id__in=tag_ids,
).values_list('object_id', flat=True))
if playlist_ids:
playlists = Playlist.objects.filter(id__in=playlist_ids, type=DYNAMIC)
for playlist in playlists:
playlist.videos.add(video)
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