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.
6 from django.conf import settings
7 from documents.models import Book
10 def gather_files(book, publishable=True):
15 xml = book.materialize(publishable=publishable)
17 'doc' if b == book else 'part',
18 (b.dc_slug + '.xml', xml, 'application/xml')
20 for part in b.wldocument(publishable=publishable).book_info.parts:
21 todo.push(Book.objects.get(dc_slug=part.slug))
25 def remote_build(book, fmt, publishable=True, options=None, content=None):
26 if not settings.REMOTE_BUILD_URL or not settings.REMOTE_BUILD_TOKEN:
27 raise RuntimeError('Remote build not configured')
31 files = [('doc', ('doc.xml', content, 'application/xml'))]
33 files = gather_files(book, publishable=publishable)
36 options = json.dumps(options)
37 response = requests.post(
38 settings.REMOTE_BUILD_URL + '/build/pdf',
39 headers={'Authorization': 'Bearer ' + settings.REMOTE_BUILD_TOKEN},
41 data={'options': options}
43 if response.status_code != 200:
45 error = response.json()
47 error = {'error': response.text}
48 message = '%d: %s' % (response.status_code, error.get('error'))
50 message += '\n' + error['log']
51 raise RuntimeError(message)
52 return response.content