1 from django.contrib.auth.decorators import permission_required
2 from django.http import HttpResponse
3 from django.shortcuts import redirect, get_object_or_404
4 from django.urls import reverse
5 from django.utils.decorators import method_decorator
6 from django.views import View
7 from django.views.decorators.http import require_POST
8 from django.views.generic import DetailView
9 from django.views.generic.detail import SingleObjectMixin
10 from archive.constants import status
11 from archive.models import Audiobook
12 from . import models, tasks
17 @permission_required('archive.change_audiobook')
18 def publish(request, aid, publish=True):
19 audiobook = get_object_or_404(Audiobook, id=aid)
20 if audiobook.is_youtube_publishable:
21 audiobook.youtube_publish()
22 return redirect(reverse('file', args=[aid]))
26 @permission_required('archive.change_audiobook')
27 def book_publish(request, slug):
28 for audiobook in Audiobook.objects.filter(slug=slug).order_by("index"):
29 if audiobook.is_youtube_publishable:
30 audiobook.youtube_publish()
31 return redirect(reverse('book', args=[slug]))
34 def thumbnail(request, aid, thumbnail_id=None):
35 audiobook = get_object_or_404(Audiobook, id=aid)
36 if thumbnail_id is None:
37 yt = models.YouTube.objects.first()
38 buf = yt.prepare_thumbnail(audiobook)
40 template = get_object_or_404(models.ThumbnailTemplate, id=thumbnail_id)
41 buf = template.generate(audiobook)
42 buf = buf.getvalue() if buf is not None else b''
43 return HttpResponse(buf, content_type='image/png')
46 class Preview(DetailView):
48 template_name = 'youtube/preview.html'
50 def get_context_data(self, **kwargs):
51 ctx = super().get_context_data(**kwargs)
52 yt = models.YouTube.objects.first()
53 ctx['data'] = yt.get_data(ctx['object'])
54 ctx['title'] = yt.get_title(ctx['object'])
55 ctx['description'] = yt.get_description(ctx['object'])
56 ctx['templates'] = models.ThumbnailTemplate.objects.all()
60 @method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
61 class Update(SingleObjectMixin, View):
64 def post(self, request, pk):
65 yt = models.YouTube.objects.first()
66 yt.update_data(self.get_object())
67 return redirect(reverse('file', args=[pk]))
70 @method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
71 class UpdateThumbnail(SingleObjectMixin, View):
74 def post(self, request, pk):
75 yt = models.YouTube.objects.first()
76 yt.update_thumbnail(self.get_object())
77 return redirect(reverse('file', args=[pk]))