+
+@permission_required('cover.add_image')
+def quick_import(request, pk):
+ url = request.POST.get('url')
+ if url.startswith('%s://%s/' % (
+ request.scheme,
+ request.get_host())):
+ cover_id = url.rstrip('/').rsplit('/', 1)[-1]
+ cover = Image.objects.get(pk=cover_id)
+ else:
+ data = get_import_data(url)
+ same = Image.objects.filter(source_url=data['source_url'])
+ if not same.exists():
+ same = Image.objects.filter(download_url=data['download_url'])
+ if same.exists():
+ cover = same.first()
+ else:
+ form = forms.ImageAddForm(data)
+ if form.is_valid():
+ cover = form.save()
+
+ # We have a cover. Now let's commit.
+ book = Book.objects.get(pk=pk)
+ chunk = book[0]
+ text = chunk.head.materialize()
+
+ root = etree.fromstring(text)
+ rdf = root.find('.//' + RDFNS('Description'))
+ for tag in 'url', 'attribution', 'source':
+ for elem in rdf.findall('.//' + DCNS('relation.coverImage.%s' % tag)):
+ rdf.remove(elem)
+ e = etree.Element(DCNS('relation.coverImage.url'))
+ e.text = request.build_absolute_uri(cover.use_file.url)
+ rdf.append(e)
+ e.tail = '\n '
+ e = etree.Element(DCNS('relation.coverImage.attribution'))
+ e.text = ''
+ if cover.title:
+ e.text += cover.title + ', '
+ if cover.author:
+ e.text += cover.author + ', '
+ e.text += cover.license_name
+ e.tail = '\n '
+ rdf.append(e)
+ e = etree.Element(DCNS('relation.coverImage.source'))
+ e.text = cover.get_full_url()
+ e.tail = '\n '
+ rdf.append(e)
+
+ xml = etree.tostring(root, encoding='unicode')
+ chunk.commit(
+ xml,
+ author=request.user,
+ comment='Cover',
+ publishable=chunk.head.publishable,
+ )
+ return HttpResponseRedirect(book.get_absolute_url())
+