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 catalogue.helpers import active_tab
12 from catalogue.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 cover = make_cover(info)
46 response = HttpResponse(content_type=cover.mime_type())
47 img = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
48 img.save(response, cover.format)
54 def preview_from_xml(request):
55 from hashlib import sha1
57 from os import makedirs
58 from lxml import etree
59 from librarian.cover import make_cover
60 from librarian.dcparser import BookInfo
62 xml = request.POST['xml']
64 info = BookInfo.from_bytes(xml.encode('utf-8'))
66 return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
67 coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
68 cover = make_cover(info)
70 cover_dir = 'cover/preview'
72 makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
75 fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
76 img = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
77 img.save(os.path.join(settings.MEDIA_ROOT, fname))
78 return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
82 def image(request, pk):
83 img = get_object_or_404(Image, pk=pk)
85 if request.user.has_perm('cover.change_image'):
86 if request.method == "POST":
87 form = forms.ImageEditForm(request.POST, request.FILES, instance=img)
90 return HttpResponseRedirect(img.get_absolute_url())
92 form = forms.ImageEditForm(instance=img)
95 form = forms.ReadonlyImageEditForm(instance=img)
98 return render(request, "cover/image_detail.html", {
99 "object": Image.objects.get(id=img.id),
101 "editable": editable,
105 def image_file(request, pk):
106 img = get_object_or_404(Image, pk=pk)
107 return HttpResponseRedirect(img.file.url)
111 def image_list(request):
112 return render(request, "cover/image_list.html", {
113 'object_list': Image.objects.all(),
114 'can_add': request.user.has_perm('cover.add_image'),
118 @permission_required('cover.add_image')
120 def add_image(request):
122 if request.method == 'POST':
123 if request.POST.get('form_id') == 'flickr':
124 ff = forms.FlickrForm(request.POST)
126 form = forms.ImageAddForm(ff.cleaned_data)
128 form = forms.ImageAddForm(request.POST, request.FILES)
131 return HttpResponseRedirect(obj.get_absolute_url())
133 form = forms.ImageAddForm()
135 ff = forms.FlickrForm()
136 return render(request, 'cover/add_image.html', {