X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/5913c54d19b8f6775633176032161d49f9b2f1aa..a4f5747875aa0fb598e69d2200ef431c2fb7f1e9:/src/cover/views.py?ds=inline diff --git a/src/cover/views.py b/src/cover/views.py index 3f2c46fa..620ad1aa 100644 --- a/src/cover/views.py +++ b/src/cover/views.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- -# # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # @@ -10,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 catalogue.helpers import active_tab -from catalogue.models import Chunk +from lxml import etree +from librarian import RDFNS, DCNS +from documents.helpers import active_tab +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) @@ -37,17 +39,30 @@ def preview(request, book, chunk=None, rev=None): else: revision = chunk.publishable() if revision is None: - raise Http404 + revision = chunk.head xml = revision.materialize().encode('utf-8') try: info = BookInfo.from_bytes(xml) except: return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png")) - cover = make_cover(info) + width = request.GET.get('width') + width = int(width) if width else None + height=request.GET.get('height') + height = int(height) if height else None + cover = make_cover(info, width=width, height=height) + #cover = make_cover(info) response = HttpResponse(content_type=cover.mime_type()) - img = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS) + if height or width: + size = (width, height) + else: + size = PREVIEW_SIZE + img = cover.image().resize(size, Image.ANTIALIAS) img.save(response, cover.format) + + if 'download' in request.GET: + response['Content-Disposition'] = 'attachment; filename=%s.jpg' % chunk.book.slug + return response @@ -112,7 +127,7 @@ def image_file(request, pk): @active_tab('cover') def image_list(request): return render(request, "cover/image_list.html", { - 'object_list': Image.objects.all(), + 'object_list': Image.objects.all().order_by('-id'), 'can_add': request.user.has_perm('cover.add_image'), }) @@ -122,8 +137,8 @@ def image_list(request): def add_image(request): form = ff = None if request.method == 'POST': - if request.POST.get('form_id') == 'flickr': - ff = forms.FlickrForm(request.POST) + if request.POST.get('form_id') == 'import': + ff = forms.ImportForm(request.POST) if ff.is_valid(): form = forms.ImageAddForm(ff.cleaned_data) else: @@ -134,8 +149,66 @@ def add_image(request): if form is None: form = forms.ImageAddForm() if ff is None: - ff = forms.FlickrForm() + ff = forms.ImportForm() return render(request, 'cover/add_image.html', { '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()) +