fix
[audio.git] / src / youtube / views.py
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
13
14
15
16 @require_POST
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]))
23
24
25 @require_POST
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]))
32
33
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 = audiobook.project.youtube
38         buf = yt.prepare_thumbnail(audiobook)
39     else:
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')
44
45
46 class Preview(DetailView):
47     model = Audiobook
48     template_name = 'youtube/preview.html'
49
50     def get_context_data(self, **kwargs):
51         ctx = super().get_context_data(**kwargs)
52         yt = ctx['object'].project.youtube
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()
57         return ctx
58
59
60 @method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
61 class Update(SingleObjectMixin, View):
62     model = Audiobook
63
64     def post(self, request, pk):
65         obj = self.get_object()
66         yt = obj.project.youtube
67         yt.update_data(obj)
68         return redirect(reverse('file', args=[pk]))
69
70
71 @method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
72 class UpdateThumbnail(SingleObjectMixin, View):
73     model = Audiobook
74
75     def post(self, request, pk):
76         obj = self.get_object()
77         yt = obj.project.youtube
78         yt.update_thumbnail(obj)
79         return redirect(reverse('file', args=[pk]))