src/cm/views/export.py
author gibus
Mon, 06 Aug 2012 13:42:15 +0200
changeset 454 b7a092a52eae
parent 453 1d314f629611
child 455 33c7e20efcb7
permissions -rw-r--r--
Cleaned export.
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
453
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
     5
from django.template.loader import render_to_string
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     6
from django.template import RequestContext
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
     7
from django.utils.translation import ugettext as _, ugettext_lazy
453
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
     8
from django.contrib.auth.models import User
364
41dd28557b5d tidyfy html before conversion with abiword
gibus
parents: 363
diff changeset
     9
from cm.converters.pandoc_converters import pandoc_convert, do_tidy
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    10
from cm.models import Text, TextVersion, Attachment, Comment
453
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
    11
from cm.security import get_viewable_comments
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    12
import mimetypes
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    13
import simplejson
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    14
from cm.cm_settings import USE_ABI
454
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    15
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    16
EXPORT2_INFOS = {
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    17
# key -> { mimetype, extension}
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    18
's5' :   {},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    19
'pdf' :  {'mimetype': 'application/pdf', 'extension':'pdf'},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    20
'markdown' :  {'mimetype': 'text/plain', 'extension':'mkd'},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    21
'odt' :  {'mimetype': 'application/vnd.oasis.opendocument.text', 'extension':'odt'},
433
056d92bffb23 Added export in .doc and .docx formats.
gibus
parents: 367
diff changeset
    22
'doc' :  {'mimetype': 'application/msword', 'extension':'doc'},
056d92bffb23 Added export in .doc and .docx formats.
gibus
parents: 367
diff changeset
    23
'docx' :  {'mimetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'extension':'docx'},
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    24
'latex' :{'mimetype': 'text/x-tex', 'extension':'tex'},
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    25
'html' :{'mimetype': 'text/html', 'extension':'html'},
443
cacd524f5279 Adds export to epub.
gibus
parents: 441
diff changeset
    26
'epub' :{'mimetype': 'application/epub+zip', 'extension':'epub'},
453
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
    27
'raw' : {'mimetype': 'text/plain', 'extension':'txt'},
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
    28
'xml' : {'mimetype': 'text/xml', 'extension':'xml'},
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    29
}
454
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    30
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    31
HTML_HEADER = u"""
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    32
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    33
<html><head>
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    34
<STYLE TYPE='text/css'>
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    35
div.pagebreakhere {
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    36
    page-break-before: always ;
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    37
}
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    38
</STYLE>
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    39
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    40
<body>%s</body>
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    41
</html>
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    42
"""
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    43
 
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    44
def content_export2(request, content, title, content_format, format, use_pandoc, download_response):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    45
    # 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
    46
#    import pdb;pdb.set_trace()
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    47
    if format == 'raw' :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    48
        export_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    49
    elif content_format == 'html' and format == 'html':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    50
        export_content = HTML_HEADER % content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    51
    elif content_format == 'markdown' and format == 'markdown':
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    52
        export_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    53
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    54
        if use_pandoc :
367
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    55
          # markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
454
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    56
          if format in ('pdf', 'odt', 'docx', 'doc') and USE_ABI:
367
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    57
            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
    58
            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
    59
            converter = AbiFileConverter()
454
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    60
            fix_content = converter.add_html_header(html_content)
367
e4a0c2fe8df2 markdown2pdf is buggy => convert to HTML and use abiword to export in PDF
gibus
parents: 364
diff changeset
    61
            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
    62
          else:
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    63
            export_content = pandoc_convert(content, content_format, format, full=True)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    64
        else :
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    65
            fix_content = content
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    66
            if content_format == 'html':
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    67
                if USE_ABI:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    68
                  from cm.converters.abi_converters import AbiFileConverter
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    69
                  converter = AbiFileConverter()
454
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
    70
                  fix_content = converter.add_html_header(content)
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    71
                else:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    72
                  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
    73
                  fix_content = combine_css_body(content, '')
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    74
            if USE_ABI:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    75
              from cm.converters.abi_converters import AbiFileConverter
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    76
              converter = AbiFileConverter()
441
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    77
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    78
              # 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
    79
              from django.conf import settings
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    80
              site_url = settings.SITE_URL
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    81
              import re
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    82
              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
    83
              attach_str = r'%s/attach/%s/'
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    84
              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
    85
                link = attach_str %(site_url, match)
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    86
                attach = Attachment.objects.get(key=match)
d5d3bcd26a0b Fixes export with abiword when attached images are served by co-ment
gibus
parents: 433
diff changeset
    87
                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
    88
360
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    89
              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
    90
            else:
bfaab8740995 Add abiword as an alternative to open office for conversions
gibus
parents: 77
diff changeset
    91
              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
    92
              export_content = oo_convert(fix_content, format)
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    93
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    94
    export_infos = EXPORT2_INFOS[format]
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    95
     
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    96
    if download_response:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    97
        return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ;
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    98
    else:
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
    99
        return _response_write(export_content)    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   100
    
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   101
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   102
def _response_download(content, title, mimetype, extension):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   103
    response = HttpResponse(mimetype=mimetype)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   104
    file_title = title + '.' + extension
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   105
    from email.header import Header
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   106
    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
   107
    # 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
   108
    encoded_name = encoded_name.replace('\n','')
9fa013909d9a prevent header error if newline in header (pb not fully solved)
raph
parents: 0
diff changeset
   109
    response['Content-Disposition'] = 'attachment; filename=%s' % encoded_name
0
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   110
    response.write(content)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   111
    return response        
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   112
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   113
def _response_write(content):
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   114
    response = HttpResponse()
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   115
    response.write(content)
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   116
    return response
40c8f766c9b8 import from internal svn r 4007
raph
parents:
diff changeset
   117
453
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   118
def xml_export(request, text_version, whichcomments):
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   119
  # Text version infos
454
b7a092a52eae Cleaned export.
gibus
parents: 453
diff changeset
   120
  template_dict = { 'title': text_version.title, 'date': text_version.modified, 'format': text_version.format, 'content': text_version.content, 'tags': text_version.tags, }
453
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   121
  
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   122
  # Comments
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   123
  comments = [] # whichcomments=="none"
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   124
  if whichcomments == "filtered" or whichcomments == "all":
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   125
    _comments = text_version.comment_set.all()
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   126
    if whichcomments == "filtered" :
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   127
      filteredIds = []
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   128
      if request.method == 'POST' :
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   129
        ll = request.POST.get('filteredIds',[]).split(",")
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   130
        filteredIds = [ int(l) for l in ll if l]
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   131
      _comments = text_version.comment_set.filter(id__in=filteredIds)
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   132
    comments = get_viewable_comments(request, _comments, text_version, order_by=('start_wrapper','start_offset','end_wrapper','end_offset'))
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   133
    # Add user name/email if missing comment name/email
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   134
    for comment in comments:
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   135
      users = User.objects.filter(id=comment.user_id)
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   136
      if not(comment.name):
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   137
        comment.name = users[0].username
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   138
      if not(comment.email):
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   139
        comment.email = users[0].email
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   140
      
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   141
    template_dict['comments'] = comments
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   142
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   143
  # Author
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   144
  users = User.objects.filter(id=text_version.user_id)
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   145
  if text_version.name:
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   146
    template_dict['name'] = text_version.name
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   147
  else:
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   148
    template_dict['name'] = users[0].username
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   149
  if text_version.email:
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   150
    template_dict['email'] = text_version.email
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   151
  else:
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   152
    template_dict['email'] = users[0].email
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   153
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   154
  # Renders template
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   155
  export_content = render_to_string('site/export.xml', template_dict, context_instance=RequestContext(request))
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   156
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   157
  # Returns HTTP response
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   158
  export_infos = EXPORT2_INFOS['xml']
1d314f629611 Added export to XML for re-import (nb. without attachements).
gibus
parents: 443
diff changeset
   159
  return _response_download(export_content, text_version.title, export_infos['mimetype'], export_infos['extension']) ;