1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from django.conf import settings
8 from django.contrib.auth.decorators import permission_required
9 from django.http import HttpResponse, HttpResponseRedirect, Http404
10 from django.shortcuts import get_object_or_404, render
11 from django.views.decorators.csrf import csrf_exempt
12 from django.views.decorators.http import require_POST
13 from catalogue.helpers import active_tab
14 from catalogue.models import Chunk
15 from cover.models import Image
16 from cover import forms
18 PREVIEW_SIZE = (216, 300)
21 def preview(request, book, chunk=None, rev=None):
22 """Creates a cover image.
24 If chunk and rev number are given, use version from given revision.
25 If rev is not given, use publishable version.
28 from librarian.cover import make_cover
29 from librarian.dcparser import BookInfo
31 chunk = Chunk.get(book, chunk)
34 revision = chunk.at_revision(rev)
35 except Chunk.change_model.DoesNotExist:
38 revision = chunk.publishable()
41 xml = revision.materialize().encode('utf-8')
44 info = BookInfo.from_string(xml)
46 return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
47 cover = make_cover(info)
48 response = HttpResponse(content_type=cover.mime_type())
49 image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
50 image.save(response, cover.format)
56 def preview_from_xml(request):
57 from hashlib import sha1
59 from os import makedirs
60 from lxml import etree
61 from librarian.cover import make_cover
62 from librarian.dcparser import BookInfo
64 xml = request.POST['xml']
66 info = BookInfo.from_string(xml.encode('utf-8'))
68 return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
69 coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
70 cover = make_cover(info)
72 cover_dir = 'cover/preview'
74 makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
77 fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
78 image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
79 image.save(os.path.join(settings.MEDIA_ROOT, fname))
80 return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
84 def image(request, pk):
85 image = get_object_or_404(Image, pk=pk)
87 if request.user.has_perm('cover.change_image'):
88 if request.method == "POST":
89 form = forms.ImageEditForm(request.POST, request.FILES, instance=image)
92 return HttpResponseRedirect(image.get_absolute_url())
94 form = forms.ImageEditForm(instance=image)
97 form = forms.ReadonlyImageEditForm(instance=image)
100 return render(request, "cover/image_detail.html", {
101 "object": Image.objects.get(id=image.id),
103 "editable": editable,
108 def image_list(request):
109 return render(request, "cover/image_list.html", {
110 'object_list': Image.objects.all(),
111 'can_add': request.user.has_perm('cover.add_image'),
115 @permission_required('cover.add_image')
117 def add_image(request):
119 if request.method == 'POST':
120 if request.POST.get('form_id') == 'flickr':
121 ff = forms.FlickrForm(request.POST)
123 form = forms.ImageAddForm(ff.cleaned_data)
125 form = forms.ImageAddForm(request.POST, request.FILES)
128 return HttpResponseRedirect(obj.get_absolute_url())
130 form = forms.ImageAddForm()
132 ff = forms.FlickrForm()
133 return render(request, 'cover/add_image.html', {