1 # Create your views here.
3 from django.conf import settings
4 from django.http import HttpResponse, 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)
13 def preview(request, book, chunk=None, rev=None):
14 """Creates a cover image.
16 If chunk and rev number are given, use version from given revision.
17 If rev is not given, use publishable version.
20 from librarian.cover import WLCover
21 from librarian.dcparser import BookInfo
23 chunk = Chunk.get(book, chunk)
26 revision = chunk.at_revision(rev)
27 except Chunk.change_model.DoesNotExist:
30 revision = chunk.publishable()
33 xml = revision.materialize().encode('utf-8')
35 info = BookInfo.from_string(xml)
37 response = HttpResponse(mimetype=cover.mime_type())
38 image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
39 image.save(response, cover.format)
45 def preview_from_xml(request):
46 from hashlib import sha1
48 from os import makedirs
49 from lxml import etree
50 from librarian.cover import WLCover
51 from librarian.dcparser import BookInfo
53 xml = request.POST['xml']
54 info = BookInfo.from_string(xml.encode('utf-8'))
55 coverid = sha1(etree.tostring(info.to_etree())).hexdigest()
58 cover_dir = 'cover/preview'
60 makedirs(os.path.join(settings.MEDIA_ROOT, cover_dir))
63 fname = os.path.join(cover_dir, "%s.%s" % (coverid, cover.ext()))
64 image = cover.image().resize(PREVIEW_SIZE, Image.ANTIALIAS)
65 image.save(os.path.join(settings.MEDIA_ROOT, fname))
66 return HttpResponse(os.path.join(settings.MEDIA_URL, fname))
70 url = request.POST.get('flickr_url')
73 from urllib2 import urlopen
75 html = urlopen(url).read()
76 match = re.search(r'<a href="([^"]*)" rel="license cc:license">Some rights reserved</a>', html)
78 license_url = match.group(1)
80 re_license = re.compile(r'http://creativecommons.org/licenses/([^/]*)/([^/]*)/.*')
81 m = re_license.match(license_url)
83 license_name = 'CC %s %s' % (m.group(1).upper(), m.group(2))
85 m = re.search(r'<strong class="username">By <a href="[^"]*">([^<]*)</a></strong>', html)
89 url_size = url.rstrip('/') + '/sizes/o/'
90 html = urlopen(url_size).read()
91 m = re.search(r'<div id="allsizes-photo">\s*<img src="([^"]*)"', html)
93 image_url = m.group(1)
97 return render(request, 'cover/flickr.html', locals())