fix
[audio.git] / src / youtube / views.py
index 058178f..e0bcab2 100644 (file)
@@ -3,7 +3,6 @@ from django.http import HttpResponse
 from django.shortcuts import redirect, get_object_or_404
 from django.urls import reverse
 from django.utils.decorators import method_decorator
 from django.shortcuts import redirect, get_object_or_404
 from django.urls import reverse
 from django.utils.decorators import method_decorator
-from django.utils.timezone import now
 from django.views import View
 from django.views.decorators.http import require_POST
 from django.views.generic import DetailView
 from django.views import View
 from django.views.decorators.http import require_POST
 from django.views.generic import DetailView
@@ -19,16 +18,23 @@ from . import models, tasks
 def publish(request, aid, publish=True):
     audiobook = get_object_or_404(Audiobook, id=aid)
     if audiobook.is_youtube_publishable:
 def publish(request, aid, publish=True):
     audiobook = get_object_or_404(Audiobook, id=aid)
     if audiobook.is_youtube_publishable:
-        audiobook.youtube_status = status.QUEUED
-        audiobook.youtube_queued = now()
-        audiobook.save(update_fields=['youtube_status', 'youtube_queued'])
+        audiobook.youtube_publish()
     return redirect(reverse('file', args=[aid]))
 
 
     return redirect(reverse('file', args=[aid]))
 
 
+@require_POST
+@permission_required('archive.change_audiobook')
+def book_publish(request, slug):
+    for audiobook in Audiobook.objects.filter(slug=slug).order_by("index"):
+        if audiobook.is_youtube_publishable:
+            audiobook.youtube_publish()
+    return redirect(reverse('book', args=[slug]))
+
+
 def thumbnail(request, aid, thumbnail_id=None):
     audiobook = get_object_or_404(Audiobook, id=aid)
     if thumbnail_id is None:
 def thumbnail(request, aid, thumbnail_id=None):
     audiobook = get_object_or_404(Audiobook, id=aid)
     if thumbnail_id is None:
-        yt = models.YouTube.objects.first()
+        yt = audiobook.project.youtube
         buf = yt.prepare_thumbnail(audiobook)
     else:
         template = get_object_or_404(models.ThumbnailTemplate, id=thumbnail_id)
         buf = yt.prepare_thumbnail(audiobook)
     else:
         template = get_object_or_404(models.ThumbnailTemplate, id=thumbnail_id)
@@ -43,7 +49,7 @@ class Preview(DetailView):
 
     def get_context_data(self, **kwargs):
         ctx = super().get_context_data(**kwargs)
 
     def get_context_data(self, **kwargs):
         ctx = super().get_context_data(**kwargs)
-        yt = models.YouTube.objects.first()
+        yt = ctx['object'].project.youtube
         ctx['data'] = yt.get_data(ctx['object'])
         ctx['title'] = yt.get_title(ctx['object'])
         ctx['description'] = yt.get_description(ctx['object'])
         ctx['data'] = yt.get_data(ctx['object'])
         ctx['title'] = yt.get_title(ctx['object'])
         ctx['description'] = yt.get_description(ctx['object'])
@@ -56,8 +62,9 @@ class Update(SingleObjectMixin, View):
     model = Audiobook
 
     def post(self, request, pk):
     model = Audiobook
 
     def post(self, request, pk):
-        yt = models.YouTube.objects.first()
-        yt.update_data(self.get_object())
+        obj = self.get_object()
+        yt = obj.project.youtube
+        yt.update_data(obj)
         return redirect(reverse('file', args=[pk]))
 
 
         return redirect(reverse('file', args=[pk]))
 
 
@@ -66,6 +73,7 @@ class UpdateThumbnail(SingleObjectMixin, View):
     model = Audiobook
 
     def post(self, request, pk):
     model = Audiobook
 
     def post(self, request, pk):
-        yt = models.YouTube.objects.first()
-        yt.update_thumbnail(self.get_object())
+        obj = self.get_object()
+        yt = obj.project.youtube
+        yt.update_thumbnail(obj)
         return redirect(reverse('file', args=[pk]))
         return redirect(reverse('file', args=[pk]))