cbe1520649ed3f4daa035c70bc20a53ab63459e7
[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.utils.timezone import now
7 from django.views import View
8 from django.views.decorators.http import require_POST
9 from django.views.generic import DetailView
10 from django.views.generic.detail import SingleObjectMixin
11 from archive.constants import status
12 from archive.models import Audiobook
13 from . import models, tasks
14
15
16
17 @require_POST
18 @permission_required('archive.change_audiobook')
19 def publish(request, aid, publish=True):
20     audiobook = get_object_or_404(Audiobook, id=aid)
21     audiobook.youtube_status = status.QUEUED
22     audiobook.youtube_queued = now()
23     audiobook.save(update_fields=['youtube_status', 'youtube_queued'])
24     return redirect(reverse('file', args=[aid]))
25
26
27 def thumbnail(request, aid):
28     audiobook = get_object_or_404(Audiobook, id=aid)
29     yt = models.YouTube.objects.first()
30     buf = yt.prepare_thumbnail(audiobook)
31     return HttpResponse(buf.getvalue(), content_type='image/png')
32
33
34 class Preview(DetailView):
35     model = Audiobook
36     template_name = 'youtube/preview.html'
37
38     def get_context_data(self, **kwargs):
39         ctx = super().get_context_data(**kwargs)
40         yt = models.YouTube.objects.first()
41         ctx['data'] = yt.get_data(ctx['object'])
42         ctx['title'] = yt.get_title(ctx['object'])
43         ctx['description'] = yt.get_description(ctx['object'])
44         return ctx
45
46
47 @method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
48 class Update(SingleObjectMixin, View):
49     model = Audiobook
50
51     def post(self, request, pk):
52         yt = models.YouTube.objects.first()
53         yt.update_data(self.get_object())
54         return redirect(reverse('file', args=[pk]))