| author | gibus |
| Thu, 21 Jul 2011 12:29:45 +0200 | |
| changeset 363 | b3424ca5b836 |
| parent 362 | afb7dc8758f5 |
| child 364 | 41dd28557b5d |
| 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 |
|
5 |
from django.template import RequestContext |
|
6 |
from django.utils.translation import ugettext as _, ugettext_lazy |
|
7 |
from cm.converters.pandoc_converters import pandoc_convert |
|
8 |
from cm.models import Text, TextVersion, Attachment, Comment |
|
9 |
import mimetypes |
|
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 | 12 |
EXPORT2_INFOS = { |
13 |
# key -> { mimetype, extension} |
|
14 |
's5' : {}, |
|
15 |
'pdf' : {'mimetype': 'application/pdf', 'extension':'pdf'}, |
|
16 |
'markdown' : {'mimetype': 'text/plain', 'extension':'mkd'}, |
|
17 |
'odt' : {'mimetype': 'application/vnd.oasis.opendocument.text', 'extension':'odt'}, |
|
18 |
'latex' :{'mimetype': 'text/x-tex', 'extension':'tex'}, |
|
19 |
'html' :{'mimetype': 'text/html', 'extension':'html'}, |
|
20 |
# raw export |
|
21 |
'raw' : {'mimetype': 'text/plain', 'extension':'txt'} |
|
22 |
} |
|
23 |
def content_export2(request, content, title, content_format, format, use_pandoc, download_response): |
|
24 |
# 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
|
25 |
# import pdb;pdb.set_trace() |
| 0 | 26 |
if format == 'raw' : |
27 |
export_content = content |
|
28 |
elif content_format == 'html' and format == 'html': |
|
29 |
export_content = HTML_HEADER % content |
|
30 |
elif content_format == 'markdown' and format == 'markdown': |
|
31 |
export_content = content |
|
32 |
else: |
|
33 |
if use_pandoc : |
|
34 |
export_content = pandoc_convert(content, content_format, format, full=True) |
|
35 |
else : |
|
36 |
fix_content = content |
|
37 |
if content_format == 'html': |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
38 |
if USE_ABI: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
39 |
from cm.converters.abi_converters import AbiFileConverter |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
40 |
converter = AbiFileConverter() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
41 |
fix_content = converter.add_html_header(content) |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
42 |
else: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
43 |
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
|
44 |
fix_content = combine_css_body(content, '') |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
45 |
if USE_ABI: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
46 |
from cm.converters.abi_converters import AbiFileConverter |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
47 |
converter = AbiFileConverter() |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
48 |
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
|
49 |
else: |
|
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
50 |
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
|
51 |
export_content = oo_convert(fix_content, format) |
| 0 | 52 |
|
53 |
export_infos = EXPORT2_INFOS[format] |
|
54 |
||
55 |
if download_response: |
|
56 |
return _response_download(export_content, title, export_infos['mimetype'], export_infos['extension']) ; |
|
57 |
else: |
|
58 |
return _response_write(export_content) |
|
59 |
||
60 |
def content_export_new(request, content, title, src_format, format, use_pandoc, download_response): |
|
61 |
# TODO : formats must be imported from converters |
|
62 |
if format == 'raw' : |
|
63 |
export_content = content |
|
64 |
elif src_format == format and format == 'html': |
|
65 |
export_content = HTML_HEADER % content |
|
66 |
else: |
|
67 |
if use_pandoc : |
|
68 |
export_content = pandoc_convert(content, src_format, format, full=True) |
|
69 |
else : |
|
70 |
fix_content = content |
|
71 |
if src_format == 'html': |
|
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
57
diff
changeset
|
72 |
from cm.converters.oo_converters import combine_css_body |
| 0 | 73 |
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
|
74 |
from cm.converters.oo_converters import convert_html as oo_convert |
| 0 | 75 |
export_content = oo_convert(fix_content, format) |
76 |
||
77 |
export_infos = EXPORT_INFOS[format] |
|
78 |
format_download = export_infos[0] |
|
79 |
||
80 |
if download_response: |
|
81 |
return _response_download(export_content, export_infos[1], export_infos[2]) ; |
|
82 |
else: |
|
83 |
return _response_write(export_content) |
|
84 |
||
85 |
||
86 |
# read conversion chain |
|
87 |
# execute chain |
|
88 |
# ready to send response |
|
89 |
# # TODO : formats must be imported from converters |
|
90 |
# if format == 'raw' : |
|
91 |
# export_content = content |
|
92 |
# elif src_format == format and format == 'html': |
|
93 |
# export_content = HTML_HEADER % content |
|
94 |
# else: |
|
95 |
# if use_pandoc : |
|
96 |
# export_content = pandoc_convert(content, src_format, format, full=True) |
|
97 |
# else : |
|
98 |
# fix_content = content |
|
99 |
# if src_format == 'html': |
|
100 |
# fix_content = combine_css_body(content, '') |
|
101 |
# export_content = oo_convert(fix_content, format) |
|
102 |
# |
|
103 |
## send response |
|
104 |
# export_infos = EXPORT_INFOS[format] |
|
105 |
# mimetype = export_infos['mimetype'] |
|
106 |
# extension = export_infos['extension'] |
|
107 |
# |
|
108 |
# if download: |
|
109 |
# return _response_download(export_content, mimetype, extension) |
|
110 |
# else : |
|
111 |
# return _response_write(export_content) |
|
112 |
# |
|
113 |
def _response_download(content, title, mimetype, extension): |
|
114 |
response = HttpResponse(mimetype=mimetype) |
|
115 |
file_title = title + '.' + extension |
|
116 |
from email.header import Header |
|
117 |
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
|
118 |
# 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
|
119 |
encoded_name = encoded_name.replace('\n','') |
|
9fa013909d9a
prevent header error if newline in header (pb not fully solved)
raph
parents:
0
diff
changeset
|
120 |
response['Content-Disposition'] = 'attachment; filename=%s' % encoded_name |
| 0 | 121 |
response.write(content) |
122 |
return response |
|
123 |
||
124 |
def _response_write(content): |
|
125 |
response = HttpResponse() |
|
126 |
response.write(content) |
|
127 |
return response |
|
128 |
||
129 |
||
130 |
EXPORT_INFOS = { |
|
131 |
# key -> [ download?, mimetype, extension] |
|
132 |
's5' : [False , ], |
|
133 |
'pdf' : [True , 'application/pdf' , 'pdf'], |
|
134 |
'markdown' : [True , 'text/plain' , 'mkd'], |
|
135 |
'odt' : [True , 'application/vnd.oasis.opendocument.text', 'odt'], |
|
136 |
'latex' :[True , 'text/x-tex', 'tex'], |
|
137 |
'html' :[True , 'text/html', 'html'], |
|
138 |
# raw export |
|
139 |
'raw' : [True, 'text/plain', 'txt'] |
|
140 |
} |
|
141 |
HTML_HEADER = u""" |
|
142 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
|
143 |
<html><head> |
|
144 |
<STYLE TYPE='text/css'> |
|
145 |
div.pagebreakhere { |
|
146 |
page-break-before: always ; |
|
147 |
} |
|
148 |
</STYLE> |
|
149 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head> |
|
150 |
<body>%s</body> |
|
151 |
</html> |
|
152 |
""" |
|
153 |
EXPORT_FORMATS = EXPORT_INFOS.keys() |
|
154 |
||
155 |
def content_export(request, content, title, src_format, format, use_pandoc): |
|
156 |
# TODO : formats must be imported from converters |
|
157 |
if format == 'raw' : |
|
158 |
export_content = content |
|
159 |
elif src_format == format and format == 'html': |
|
160 |
export_content = HTML_HEADER % content |
|
161 |
else: |
|
162 |
if use_pandoc : |
|
163 |
export_content = pandoc_convert(content, src_format, format, full=True) |
|
164 |
else : |
|
165 |
fix_content = content |
|
166 |
if src_format == 'html': |
|
|
77
fe91eb717a96
import oo_converters locally (not at module level) to avoid weird uno imports
raph
parents:
57
diff
changeset
|
167 |
from cm.converters.oo_converters import combine_css_body |
| 0 | 168 |
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
|
169 |
from cm.converters.oo_converters import convert_html as oo_convert |
| 0 | 170 |
export_content = oo_convert(fix_content, format) |
171 |
||
172 |
export_infos = EXPORT_INFOS[format] |
|
173 |
format_download = export_infos[0] |
|
174 |
||
175 |
if format_download: |
|
176 |
format_mimetype = export_infos[1] |
|
177 |
format_extension = export_infos[2] |
|
178 |
||
179 |
response = HttpResponse(mimetype=format_mimetype) |
|
180 |
file_title = title + '.' + format_extension |
|
181 |
from email.header import Header |
|
182 |
encoded_name = str(Header(file_title.encode('utf8'), charset='utf8', maxlinelen=500)) |
|
183 |
response['Content-Disposition'] = u'attachment; filename=%s' % encoded_name |
|
184 |
response.write(export_content) |
|
185 |
return response |
|
186 |
else: |
|
187 |
response = HttpResponse() |
|
188 |
response.write(export_content) |
|
189 |
return response |
|
190 |
||
191 |
||
192 |
def text_export(request, key, format): |
|
193 |
# TODO : formats must be imported from converters |
|
194 |
format = format.lower() |
|
195 |
if format not in EXPORT_FORMATS: |
|
196 |
raise Exception("Unsupported format %s (supported formats %s)" % (format, ' '.join(EXPORT_FORMATS))) |
|
197 |
text = Text.objects.get(key=key) |
|
198 |
text_version = text.get_latest_version() |
|
199 |
||
200 |
return content_export(request, text_version.content, text_version.title, text_version.format, format) |
|
201 |
||
202 |
def text_feed(request, key): |
|
|
360
bfaab8740995
Add abiword as an alternative to open office for conversions
gibus
parents:
77
diff
changeset
|
203 |
return "" |