fix
[audio.git] / src / youtube / views.py
index 73eecd3..e0bcab2 100644 (file)
@@ -17,20 +17,30 @@ from . import models, tasks
 @permission_required('archive.change_audiobook')
 def publish(request, aid, publish=True):
     audiobook = get_object_or_404(Audiobook, id=aid)
-    tags = {}
-    #audiobook.set_youtube_tags(tags)
-    audiobook.youtube_status = status.WAITING
-    audiobook.save(update_fields=['youtube_status'])
-    audiobook.youtube_task = tasks.YouTubeTask.delay(request.user.id, aid, publish).task_id
-    audiobook.save(update_fields=['youtube_task'])
+    if audiobook.is_youtube_publishable:
+        audiobook.youtube_publish()
     return redirect(reverse('file', args=[aid]))
 
 
-def thumbnail(request, 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)
-    yt = models.YouTube.objects.first()
-    buf = yt.prepare_thumbnail(audiobook)
-    return HttpResponse(buf.getvalue(), content_type='image/png')
+    if thumbnail_id is None:
+        yt = audiobook.project.youtube
+        buf = yt.prepare_thumbnail(audiobook)
+    else:
+        template = get_object_or_404(models.ThumbnailTemplate, id=thumbnail_id)
+        buf = template.generate(audiobook)
+    buf = buf.getvalue() if buf is not None else b''
+    return HttpResponse(buf, content_type='image/png')
 
 
 class Preview(DetailView):
@@ -39,10 +49,11 @@ class Preview(DetailView):
 
     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['templates'] = models.ThumbnailTemplate.objects.all()
         return ctx
 
 
@@ -51,6 +62,18 @@ class Update(SingleObjectMixin, View):
     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]))
+
+
+@method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
+class UpdateThumbnail(SingleObjectMixin, View):
+    model = Audiobook
+
+    def post(self, request, pk):
+        obj = self.get_object()
+        yt = obj.project.youtube
+        yt.update_thumbnail(obj)
         return redirect(reverse('file', args=[pk]))