Ejemplo n.º 1
0
def as_text_only_dtb(request, document_id):
    form = TextOnlyDTBForm(request.POST)

    if not form.is_valid():
        return HttpResponseRedirect(reverse('browse_detail', args=[document_id]))

    document = Document.objects.get(pk=document_id)
    inputFile = document.latest_version().content.path
    outputDir = tempfile.mkdtemp(prefix="daisyproducer-")

    DaisyPipeline.dtbook2text_only_dtb(inputFile, outputDir, **form.cleaned_data)

    zipFile = tempfile.NamedTemporaryFile(suffix='.zip', prefix=document_id, delete=False)
    zipFile.close() # we are only interested in a unique filename
    zipDirectory(outputDir, zipFile.name, document.title)
    shutil.rmtree(outputDir)
    
    return render_to_mimetype_response('application/zip', document.title.encode('utf-8'), zipFile.name)
Ejemplo n.º 2
0
def preview_text_only_dtb(request, document_id):
    document = get_object_or_404(Document, pk=document_id)

    if request.method == 'POST':
        form = TextOnlyDTBForm(request.POST)
        if form.is_valid():
            inputFile = document.latest_version().content.path
            outputDir = tempfile.mkdtemp(prefix="daisyproducer-")
            
            ebookNumber = form.cleaned_data.pop('ebookNumber')

            transformationErrors = DaisyPipeline.dtbook2text_only_dtb(
                inputFile, outputDir,
                images=document.image_set.all(), **form.cleaned_data)
            if transformationErrors:
                return render_to_response('documents/todo_text_only_dtb.html', locals(),
                                          context_instance=RequestContext(request))

            zipFile = tempfile.NamedTemporaryFile(suffix='.zip', prefix=document_id, delete=False)
            zipFile.close() # we are only interested in a unique filename
            zipDirectory(outputDir, zipFile.name, ebookNumber)
            shutil.rmtree(outputDir)

            # put a copy of the ebook to a shared folder where it is fetched by another process that
            # puts into the distribution system. This will change, as the distribution system should
            # fetch the ebook directly from the archive. See fhs for a rationale about the dest
            # folder (http://www.pathname.com/fhs/pub/fhs-2.3.html#VARSPOOLAPPLICATIONSPOOLDATA)
            shutil.copy2(zipFile.name, os.path.join('/var/spool/daisyproducer', ebookNumber + '.zip'))
    
            return render_to_mimetype_response('application/zip', 
                                               document.title.encode('utf-8'), zipFile.name)
    else:
        ebook = Product.objects.filter(document=document, type=2)
        if ebook:
            form = TextOnlyDTBForm({'ebookNumber': ebook[0].identifier})
        else:
            form = TextOnlyDTBForm()

    return render_to_response('documents/todo_text_only_dtb.html', locals(),
                              context_instance=RequestContext(request))