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