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