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
19 PREVIEW_SIZE = (216, 300)
22 def preview(request, book, chunk=None, rev=None):
23 """Creates a cover image.
25 If chunk and rev number are given, use version from given revision.
26 If rev is not given, use publishable version.
29 from librarian.cover import DefaultEbookCover
30 from librarian.dcparser import BookInfo
32 chunk = Chunk.get(book, chunk)
35 revision = chunk.at_revision(rev)
36 except Chunk.change_model.DoesNotExist:
39 revision = chunk.publishable()
42 xml = revision.materialize().encode('utf-8')
45 info = BookInfo.from_string(xml)
47 return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
48 cover = DefaultEbookCover(info)
49 response = HttpResponse(content_type=cover.mime_type())
50 image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
51 image.save(response, cover.format)
57 def preview_from_xml(request):
58 from hashlib import sha1
60 from os import makedirs
61 from lxml import etree
62 from librarian.cover import DefaultEbookCover
63 from librarian.dcparser import BookInfo
65 xml = request.POST['xml']
67 info = BookInfo.from_string(xml.encode('utf-8'))
69 return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
70 coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
71 cover = DefaultEbookCover(info)
73 cover_dir = 'cover/preview'
75 makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
78 fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
79 image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
80 image.save(os.path.join(settings.MEDIA_ROOT, fname))
81 return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
85 def image(request, pk):
86 image = get_object_or_404(Image, pk=pk)
88 if request.user.has_perm('cover.change_image'):
89 if request.method == "POST":
90 form = forms.ImageEditForm(request.POST, request.FILES, instance=image)
93 return HttpResponseRedirect(image.get_absolute_url())
95 form = forms.ImageEditForm(instance=image)
98 form = forms.ReadonlyImageEditForm(instance=image)
101 return render(request, "cover/image_detail.html", {
104 "editable": editable,
109 def image_list(request):
110 objects = Image.objects.all()
111 enable_add = request.user.has_perm('cover.add_image')
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', {