1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.conf import settings
6 from django.contrib.auth.decorators import permission_required
7 from django.http import HttpResponse, HttpResponseRedirect, Http404
8 from django.shortcuts import get_object_or_404, render
9 from django.views.decorators.csrf import csrf_exempt
10 from django.views.decorators.http import require_POST
11 from documents.helpers import active_tab
12 from documents.models import Chunk
13 from cover.models import Image
14 from cover import forms
16 PREVIEW_SIZE = (216, 300)
19 def preview(request, book, chunk=None, rev=None):
20 """Creates a cover image.
22 If chunk and rev number are given, use version from given revision.
23 If rev is not given, use publishable version.
26 from librarian.cover import make_cover
27 from librarian.dcparser import BookInfo
29 chunk = Chunk.get(book, chunk)
32 revision = chunk.at_revision(rev)
33 except Chunk.change_model.DoesNotExist:
36 revision = chunk.publishable()
39 xml = revision.materialize().encode('utf-8')
42 info = BookInfo.from_bytes(xml)
44 return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
45 width = request.GET.get('width')
46 width = int(width) if width else None
47 height=request.GET.get('height')
48 height = int(height) if height else None
49 cover = make_cover(info, width=width, height=height)
50 #cover = make_cover(info)
51 response = HttpResponse(content_type=cover.mime_type())
53 size = (width, height)
56 img = cover.image().resize(size, Image.ANTIALIAS)
57 img.save(response, cover.format)
59 if 'download' in request.GET:
60 response['Content-Disposition'] = 'attachment; filename=%s.jpg' % chunk.book.slug
67 def preview_from_xml(request):
68 from hashlib import sha1
70 from os import makedirs
71 from lxml import etree
72 from librarian.cover import make_cover
73 from librarian.dcparser import BookInfo
75 xml = request.POST['xml']
77 info = BookInfo.from_bytes(xml.encode('utf-8'))
79 return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
80 coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
81 cover = make_cover(info)
83 cover_dir = 'cover/preview'
85 makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
88 fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
89 img = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
90 img.save(os.path.join(settings.MEDIA_ROOT, fname))
91 return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
95 def image(request, pk):
96 img = get_object_or_404(Image, pk=pk)
98 if request.user.has_perm('cover.change_image'):
99 if request.method == "POST":
100 form = forms.ImageEditForm(request.POST, request.FILES, instance=img)
103 return HttpResponseRedirect(img.get_absolute_url())
105 form = forms.ImageEditForm(instance=img)
108 form = forms.ReadonlyImageEditForm(instance=img)
111 return render(request, "cover/image_detail.html", {
112 "object": Image.objects.get(id=img.id),
114 "editable": editable,
118 def image_file(request, pk):
119 img = get_object_or_404(Image, pk=pk)
120 return HttpResponseRedirect(img.file.url)
124 def image_list(request):
125 return render(request, "cover/image_list.html", {
126 'object_list': Image.objects.all(),
127 'can_add': request.user.has_perm('cover.add_image'),
131 @permission_required('cover.add_image')
133 def add_image(request):
135 if request.method == 'POST':
136 if request.POST.get('form_id') == 'import':
137 ff = forms.ImportForm(request.POST)
139 form = forms.ImageAddForm(ff.cleaned_data)
141 form = forms.ImageAddForm(request.POST, request.FILES)
144 return HttpResponseRedirect(obj.get_absolute_url())
146 form = forms.ImageAddForm()
148 ff = forms.ImportForm()
149 return render(request, 'cover/add_image.html', {