# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
+from hashlib import sha1
+from os import makedirs
import os.path
+import PIL.Image
from django.conf import settings
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.views.decorators.http import require_POST
from lxml import etree
from librarian import RDFNS, DCNS
+from librarian.cover import make_cover
+from librarian.dcparser import BookInfo
from documents.helpers import active_tab
from documents.models import Book, Chunk
from cover.models import Image
If chunk and rev number are given, use version from given revision.
If rev is not given, use publishable version.
"""
- from PIL import Image
- from librarian.cover import make_cover
- from librarian.dcparser import BookInfo
-
chunk = Chunk.get(book, chunk)
if rev is not None:
try:
try:
info = BookInfo.from_bytes(xml)
- except:
+ except Exception as e:
+ print(e)
return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
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)
+
+ if not (height or width):
+ width, height = PREVIEW_SIZE
+
+ cover_class = request.GET.get('cover_class', 'default')
+
+ cover = make_cover(info, cover_class=cover_class, width=width, height=height)
response = HttpResponse(content_type=cover.mime_type())
- if height or width:
- size = (width, height)
- else:
- size = PREVIEW_SIZE
- img = cover.image().resize(size, Image.ANTIALIAS)
+ img = cover.final_image()
img.save(response, cover.format)
if 'download' in request.GET:
@csrf_exempt
@require_POST
def preview_from_xml(request):
- from hashlib import sha1
- from PIL import Image
- from os import makedirs
- from lxml import etree
- from librarian.cover import make_cover
- from librarian.dcparser import BookInfo
-
xml = request.POST['xml']
try:
info = BookInfo.from_bytes(xml.encode('utf-8'))
- except:
+ except Exception as e:
+ print(e)
return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
cover = make_cover(info)
except OSError:
pass
fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
- img = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
+ img = cover.image().resize(PREVIEW_SIZE, PIL.Image.ANTIALIAS)
img.save(os.path.join(settings.MEDIA_ROOT, fname))
return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
if url.startswith('%s://%s/' % (
request.scheme,
request.get_host())):
- cover_id = url.rsplit('/', 1)[-1]
+ cover_id = url.rstrip('/').rsplit('/', 1)[-1]
cover = Image.objects.get(pk=cover_id)
else:
data = get_import_data(url)