25 |
25 |
from qommon import ezt
|
26 |
26 |
from qommon.form import (SingleSelectWidget, WidgetList, CheckboxWidget,
|
27 |
27 |
StringWidget, UploadWidget, WysiwygTextWidget, Upload,
|
28 |
|
UploadedFile)
|
|
28 |
UploadedFile, UploadValidationError)
|
29 |
29 |
from qommon.errors import PublishError
|
30 |
30 |
import qommon
|
31 |
31 |
|
... | ... | |
186 |
186 |
elif hasattr(upload, 'get_file'):
|
187 |
187 |
fp = upload.get_file()
|
188 |
188 |
else:
|
189 |
|
raise Exception('unknown upload %r' % upload)
|
|
189 |
raise UploadValidationError('unknown upload object %r' % upload)
|
190 |
190 |
if upload.content_type and upload.content_type == 'application/rtf':
|
191 |
|
return 'rtf', ''
|
|
191 |
return 'rtf'
|
192 |
192 |
if (upload.content_type and upload.content_type == 'application/octet-stream') or \
|
193 |
193 |
upload.content_type is None:
|
194 |
194 |
if upload.base_filename and upload.base_filename.endswith('.rtf'):
|
195 |
|
return 'rtf', ''
|
|
195 |
return 'rtf'
|
196 |
196 |
if fp.read(10).startswith('{\\rtf'):
|
197 |
197 |
fp.seek(0)
|
198 |
|
return 'rtf', ''
|
|
198 |
return 'rtf'
|
199 |
199 |
fp.seek(0)
|
200 |
200 |
if upload.content_type and upload.content_type.startswith('application/vnd.oasis.opendocument.'):
|
201 |
|
return 'opendocument', ''
|
|
201 |
return 'opendocument'
|
202 |
202 |
if (upload.content_type and upload.content_type == 'application/octet-stream') or \
|
203 |
203 |
upload.content_type is None:
|
204 |
204 |
if upload.base_filename and upload.base_filename.rsplit('.', 1) in ('odt', 'ods', 'odc', 'odb'):
|
205 |
|
return 'opendocument', ''
|
|
205 |
return 'opendocument'
|
206 |
206 |
if is_opendocument(fp):
|
207 |
|
return 'opendocument', ''
|
208 |
|
return False, _('Only RTF and OpenDocument files can be used')
|
|
207 |
return 'opendocument'
|
|
208 |
raise UploadValidationError(_('Only RTF and OpenDocument files can be used'))
|
209 |
209 |
|
210 |
210 |
def add_parameters_widgets(self, form, parameters, prefix='',
|
211 |
211 |
formdef=None):
|
... | ... | |
267 |
267 |
|
268 |
268 |
def apply_template_to_formdata(self, formdata):
|
269 |
269 |
assert self.model_file
|
270 |
|
kind, msg = self.model_file_validation(self.model_file)
|
|
270 |
kind = self.model_file_validation(self.model_file)
|
271 |
271 |
if kind == 'rtf':
|
272 |
272 |
return self.apply_rtf_template_to_formdata(formdata)
|
273 |
273 |
elif kind == 'opendocument':
|
274 |
|
-
|