Cover quick import.
authorRadek Czajka <rczajka@rczajka.pl>
Fri, 25 Feb 2022 14:42:47 +0000 (15:42 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Fri, 25 Feb 2022 14:42:47 +0000 (15:42 +0100)
src/cover/forms.py
src/cover/models.py
src/cover/urls.py
src/cover/views.py
src/documents/templates/documents/book_detail.html

index c4f06fa..3be4e21 100644 (file)
@@ -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']
+
index 60f1bca..45b5ff9 100644 (file)
@@ -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)
index cff09a6..8278835 100644 (file)
@@ -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<pk>\d+)/?$', views.image, name='cover_image'),
     url(r'^image/(?P<pk>\d+)/file/', views.image_file, name='cover_file'),
     url(r'^add_image/$', views.add_image, name='cover_add_image'),
+    path('quick-import/<int:pk>/', views.quick_import, name='cover_quick_import'),
 ]
index a7b96fb..02d642c 100644 (file)
@@ -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())
+        
index 55da225..1c06f97 100644 (file)
 <div class="col-lg-3">
 <img class="cover-preview" src="{% url 'cover_preview' book.slug %}" />
 {% if book.dc_cover_image %}
-    <a href="{{ book.dc_cover_image.get_absolute_url }}">{{ book.dc_cover_image }}</a>
+  <a href="{{ book.dc_cover_image.get_absolute_url }}">{{ book.dc_cover_image }}</a>
 {% endif %}
+<br><br>
+
+<form action="{% url 'cover_quick_import' book.pk %}" method="post">
+  {% csrf_token %}
+  <input type="url" name="url">
+  <button type="submit" class="btn btn-sm btn-info">></button>
+</form>
+
+
 <br><br>
 <form action="{% url 'cover_preview' book.slug %}">
 <input type="hidden" name="download" value="1">