fnp
/
redakcja.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
rename
[redakcja.git]
/
src
/
cover
/
views.py
diff --git
a/src/cover/views.py
b/src/cover/views.py
index
660909d
..
d00123c
100644
(file)
--- a/
src/cover/views.py
+++ b/
src/cover/views.py
@@
-1,7
+1,10
@@
# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
# 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 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.conf import settings
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse, HttpResponseRedirect, Http404
@@
-10,6
+13,8
@@
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 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
from documents.helpers import active_tab
from documents.models import Book, Chunk
from cover.models import Image
@@
-17,7
+22,7
@@
from cover import forms
from cover.utils import get_import_data
from cover.utils import get_import_data
-PREVIEW_SIZE = (21
6
, 300)
+PREVIEW_SIZE = (21
2
, 300)
def preview(request, book, chunk=None, rev=None):
def preview(request, book, chunk=None, rev=None):
@@
-26,11
+31,14
@@
def preview(request, book, chunk=None, rev=None):
If chunk and rev number are given, use version from given revision.
If rev is not given, use publishable version.
"""
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
+ try:
+ chunk = Chunk.get(book, chunk)
+ except Chunk.DoesNotExist:
+ raise Http404
+
+ if chunk.book.cover and rev is None and not request.GET.get('width') and not request.GET.get('height'):
+ return HttpResponseRedirect(chunk.book.cover.url)
- chunk = Chunk.get(book, chunk)
if rev is not None:
try:
revision = chunk.at_revision(rev)
if rev is not None:
try:
revision = chunk.at_revision(rev)
@@
-44,7
+52,8
@@
def preview(request, book, chunk=None, rev=None):
try:
info = BookInfo.from_bytes(xml)
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
return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
width = request.GET.get('width')
width = int(width) if width else None
@@
-70,17
+79,11
@@
def preview(request, book, chunk=None, rev=None):
@csrf_exempt
@require_POST
def preview_from_xml(request):
@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'))
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)
return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
cover = make_cover(info)
@@
-91,7
+94,7
@@
def preview_from_xml(request):
except OSError:
pass
fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
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))
img.save(os.path.join(settings.MEDIA_ROOT, fname))
return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
@@
-162,7
+165,7
@@
def quick_import(request, pk):
if url.startswith('%s://%s/' % (
request.scheme,
request.get_host())):
if url.startswith('%s://%s/' % (
request.scheme,
request.get_host())):
- cover_id = url.rsplit('/', 1)[-1]
+ cover_id = url.rs
trip('/').rs
plit('/', 1)[-1]
cover = Image.objects.get(pk=cover_id)
else:
data = get_import_data(url)
cover = Image.objects.get(pk=cover_id)
else:
data = get_import_data(url)