Fix for covers generation.
[redakcja.git] / src / cover / views.py
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.
3 #
4 from hashlib import sha1
5 from os import makedirs
6 import os.path
7 import PIL.Image
8 from django.conf import settings
9 from django.contrib.auth.decorators import permission_required
10 from django.http import HttpResponse, HttpResponseRedirect, Http404, JsonResponse
11 from django.shortcuts import get_object_or_404, render
12 from django.views.decorators.csrf import csrf_exempt
13 from django.views.decorators.http import require_POST
14 from lxml import etree
15 from librarian import RDFNS, DCNS
16 from librarian.cover import make_cover
17 from librarian.dcparser import BookInfo
18 from documents.helpers import active_tab
19 from documents.models import Book, Chunk
20 from cover.models import Image
21 from cover import forms
22 from cover.utils import get_import_data
23
24
25 PREVIEW_SIZE = (212, 300)
26
27
28 def preview(request, book, chunk=None, rev=None):
29     """Creates a cover image.
30
31     If chunk and rev number are given, use version from given revision.
32     If rev is not given, use publishable version.
33     """
34     try:
35         chunk = Chunk.get(book, chunk)
36     except Chunk.DoesNotExist:
37         raise Http404
38
39     if chunk.book.cover and rev is None and not request.GET.get('width') and not request.GET.get('height'):
40         return HttpResponseRedirect(chunk.book.cover.url)
41
42     if rev is not None:
43         try:
44             revision = chunk.at_revision(rev)
45         except Chunk.change_model.DoesNotExist:
46             raise Http404
47     else:
48         revision = chunk.publishable()
49         if revision is None:
50             revision = chunk.head
51     xml = revision.materialize().encode('utf-8')
52
53     try:
54         info = BookInfo.from_bytes(xml)
55     except Exception as e:
56         print(e)
57         return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
58     width = request.GET.get('width')
59     width = int(width) if width else None
60     height=request.GET.get('height')
61     height = int(height) if height else None
62
63     if not (height or width):
64         width, height = PREVIEW_SIZE
65
66     cover_class = request.GET.get('cover_class', 'default')
67
68     kwargs = {}
69     if chunk.book.project is not None:
70         if chunk.book.project.logo_mono or chunk.book.project.logo:
71             kwargs['cover_logo'] = (chunk.book.project.logo_mono or chunk.book.project.logo).path
72     cover = make_cover(info, cover_class=cover_class, width=width, height=height, **kwargs)
73     response = HttpResponse(content_type=cover.mime_type())
74     img = cover.final_image()
75     img.save(response, cover.format)
76
77     if 'download' in request.GET:
78         response['Content-Disposition'] = 'attachment; filename=%s.jpg' % chunk.book.slug
79
80     return response
81
82
83 @csrf_exempt
84 @require_POST
85 def preview_from_xml(request):
86     xml = request.POST['xml']
87     try:
88         info = BookInfo.from_bytes(xml.encode('utf-8'))
89     except Exception as e:
90         print(e)
91         return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
92     coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
93     cover = make_cover(info)
94
95     cover_dir = 'cover/preview'
96     try:
97         makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
98     except OSError:
99         pass
100     fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
101     img = cover.image().resize(PREVIEW_SIZE, PIL.Image.ANTIALIAS)
102     img.save(os.path.join(settings.MEDIA_ROOT, fname))
103     return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
104
105
106 @active_tab('cover')
107 def image(request, pk):
108     img = get_object_or_404(Image, pk=pk)
109
110     if not request.accepts('text/html') and request.accepts('application/json') or request.GET.get('format') == 'json':
111         return JsonResponse({
112             'title': img.title,
113             'author': img.author,
114             'license_name': img.license_name,
115             'license_url': img.license_url,
116             'source_url': img.source_url,
117             'attribution': img.attribution,
118             'cut_left': img.cut_left,
119             'cut_right': img.cut_right,
120             'cut_top': img.cut_top,
121             'cut_bottom': img.cut_bottom,
122             'file': img.file.url,
123             'use_file': img.use_file.url,
124         })
125
126     if request.user.has_perm('cover.change_image'):
127         if request.method == "POST":
128             form = forms.ImageEditForm(request.POST, request.FILES, instance=img)
129             if form.is_valid():
130                 form.save()
131                 return HttpResponseRedirect(img.get_absolute_url())
132         else:
133             form = forms.ImageEditForm(instance=img)
134         editable = True
135     else:
136         form = forms.ReadonlyImageEditForm(instance=img)
137         editable = False
138
139     return render(request, "cover/image_detail.html", {
140         "object": Image.objects.get(id=img.id),
141         "form": form,
142         "editable": editable,
143     })
144
145
146 def image_file(request, pk):
147     img = get_object_or_404(Image, pk=pk)
148     return HttpResponseRedirect(img.file.url)
149
150
151 @active_tab('cover')
152 def image_list(request):
153     qs = Image.objects.all().order_by('-id')
154     only_unused = request.GET.get('unused')
155     if only_unused:
156         qs = qs.filter(book=None)
157     return render(request, "cover/image_list.html", {
158         'object_list': qs,
159         'can_add': request.user.has_perm('cover.add_image'),
160         'only_unused': only_unused,
161     })
162
163
164 @permission_required('cover.add_image')
165 @active_tab('cover')
166 def add_image(request):
167     form = ff = None
168     if request.method == 'POST':
169         if request.POST.get('form_id') == 'import':
170             ff = forms.ImportForm(request.POST)
171             if ff.is_valid():
172                 form = forms.ImageAddForm(ff.cleaned_data)
173         else:
174             form = forms.ImageAddForm(request.POST, request.FILES)
175             if form.is_valid():
176                 obj = form.save()
177                 return HttpResponseRedirect(obj.get_absolute_url())
178     if form is None:
179         form = forms.ImageAddForm()
180     if ff is None:
181         ff = forms.ImportForm()
182     return render(request, 'cover/add_image.html', {
183             'form': form,
184             'ff': ff,
185         })
186
187 @permission_required('cover.add_image')
188 def quick_import(request, pk):
189     url = request.POST.get('url')
190     if url.startswith('%s://%s/' % (
191             request.scheme,
192             request.get_host())):
193         cover_id = url.rstrip('/').rsplit('/', 1)[-1]
194         cover = Image.objects.get(pk=cover_id)
195     else:
196         data = get_import_data(url)
197         same = Image.objects.filter(source_url=data['source_url'])
198         if not same.exists():
199             same = Image.objects.filter(download_url=data['download_url'])
200         if same.exists():
201             cover = same.first()
202         else:
203             form = forms.ImageAddForm(data)
204             if form.is_valid():
205                 cover = form.save()
206
207     # We have a cover. Now let's commit.
208     book = Book.objects.get(pk=pk)
209     chunk = book[0]
210     text = chunk.head.materialize()
211
212     root = etree.fromstring(text)
213     rdf = root.find('.//' + RDFNS('Description'))
214     for tag in 'url', 'attribution', 'source':
215         for elem in rdf.findall('.//' + DCNS('relation.coverImage.%s' % tag)):
216             rdf.remove(elem)
217     e = etree.Element(DCNS('relation.coverImage.url'))
218     e.text = request.build_absolute_uri(cover.use_file.url)
219     rdf.append(e)
220     e.tail = '\n    '
221     e = etree.Element(DCNS('relation.coverImage.attribution'))
222     e.text = ''
223     if cover.title:
224         e.text += cover.title + ', '
225     if cover.author:
226         e.text += cover.author + ', '
227     e.text += cover.license_name
228     e.tail = '\n    '
229     rdf.append(e)
230     e = etree.Element(DCNS('relation.coverImage.source'))
231     e.text = cover.get_full_url()
232     e.tail = '\n    '
233     rdf.append(e)
234
235     xml = etree.tostring(root, encoding='unicode')
236     chunk.commit(
237         xml,
238         author=request.user,
239         comment='Cover',
240         publishable=chunk.head.publishable,
241     )
242     return HttpResponseRedirect(book.get_absolute_url())
243