0f341ad7f245676805bad353cad490a0aaffc292
[redakcja.git] / apps / cover / views.py
1 # Create your views here.
2 import os.path
3 from django.conf import settings
4 from django.http import HttpResponse, HttpResponseRedirect, Http404
5 from catalogue.models import Chunk
6 from django.views.decorators.http import require_POST
7 from django.views.decorators.csrf import csrf_exempt
8 from django.shortcuts import render
9
10
11 PREVIEW_SIZE = (216, 300)
12
13
14 def preview(request, book, chunk=None, rev=None):
15     """Creates a cover image.
16
17     If chunk and rev number are given, use version from given revision.
18     If rev is not given, use publishable version.
19     """
20     import Image
21     from librarian.cover import WLCover
22     from librarian.dcparser import BookInfo
23
24     chunk = Chunk.get(book, chunk)
25     if rev is not None:
26         try:
27             revision = chunk.at_revision(rev)
28         except Chunk.change_model.DoesNotExist:
29             raise Http404
30     else:
31         revision = chunk.publishable()
32         if revision is None:
33             raise Http404
34     xml = revision.materialize().encode('utf-8')
35
36     try:
37         info = BookInfo.from_string(xml)
38     except:
39         return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
40     cover = WLCover(info)
41     response = HttpResponse(mimetype=cover.mime_type())
42     image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
43     image.save(response, cover.format)
44     return response
45
46
47 @csrf_exempt
48 @require_POST
49 def preview_from_xml(request):
50     from hashlib import sha1
51     import Image
52     from os import makedirs
53     from lxml import etree
54     from librarian.cover import WLCover
55     from librarian.dcparser import BookInfo
56
57     xml = request.POST['xml']
58     try:
59         info = BookInfo.from_string(xml.encode('utf-8'))
60     except:
61         return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
62     coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
63     cover = WLCover(info)
64
65     cover_dir = 'cover/preview'
66     try:
67         makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
68     except OSError:
69         pass
70     fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
71     image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
72     image.save(os.path.join(settings.MEDIA_ROOT, fname))
73     return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
74
75
76 def flickr(request):
77     url = request.POST.get('flickr_url')
78     if url:
79         import re
80         from urllib2 import urlopen
81
82         html = urlopen(url).read()
83         match = re.search(r'<a href="([^"]*)" rel="license cc:license">Some rights reserved</a>', html)
84         try:
85             assert match
86             license_url = match.group(1)
87             re_license = re.compile(r'http://creativecommons.org/licenses/([^/]*)/([^/]*)/.*')
88             m = re_license.match(license_url)
89             assert m
90             license_name = 'CC %s %s' % (m.group(1).upper(), m.group(2))
91         except AssertionError:
92             license_name = 'NIEZNANA LICENCJA'
93
94         m = re.search(r'<strong class="username">By <a href="[^"]*">([^<]*)</a></strong>', html)
95         if m:
96             author = m.group(1)
97         else:
98             author = "NIEZNANY AUTOR"
99
100         url_size = url.rstrip('/') + '/sizes/o/'
101         html = urlopen(url_size).read()
102         m = re.search(r'<div id="allsizes-photo">\s*<img src="([^"]*)"', html)
103         if m:
104             image_url = m.group(1)
105     else:
106         url = ""
107
108     return render(request, 'cover/flickr.html', locals())