1 # Create your views here.
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
11 PREVIEW_SIZE = (216, 300)
14 def preview(request, book, chunk=None, rev=None):
15 """Creates a cover image.
17 If chunk and rev number are given, use version from given revision.
18 If rev is not given, use publishable version.
21 from librarian.cover import WLCover
22 from librarian.dcparser import BookInfo
24 chunk = Chunk.get(book, chunk)
27 revision = chunk.at_revision(rev)
28 except Chunk.change_model.DoesNotExist:
31 revision = chunk.publishable()
34 xml = revision.materialize().encode('utf-8')
37 info = BookInfo.from_string(xml)
39 return HttpResponseRedirect(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
41 response = HttpResponse(mimetype=cover.mime_type())
42 image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
43 image.save(response, cover.format)
49 def preview_from_xml(request):
50 from hashlib import sha1
52 from os import makedirs
53 from lxml import etree
54 from librarian.cover import WLCover
55 from librarian.dcparser import BookInfo
57 xml = request.POST['xml']
59 info = BookInfo.from_string(xml.encode('utf-8'))
61 return HttpResponse(os.path.join(settings.STATIC_URL, "img/sample_cover.png"))
62 coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
65 cover_dir = 'cover/preview'
67 makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
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))
77 url = request.POST.get('flickr_url')
80 from urllib2 import urlopen
82 html = urlopen(url).read()
83 match = re.search(r'<a href="([^"]*)" rel="license cc:license">Some rights reserved</a>', html)
86 license_url = match.group(1)
87 re_license = re.compile(r'http://creativecommons.org/licenses/([^/]*)/([^/]*)/.*')
88 m = re_license.match(license_url)
90 license_name = 'CC %s %s' % (m.group(1).upper(), m.group(2))
91 except AssertionError:
92 license_name = 'NIEZNANA LICENCJA'
94 m = re.search(r'<strong class="username">By <a href="[^"]*">([^<]*)</a></strong>', html)
98 author = "NIEZNANY AUTOR"
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)
104 image_url = m.group(1)
108 return render(request, 'cover/flickr.html', locals())