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
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']
+
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
from django.conf.urls import url
+from django.urls import path
from . import views
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'),
]
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)
'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())
+
<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">