Show YouTube publishing status.
[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     tags = {}
21     #audiobook.set_youtube_tags(tags)
22     audiobook.youtube_status = status.WAITING
23     audiobook.save(update_fields=['youtube_status'])
24     audiobook.youtube_task = tasks.YouTubeTask.delay(request.user.id, aid, publish).task_id
25     audiobook.save(update_fields=['youtube_task'])
26     return redirect(reverse('file', args=[aid]))
27
28
29 def thumbnail(request, aid):
30     audiobook = get_object_or_404(Audiobook, id=aid)
31     yt = models.YouTube.objects.first()
32     buf = yt.prepare_thumbnail(audiobook)
33     return HttpResponse(buf.getvalue(), content_type='image/png')
34
35
36 class Preview(DetailView):
37     model = Audiobook
38     template_name = 'youtube/preview.html'
39
40     def get_context_data(self, **kwargs):
41         ctx = super().get_context_data(**kwargs)
42         yt = models.YouTube.objects.first()
43         ctx['data'] = yt.get_data(ctx['object'])
44         ctx['title'] = yt.get_title(ctx['object'])
45         ctx['description'] = yt.get_description(ctx['object'])
46         return ctx
47
48
49 @method_decorator(permission_required('archive.change_audiobook'), name='dispatch')
50 class Update(SingleObjectMixin, View):
51     model = Audiobook
52
53     def post(self, request, pk):
54         yt = models.YouTube.objects.first()
55         yt.update_data(self.get_object())
56         return redirect(reverse('file', args=[pk]))