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