src/cm/views/export.py
author gibus
Thu, 24 May 2012 14:42:22 +0200
changeset 443 cacd524f5279
parent 441 d5d3bcd26a0b
child 453 1d314f629611
permissions -rw-r--r--
Adds export to epub.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     1
from django import forms
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     2
from django.core.urlresolvers import reverse
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     3
from django.http import HttpResponse, HttpResponseRedirect, Http404
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     4
from django.shortcuts import render_to_response
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     5
from django.template import RequestContext
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     6
from django.utils.translation import ugettext as _, ugettext_lazy
364
41dd28557b5d tidyfy html before conversion with abiword
gibus
parents: 363
diff changeset
     7
from cm.converters.pandoc_converters import pandoc_convert, do_tidy
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     8
from cm.models import Text, TextVersion, Attachment, Comment
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     9
import mimetypes
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    10
import simplejson
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    11
from cm.cm_settings import USE_ABI
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    12
EXPORT2_INFOS = {
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    13
# key -> { mimetype, extension}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    14
's5' :   {},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    15
'pdf' :  {'mimetype': 'application/pdf', 'extension':'pdf'},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    16
'markdown' :  {'mimetype': 'text/plain', 'extension':'mkd'},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    17
'odt' :  {'mimetype': 'application/vnd.oasis.opendocument.text', 'extension':'odt'},
433
056d92bffb23 Added export in .doc and .docx formats.
gibus
parents: 367
diff changeset
    18
'doc' :  {'mimetype': 'application/msword', 'extension':'doc'},
056d92bffb23 Added export in .doc and .docx formats.
gibus
parents: 367
diff changeset
    19
'docx' :  {'mimetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'extension':'docx'},
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    20
'latex' :{'mimetype': 'text/x-tex', 'extension':'tex'},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    21
'html' :{'mimetype': 'text/html', 'extension':'html'},
443
cacd524f5279 Adds export to epub.
gibus
parents: 441
diff changeset
    22
'epub' :{'mimetype': 'application/epub+zip', 'extension':'epub'},
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    23
# raw export
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    24
'raw' : {'mimetype': 'text/plain', 'extension':'txt'}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    25
}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    26
def content_export2(request, content, title, content_format, format, use_pandoc, download_response):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    27
    # TODO : formats must be imported from converters
56
bd8a4ffc7dad BUG FIX : pdf export PhA tests, TODO fix special html caracters in comments
reno
parents: 0
diff changeset
    28
#    import pdb;pdb.set_trace()
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    29
    if format == 'raw' :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    30
        export_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    31
    elif content_format == 'html' and format == 'html':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    32
        export_content = HTML_HEADER % content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    33
    elif content_format == 'markdown' and format == 'markdown':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    34
        export_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    35
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    36
        if use_pandoc :
367
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    37
          # markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    38
          if format == 'pdf' and USE_ABI:
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    39
            html_content = pandoc_convert(content, content_format, 'html', full=True)
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    40
            from cm.converters.abi_converters import AbiFileConverter
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    41
            converter = AbiFileConverter()
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    42
            full_content = converter.add_html_header(html_content)
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    43
            fix_content = do_tidy(full_content)
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    44
            export_content = converter.convert_from_html(fix_content, format)
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    45
          else:
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    46
            export_content = pandoc_convert(content, content_format, format, full=True)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    47
        else :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    48
            fix_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    49
            if content_format == 'html':
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    50
                if USE_ABI:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    51
                  from cm.converters.abi_converters import AbiFileConverter
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    52
                  converter = AbiFileConverter()
364
41dd28557b5d tidyfy html before conversion with abiword
gibus
parents: 363
diff changeset
    53
                  full_content = converter.add_html_header(content)
41dd28557b5d tidyfy html before conversion with abiword
gibus
parents: 363
diff changeset
    54
                  fix_content = do_tidy(full_content)
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    55
                else:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    56
                  from cm.converters.oo_converters import combine_css_body                
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    57
                  fix_content = combine_css_body(content, '')
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    58
            if USE_ABI:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    59
              from cm.converters.abi_converters import AbiFileConverter
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    60
              converter = AbiFileConverter()
441
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    61
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    62
              # replaces images url by their actual path
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    63
              from django.conf import settings
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    64
              site_url = settings.SITE_URL
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    65
              import re
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    66
              attach_re = r'/attach/(?P<attach_key>\w*)/'
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    67
              attach_str = r'%s/attach/%s/'
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    68
              for match in re.findall(attach_re, fix_content):
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    69
                link = attach_str %(site_url, match)
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    70
                attach = Attachment.objects.get(key=match)
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    71
                fix_content = fix_content.replace(link, attach.data.path)
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    72
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    73
              export_content = converter.convert_from_html(fix_content, format)
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    74
            else:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    75
              from cm.converters.oo_converters import convert_html as oo_convert                
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    76
              export_content = oo_convert(fix_content, format)
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    77
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    78
    export_infos = EXPORT2_INFOS[format]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    79
     
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    80
    if download_response:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    81
        return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ;
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    82
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    83
        return _response_write(export_content)    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    84
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    85
def content_export_new(request, content, title, src_format, format, use_pandoc, download_response):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    86
    # TODO : formats must be imported from converters
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    87
    if format == 'raw' :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    88
        export_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    89
    elif src_format == format and format == 'html':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    90
        export_content = HTML_HEADER % content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    91
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    92
        if use_pandoc :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    93
            export_content = pandoc_convert(content, src_format, format, full=True)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    94
        else :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    95
            fix_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    96
            if src_format == 'html':
77
fe91eb717a96 import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents: 57
diff changeset
    97
                from cm.converters.oo_converters import combine_css_body                
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    98
                fix_content = combine_css_body(content, '')
77
fe91eb717a96 import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents: 57
diff changeset
    99
            from cm.converters.oo_converters import convert_html as oo_convert                
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   100
            export_content = oo_convert(fix_content, format)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   101
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   102
    export_infos = EXPORT_INFOS[format]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   103
    format_download = export_infos[0] 
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   104
     
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   105
    if download_response:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   106
        return _response_download(export_content, export_infos[1], export_infos[2]) ;
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   107
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   108
        return _response_write(export_content)    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   109
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   110
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   111
# read conversion chain 
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   112
# execute chain 
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   113
# ready to send response
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   114
#    # TODO : formats must be imported from converters
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   115
#    if format == 'raw' :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   116
#        export_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   117
#    elif src_format == format and format == 'html':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   118
#        export_content = HTML_HEADER % content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   119
#    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   120
#        if use_pandoc :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   121
#            export_content = pandoc_convert(content, src_format, format, full=True)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   122
#        else :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   123
#            fix_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   124
#            if src_format == 'html':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   125
#                fix_content = combine_css_body(content, '')
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   126
#            export_content = oo_convert(fix_content, format)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   127
#    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   128
## send response
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   129
#    export_infos = EXPORT_INFOS[format]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   130
#    mimetype = export_infos['mimetype']
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   131
#    extension = export_infos['extension']
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   132
#    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   133
#    if download:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   134
#        return _response_download(export_content, mimetype, extension)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   135
#    else :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   136
#        return _response_write(export_content)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   137
#
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   138
def _response_download(content, title, mimetype, extension):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   139
    response = HttpResponse(mimetype=mimetype)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   140
    file_title = title + '.' + extension
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   141
    from email.header import Header
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   142
    encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500))
52
9fa013909d9a prevent header error if newline in header (pb not fully solved)
raph
parents: 0
diff changeset
   143
    # TODO: find a way to include long (more than 76 chars) into header
9fa013909d9a prevent header error if newline in header (pb not fully solved)
raph
parents: 0
diff changeset
   144
    encoded_name = encoded_name.replace('\n','')
9fa013909d9a prevent header error if newline in header (pb not fully solved)
raph
parents: 0
diff changeset
   145
    response['Content-Disposition'] = 'attachment; filename=%s' % encoded_name
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   146
    response.write(content)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   147
    return response        
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   148
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   149
def _response_write(content):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   150
    response = HttpResponse()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   151
    response.write(content)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   152
    return response
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   153
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   154
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   155
EXPORT_INFOS = {
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   156
# key -> [ download?, mimetype, extension]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   157
's5' :   [False , ],
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   158
'pdf' :  [True , 'application/pdf' , 'pdf'],
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   159
'markdown' :  [True , 'text/plain' , 'mkd'],
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   160
'odt' :  [True , 'application/vnd.oasis.opendocument.text', 'odt'],
433
056d92bffb23 Added export in .doc and .docx formats.
gibus
parents: 367
diff changeset
   161
'doc' :  [True , 'application/msword', 'odt'],
056d92bffb23 Added export in .doc and .docx formats.
gibus
parents: 367
diff changeset
   162
'docx' :  [True , 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'docx'],
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   163
'latex' :[True , 'text/x-tex', 'tex'],
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   164
'html' :[True , 'text/html', 'html'],
443
cacd524f5279 Adds export to epub.
gibus
parents: 441
diff changeset
   165
'epub' :[True , 'application/epub+zip', 'epub'],
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   166
# raw export
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   167
'raw' : [True, 'text/plain', 'txt']
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   168
}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   169
HTML_HEADER = u"""
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   170
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   171
<html><head>
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   172
<STYLE TYPE='text/css'>
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   173
div.pagebreakhere {
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   174
    page-break-before: always ;
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   175
}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   176
</STYLE>
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   177
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   178
<body>%s</body>
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   179
</html>
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   180
"""
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   181
EXPORT_FORMATS = EXPORT_INFOS.keys()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   182
 
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   183
def content_export(request, content, title, src_format, format, use_pandoc):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   184
    # TODO : formats must be imported from converters
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   185
    if format == 'raw' :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   186
        export_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   187
    elif src_format == format and format == 'html':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   188
        export_content = HTML_HEADER % content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   189
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   190
        if use_pandoc :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   191
            export_content = pandoc_convert(content, src_format, format, full=True)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   192
        else :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   193
            fix_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   194
            if src_format == 'html':
77
fe91eb717a96 import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents: 57
diff changeset
   195
                from cm.converters.oo_converters import combine_css_body                
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   196
                fix_content = combine_css_body(content, '')
77
fe91eb717a96 import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents: 57
diff changeset
   197
            from cm.converters.oo_converters import convert_html as oo_convert                
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   198
            export_content = oo_convert(fix_content, format)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   199
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   200
    export_infos = EXPORT_INFOS[format]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   201
    format_download = export_infos[0] 
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   202
     
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   203
    if format_download:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   204
        format_mimetype = export_infos[1]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   205
        format_extension = export_infos[2]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   206
        
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   207
        response = HttpResponse(mimetype=format_mimetype)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   208
        file_title = title + '.' + format_extension
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   209
        from email.header import Header
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   210
        encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   211
        response['Content-Disposition'] = u'attachment; filename=%s' % encoded_name
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   212
        response.write(export_content)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   213
        return response        
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   214
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   215
        response = HttpResponse()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   216
        response.write(export_content)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   217
        return response    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   218
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   219
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   220
def text_export(request, key, format):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   221
    # TODO : formats must be imported from converters
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   222
    format = format.lower()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   223
    if format not in EXPORT_FORMATS:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   224
        raise Exception("Unsupported format %s (supported formats %s)" % (format, ' '.join(EXPORT_FORMATS)))
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   225
    text = Text.objects.get(key=key)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   226
    text_version = text.get_latest_version()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   227
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   228
    return content_export(request, text_version.content, text_version.title, text_version.format, format)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   229
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   230
def text_feed(request, key):
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
   231
    return ""