| author | gibus |
| Mon, 06 Aug 2012 13:42:15 +0200 | |
| changeset 454 | b7a092a52eae |
| parent 453 | 1d314f629611 |
| child 455 | 33c7e20efcb7 |
| permissions | -rw-r--r-- |
| 0 | 1 |
from django import forms |
2 |
from django.core.urlresolvers import reverse |
|
3 |
from django.http import HttpResponse, HttpResponseRedirect, Http404 |
|
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 | 6 |
from django.template import RequestContext |
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 | 9 |
from cm.converters.pandoc_converters import pandoc_convert, do_tidy |
| 0 | 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 | 12 |
import mimetypes |
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 | 15 |
|
| 0 | 16 |
EXPORT2_INFOS = { |
17 |
# key -> { mimetype, extension} |
|
18 |
's5' : {}, |
|
19 |
'pdf' : {'mimetype': 'application/pdf', 'extension':'pdf'}, |
|
20 |
'markdown' : {'mimetype': 'text/plain', 'extension':'mkd'}, |
|
21 |
'odt' : {'mimetype': 'application/vnd.oasis.opendocument.text', 'extension':'odt'}, |
|
| 433 | 22 |
'doc' : {'mimetype': 'application/msword', 'extension':'doc'}, |
23 |
'docx' : {'mimetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'extension':'docx'}, |
|
| 0 | 24 |
'latex' :{'mimetype': 'text/x-tex', 'extension':'tex'}, |
25 |
'html' :{'mimetype': 'text/html', 'extension':'html'}, |
|
| 443 | 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 | 29 |
} |
| 454 | 30 |
|
31 |
HTML_HEADER = u""" |
|
32 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
33 |
<html><head> |
|
34 |
<STYLE TYPE='text/css'> |
|
35 |
div.pagebreakhere { |
|
36 |
page-break-before: always ; |
|
37 |
} |
|
38 |
</STYLE> |
|
39 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head> |
|
40 |
<body>%s</body> |
|
41 |
</html> |
|
42 |
""" |
|
43 |
||
| 0 | 44 |
def content_export2(request, content, title, content_format, format, use_pandoc, download_response): |
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 | 47 |
if format == 'raw' : |
48 |
export_content = content |
|
49 |
elif content_format == 'html' and format == 'html': |
|
50 |
export_content = HTML_HEADER % content |
|
51 |
elif content_format == 'markdown' and format == 'markdown': |
|
52 |
export_content = content |
|
53 |
else: |
|
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 | 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 | 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 | 63 |
export_content = pandoc_convert(content, content_format, format, full=True) |
64 |
else : |
|
65 |
fix_content = content |
|
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 | 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 | 93 |
|
94 |
export_infos = EXPORT2_INFOS[format] |
|
95 |
||
96 |
if download_response: |
|
97 |
return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ; |
|
98 |
else: |
|
99 |
return _response_write(export_content) |
|
100 |
||
101 |
||
102 |
def _response_download(content, title, mimetype, extension): |
|
103 |
response = HttpResponse(mimetype=mimetype) |
|
104 |
file_title = title + '.' + extension |
|
105 |
from email.header import Header |
|
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 | 110 |
response.write(content) |
111 |
return response |
|
112 |
||
113 |
def _response_write(content): |
|
114 |
response = HttpResponse() |
|
115 |
response.write(content) |
|
116 |
return response |
|
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 | 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']) ; |