From 5899a0fb944e113022869522c46a3b6d2bac5bc5 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 21 May 2020 14:11:19 +0200 Subject: [PATCH 1/1] Add preview and update for YouTube. --- .../templates/archive/file_managed.html | 18 ++++++++++--- src/youtube/templates/youtube/preview.html | 27 +++++++++++++++++++ src/youtube/templatetags/youtube.py | 15 +++++++++++ src/youtube/thumbnail.py | 1 - src/youtube/urls.py | 2 ++ src/youtube/views.py | 27 +++++++++++++++++++ 6 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 src/youtube/templates/youtube/preview.html create mode 100644 src/youtube/templatetags/youtube.py diff --git a/src/archive/templates/archive/file_managed.html b/src/archive/templates/archive/file_managed.html index f7992e4..33a6646 100644 --- a/src/archive/templates/archive/file_managed.html +++ b/src/archive/templates/archive/file_managed.html @@ -69,6 +69,11 @@ {% endif %}
+

+ + {% trans "Preview YouTube metadata" %} + +

{% csrf_token %} @@ -78,7 +83,6 @@ {% csrf_token %}
-
@@ -141,8 +145,16 @@

{% trans "YouTube" %}

- {% if audiobook.youtube_file %} -

{% trans "Download YouTube file." %}

+ {% if audiobook.youtube_id %} + {% if audiobook.youtube_id %} +

+ {% trans "See on YouTube" %} +

+
+ {% csrf_token %} + +
+ {% endif %} {% if audiobook.youtube_published %}

{% trans "Published:" %} {{ audiobook.youtube_published }}

{% if audiobook.get_youtube_published_tags.tags %} diff --git a/src/youtube/templates/youtube/preview.html b/src/youtube/templates/youtube/preview.html new file mode 100644 index 0000000..c017265 --- /dev/null +++ b/src/youtube/templates/youtube/preview.html @@ -0,0 +1,27 @@ +{% extends "archive/base.html" %} + + +{% block content %} + + + +
+
+

{{ title }}

+
+
+ {{ description|linebreaksbr }} +
+
+{% endblock %} diff --git a/src/youtube/templatetags/youtube.py b/src/youtube/templatetags/youtube.py new file mode 100644 index 0000000..bb5b34b --- /dev/null +++ b/src/youtube/templatetags/youtube.py @@ -0,0 +1,15 @@ +import django.template + + +register = django.template.Library() + + +@register.simple_tag +def last_name(author): + # TODO: we should reference the Catalogue to get proper last name. + if author.lower() == 'autor nieznany': + return + parts = author.split(' ') + for part in reversed(parts): + if part.startswith('('): continue + return part diff --git a/src/youtube/thumbnail.py b/src/youtube/thumbnail.py index 3f67cac..a6cd9c5 100644 --- a/src/youtube/thumbnail.py +++ b/src/youtube/thumbnail.py @@ -53,7 +53,6 @@ def draw_version(img, d, context, get_font_path): realheight = draw.textsize(line, font=font)[1] if cursor + realheight > newimg.size[1]: return False - print('x', draw.text((0, cursor), line, font=font, fill=item.get('color'))) cursor += item['line-height'] img.paste(newimg, (d.get('x', 0), d.get('y', 0)), newimg) diff --git a/src/youtube/urls.py b/src/youtube/urls.py index 8dbd3d8..58071e5 100644 --- a/src/youtube/urls.py +++ b/src/youtube/urls.py @@ -6,4 +6,6 @@ urlpatterns = [ url(r'^publish/(\d+)/$', views.publish, name="youtube_publish"), url(r'^convert/(\d+)/$', views.publish, {'publish': False}, name="youtube_convert"), path('thumbnail//', views.thumbnail, name='youtube_thumbnail'), + path('preview//', views.Preview.as_view(), name="youtube_preview"), + path('update//', views.Update.as_view(), name="youtube_update"), ] diff --git a/src/youtube/views.py b/src/youtube/views.py index 4e59f5f..73eecd3 100644 --- a/src/youtube/views.py +++ b/src/youtube/views.py @@ -2,7 +2,11 @@ from django.contrib.auth.decorators import permission_required from django.http import HttpResponse from django.shortcuts import redirect, get_object_or_404 from django.urls import reverse +from django.utils.decorators import method_decorator +from django.views import View from django.views.decorators.http import require_POST +from django.views.generic import DetailView +from django.views.generic.detail import SingleObjectMixin from archive.constants import status from archive.models import Audiobook from . import models, tasks @@ -27,3 +31,26 @@ def thumbnail(request, aid): yt = models.YouTube.objects.first() buf = yt.prepare_thumbnail(audiobook) return HttpResponse(buf.getvalue(), content_type='image/png') + + +class Preview(DetailView): + model = Audiobook + template_name = 'youtube/preview.html' + + def get_context_data(self, **kwargs): + ctx = super().get_context_data(**kwargs) + yt = models.YouTube.objects.first() + ctx['data'] = yt.get_data(ctx['object']) + ctx['title'] = yt.get_title(ctx['object']) + ctx['description'] = yt.get_description(ctx['object']) + return ctx + + +@method_decorator(permission_required('archive.change_audiobook'), name='dispatch') +class Update(SingleObjectMixin, View): + model = Audiobook + + def post(self, request, pk): + yt = models.YouTube.objects.first() + yt.update_data(self.get_object()) + return redirect(reverse('file', args=[pk])) -- 2.20.1