X-Git-Url: https://git.mdrn.pl/audio.git/blobdiff_plain/d7186e88fcc0cf5d5449a16d3907e01aaafc484e..776645a64178b154e8b41b7d76947b47bef91dc6:/src/youtube/views.py?ds=sidebyside diff --git a/src/youtube/views.py b/src/youtube/views.py index 1247c50..73eecd3 100644 --- a/src/youtube/views.py +++ b/src/youtube/views.py @@ -1,10 +1,15 @@ from django.contrib.auth.decorators import permission_required +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.views import View from django.views.decorators.http import require_POST +from django.views.generic import DetailView +from django.views.generic.detail import SingleObjectMixin from archive.constants import status from archive.models import Audiobook -from . import tasks +from . import models, tasks @@ -19,3 +24,33 @@ def publish(request, aid, publish=True): audiobook.youtube_task = tasks.YouTubeTask.delay(request.user.id, aid, publish).task_id audiobook.save(update_fields=['youtube_task']) return redirect(reverse('file', args=[aid])) + + +def thumbnail(request, aid): + 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') + + +class Preview(DetailView): + model = Audiobook + template_name = 'youtube/preview.html' + + def get_context_data(self, **kwargs): + ctx = super().get_context_data(**kwargs) + yt = models.YouTube.objects.first() + ctx['data'] = yt.get_data(ctx['object']) + ctx['title'] = yt.get_title(ctx['object']) + ctx['description'] = yt.get_description(ctx['object']) + return ctx + + +@method_decorator(permission_required('archive.change_audiobook'), name='dispatch') +class Update(SingleObjectMixin, View): + model = Audiobook + + def post(self, request, pk): + yt = models.YouTube.objects.first() + yt.update_data(self.get_object()) + return redirect(reverse('file', args=[pk]))