Author description generator
[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
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 = (216, 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     chunk = Chunk.get(book, chunk)
35     if rev is not None:
36         try:
37             revision = chunk.at_revision(rev)
38         except Chunk.change_model.DoesNotExist:
39             raise Http404
40     else:
41         revision = chunk.publishable()
42         if revision is None:
43             revision = chunk.head
44     xml = revision.materialize().encode('utf-8')
45
46     try:
47         info = BookInfo.from_bytes(xml)
48     except Exception as e:
49         print(e)
50         return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
51     width = request.GET.get('width')
52     width = int(width) if width else None
53     height=request.GET.get('height')
54     height = int(height) if height else None
55
56     if not (height or width):
57         width, height = PREVIEW_SIZE
58
59     cover_class = request.GET.get('cover_class', 'default')
60
61     cover = make_cover(info, cover_class=cover_class, width=width, height=height)
62     response = HttpResponse(content_type=cover.mime_type())
63     img = cover.final_image()
64     img.save(response, cover.format)
65
66     if 'download' in request.GET:
67         response['Content-Disposition'] = 'attachment; filename=%s.jpg' % chunk.book.slug
68
69     return response
70
71
72 @csrf_exempt
73 @require_POST
74 def preview_from_xml(request):
75     xml = request.POST['xml']
76     try:
77         info = BookInfo.from_bytes(xml.encode('utf-8'))
78     except Exception as e:
79         print(e)
80         return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
81     coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
82     cover = make_cover(info)
83
84     cover_dir = 'cover/preview'
85     try:
86         makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
87     except OSError:
88         pass
89     fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
90     img = cover.image().resize(PREVIEW_SIZE, PIL.Image.ANTIALIAS)
91     img.save(os.path.join(settings.MEDIA_ROOT, fname))
92     return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
93
94
95 @active_tab('cover')
96 def image(request, pk):
97     img = get_object_or_404(Image, pk=pk)
98
99     if request.user.has_perm('cover.change_image'):
100         if request.method == "POST":
101             form = forms.ImageEditForm(request.POST, request.FILES, instance=img)
102             if form.is_valid():
103                 form.save()
104                 return HttpResponseRedirect(img.get_absolute_url())
105         else:
106             form = forms.ImageEditForm(instance=img)
107         editable = True
108     else:
109         form = forms.ReadonlyImageEditForm(instance=img)
110         editable = False
111
112     return render(request, "cover/image_detail.html", {
113         "object": Image.objects.get(id=img.id),
114         "form": form,
115         "editable": editable,
116     })
117
118
119 def image_file(request, pk):
120     img = get_object_or_404(Image, pk=pk)
121     return HttpResponseRedirect(img.file.url)
122
123
124 @active_tab('cover')
125 def image_list(request):
126     return render(request, "cover/image_list.html", {
127         'object_list': Image.objects.all().order_by('-id'),
128         'can_add': request.user.has_perm('cover.add_image'),
129     })
130
131
132 @permission_required('cover.add_image')
133 @active_tab('cover')
134 def add_image(request):
135     form = ff = None
136     if request.method == 'POST':
137         if request.POST.get('form_id') == 'import':
138             ff = forms.ImportForm(request.POST)
139             if ff.is_valid():
140                 form = forms.ImageAddForm(ff.cleaned_data)
141         else:
142             form = forms.ImageAddForm(request.POST, request.FILES)
143             if form.is_valid():
144                 obj = form.save()
145                 return HttpResponseRedirect(obj.get_absolute_url())
146     if form is None:
147         form = forms.ImageAddForm()
148     if ff is None:
149         ff = forms.ImportForm()
150     return render(request, 'cover/add_image.html', {
151             'form': form,
152             'ff': ff,
153         })
154
155 @permission_required('cover.add_image')
156 def quick_import(request, pk):
157     url = request.POST.get('url')
158     if url.startswith('%s://%s/' % (
159             request.scheme,
160             request.get_host())):
161         cover_id = url.rstrip('/').rsplit('/', 1)[-1]
162         cover = Image.objects.get(pk=cover_id)
163     else:
164         data = get_import_data(url)
165         same = Image.objects.filter(source_url=data['source_url'])
166         if not same.exists():
167             same = Image.objects.filter(download_url=data['download_url'])
168         if same.exists():
169             cover = same.first()
170         else:
171             form = forms.ImageAddForm(data)
172             if form.is_valid():
173                 cover = form.save()
174
175     # We have a cover. Now let's commit.
176     book = Book.objects.get(pk=pk)
177     chunk = book[0]
178     text = chunk.head.materialize()
179
180     root = etree.fromstring(text)
181     rdf = root.find('.//' + RDFNS('Description'))
182     for tag in 'url', 'attribution', 'source':
183         for elem in rdf.findall('.//' + DCNS('relation.coverImage.%s' % tag)):
184             rdf.remove(elem)
185     e = etree.Element(DCNS('relation.coverImage.url'))
186     e.text = request.build_absolute_uri(cover.use_file.url)
187     rdf.append(e)
188     e.tail = '\n    '
189     e = etree.Element(DCNS('relation.coverImage.attribution'))
190     e.text = ''
191     if cover.title:
192         e.text += cover.title + ', '
193     if cover.author:
194         e.text += cover.author + ', '
195     e.text += cover.license_name
196     e.tail = '\n    '
197     rdf.append(e)
198     e = etree.Element(DCNS('relation.coverImage.source'))
199     e.text = cover.get_full_url()
200     e.tail = '\n    '
201     rdf.append(e)
202
203     xml = etree.tostring(root, encoding='unicode')
204     chunk.commit(
205         xml,
206         author=request.user,
207         comment='Cover',
208         publishable=chunk.head.publishable,
209     )
210     return HttpResponseRedirect(book.get_absolute_url())
211