From 5d9c33d00933597e218b732e3bd0bcbb55c4f371 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 25 Feb 2022 15:42:47 +0100 Subject: [PATCH 1/1] Cover quick import. --- src/cover/forms.py | 6 ++ src/cover/models.py | 1 + src/cover/urls.py | 2 + src/cover/views.py | 64 ++++++++++++++++++- .../templates/documents/book_detail.html | 11 +++- 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/cover/forms.py b/src/cover/forms.py index c4f06fad..3be4e211 100644 --- a/src/cover/forms.py +++ b/src/cover/forms.py @@ -21,6 +21,11 @@ class ImageAddForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(ImageAddForm, self).__init__(*args, **kwargs) self.fields['file'].required = False + self.fields['use_file'].required = False + self.fields['cut_top'].required = False + self.fields['cut_left'].required = False + self.fields['cut_bottom'].required = False + self.fields['cut_right'].required = False def clean_download_url(self): cl = self.cleaned_data['download_url'] or None @@ -103,3 +108,4 @@ class ImportForm(forms.Form): for field_name in ('license_url', 'license_name', 'author', 'title', 'download_url'): self.cleaned_data[field_name] = import_data[field_name] return import_data['source_url'] + diff --git a/src/cover/models.py b/src/cover/models.py index 60f1bca5..45b5ff9e 100644 --- a/src/cover/models.py +++ b/src/cover/models.py @@ -52,6 +52,7 @@ class Image(models.Model): return u"%s - %s" % (self.author, self.title) def save(self, **kwargs): + super().save(**kwargs) img = self.file if self.cut_top or self.cut_bottom or self.cut_left or self.cut_right: img = PILImage.open(img) diff --git a/src/cover/urls.py b/src/cover/urls.py index cff09a68..82788351 100644 --- a/src/cover/urls.py +++ b/src/cover/urls.py @@ -2,6 +2,7 @@ # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # from django.conf.urls import url +from django.urls import path from . import views @@ -17,4 +18,5 @@ urlpatterns = [ url(r'^image/(?P\d+)/?$', views.image, name='cover_image'), url(r'^image/(?P\d+)/file/', views.image_file, name='cover_file'), url(r'^add_image/$', views.add_image, name='cover_add_image'), + path('quick-import//', views.quick_import, name='cover_quick_import'), ] diff --git a/src/cover/views.py b/src/cover/views.py index a7b96fbb..02d642c4 100644 --- a/src/cover/views.py +++ b/src/cover/views.py @@ -8,10 +8,14 @@ from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.shortcuts import get_object_or_404, render from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_POST +from lxml import etree +from librarian import RDFNS, DCNS from documents.helpers import active_tab -from documents.models import Chunk +from documents.models import Book, Chunk from cover.models import Image from cover import forms +from cover.utils import get_import_data + PREVIEW_SIZE = (216, 300) @@ -150,3 +154,61 @@ def add_image(request): 'form': form, 'ff': ff, }) + +@permission_required('cover.add_image') +def quick_import(request, pk): + url = request.POST.get('url') + if url.startswith('%s://%s/' % ( + request.scheme, + request.get_host())): + cover_id = url.rsplit('/', 1)[-1] + cover = Image.objects.get(pk=cover_id) + else: + data = get_import_data(url) + same = Image.objects.filter(source_url=data['source_url']) + if not same.exists(): + same = Image.objects.filter(download_url=data['download_url']) + if same.exists(): + cover = same.first() + else: + form = forms.ImageAddForm(data) + if form.is_valid(): + cover = form.save() + + # We have a cover. Now let's commit. + book = Book.objects.get(pk=pk) + chunk = book[0] + text = chunk.head.materialize() + + root = etree.fromstring(text) + rdf = root.find('.//' + RDFNS('Description')) + for tag in 'url', 'attribution', 'source': + for elem in rdf.findall('.//' + DCNS('relation.coverImage.%s' % tag)): + rdf.remove(elem) + e = etree.Element(DCNS('relation.coverImage.url')) + e.text = request.build_absolute_uri(cover.use_file.url) + rdf.append(e) + e.tail = '\n ' + e = etree.Element(DCNS('relation.coverImage.attribution')) + e.text = '' + if cover.title: + e.text += cover.title + ', ' + if cover.author: + e.text += cover.author + ', ' + e.text += cover.license_name + e.tail = '\n ' + rdf.append(e) + e = etree.Element(DCNS('relation.coverImage.source')) + e.text = cover.get_full_url() + e.tail = '\n ' + rdf.append(e) + + xml = etree.tostring(root, encoding='unicode') + chunk.commit( + xml, + author=request.user, + comment='Cover', + publishable=chunk.head.publishable, + ) + return HttpResponseRedirect(book.get_absolute_url()) + diff --git a/src/documents/templates/documents/book_detail.html b/src/documents/templates/documents/book_detail.html index 55da2253..1c06f97c 100644 --- a/src/documents/templates/documents/book_detail.html +++ b/src/documents/templates/documents/book_detail.html @@ -75,8 +75,17 @@
{% if book.dc_cover_image %} - {{ book.dc_cover_image }} + {{ book.dc_cover_image }} {% endif %} +

+ +
+ {% csrf_token %} + + +
+ +

-- 2.20.1