from daisyproducer.documents.external import DaisyPipeline, Liblouis, SBSForm
from daisyproducer.documents.forms import SBSFormForm, RTFForm, XHTMLForm, EPUBForm, TextOnlyFilesetForm
from daisyproducer.documents.models import Document, BrailleProfileForm, LargePrintProfileForm
from daisyproducer.documents.views.utils import render_to_mimetype_response
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.views.generic.list_detail import object_list, object_detail

from os import system
import os
import os.path
import shutil
import tempfile
import zipfile

# browse use case
def index(request):
    """Show all the documents that approved and order them by title"""
    response = object_list(
        request,
        queryset = Document.objects.filter(state__name='approved').order_by('title'),
        template_name = 'documents/browse_index.html'
    )
    return response

def detail(request, document_id):
    response = object_detail(
        request,
        queryset = Document.objects.all(),
        object_id = document_id,
        template_name = 'documents/browse_detail.html',
        extra_context = {
            'lpform' : LargePrintProfileForm(),
            'bform' : BrailleProfileForm(),
            'sform' : SBSFormForm(),
            'xhtmlform' : XHTMLForm(),
            'rtfform' : RTFForm(),
            'textonlyfilesetform' : TextOnlyFilesetForm()}
        )
    return response

def as_pdf(request, document_id):
    form = LargePrintProfileForm(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
    outputFile = "/tmp/%s.pdf" % document_id
    DaisyPipeline.dtbook2pdf(inputFile, outputFile, **form.cleaned_data)

    return render_to_mimetype_response('application/pdf', "%s.pdf" % document.title.encode('utf-8'), outputFile)

def as_brl(request, document_id):
    form = BrailleProfileForm(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
    outputFile = "/tmp/%s.brl" % document_id
    Liblouis.dtbook2brl(inputFile, outputFile, **form.cleaned_data)

    return render_to_mimetype_response('text/plain', "%s.brl" % document.title.encode('utf-8'), outputFile)

def as_sbsform(request, document_id):
    form = SBSFormForm(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
    outputFile = "/tmp/%s.sbsform" % document_id
    SBSForm.dtbook2sbsform(inputFile, outputFile, **form.cleaned_data)

    return render_to_mimetype_response('text/plain', "%s.b" % document.title.encode('utf-8'), outputFile)

def as_xhtml(request, document_id):
    form = XHTMLForm(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
    outputFile = "/tmp/%s.xhtml" % document_id
    DaisyPipeline.dtbook2xhtml(inputFile, outputFile, **form.cleaned_data)

    return render_to_mimetype_response('text/html', "%s.xhtml" % document.title.encode('utf-8'), outputFile)

def as_rtf(request, document_id):
    form = RTFForm(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
    outputFile = "/tmp/%s.rtf" % document_id
    DaisyPipeline.dtbook2rtf(inputFile, outputFile, **form.cleaned_data)

    return render_to_mimetype_response('application/rtf', "%s.rtf" % document.title.encode('utf-8'), outputFile)

def as_epub(request, document_id):
    form = EPUBForm(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
    outputFile = "/tmp/%s.epub" % document_id
    defaults = {
        "dctitle" : document.title, 
        "dcidentifier" : document.identifier, 
        "dclanguage" : document.language, 
        "dccreator" : document.author, 
        "dcpublisher" : document.publisher, 
        "dcdate" : document.date}
    defaults.update(form.cleaned_data)
    DaisyPipeline.dtbook2epub(inputFile, outputFile, **defaults)

    return render_to_mimetype_response('application/epub+zip', "%s.epub" % document.title.encode('utf-8'), outputFile)

def as_text_only_fileset(request, document_id):
    form = TextOnlyFilesetForm(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_fileset(inputFile, outputDir, **form.cleaned_data)

    ignore, outputFileName = tempfile.mkstemp(suffix='zip', prefix=document_id)
    outputFile = zipfile.ZipFile(outputFileName, 'w')
    for filename in os.listdir(outputDir):
        outputFile.write(
            # zipFile support in Python has a few weak spots: Older
            # Pythons die if the filename or the arcname that is
            # passed to ZipFile.write is not in the right encoding
            os.path.join(outputDir, filename).encode("latin-1"), 
            os.path.join(document.title, os.path.basename(filename)).encode("latin-1"))
    outputFile.close()
    shutil.rmtree(outputDir)
    
    return render_to_mimetype_response('application/zip', "%s.zip" % document.title.encode('utf-8'), outputFileName)

@@ -5863,16 +5863,116 @@
 ncoding%0A
+            # FIXME: remove the encode(%22latin-1%22) workaround once we%0A            # upgrade to 2.6.2%0A
         

Add a FIXME: for the workaround for issue 1734346