def _process_multipart_body(self, mimeinput, charset): headers = StringIO() lines = mimeinput.readpart() for line in lines: headers.write(line) if line == '\r\n': break headers.seek(0) headers = rfc822.Message(headers) ctype, ctype_params = parse_header(headers.get('content-type', '')) if ctype and 'charset' in ctype_params: charset = ctype_params['charset'] cdisp, cdisp_params = parse_header(headers.get('content-disposition', '')) if not cdisp: raise RequestError('expected Content-Disposition header') name = cdisp_params.get('name') filename = cdisp_params.get('filename') if not (cdisp == 'form-data' and name): raise RequestError('expected Content-Disposition: form-data' 'with a "name" parameter: got %r' % headers.get('content-disposition', '')) # FIXME: should really to handle Content-Transfer-Encoding and other # MIME complexity here. See RFC2048 for the full horror story. if filename: # it might be large file upload so use a temporary file upload = Upload(filename, ctype, charset) upload.receive(lines) _add_field_value(self.form, name, upload) else: value = _decode_string(''.join(lines), charset or self.charset) _add_field_value(self.form, name, value)
def process_request(tid): logger.warning("TR id: {}".format(tid)) tr = TranslationRequest.objects.get(id=tid) try: tr.start() if tr.infile: infile = tr.infile else: infile = StringIO(tr.input) template = tr.template.text out = translate_document(infile=infile.get(), template=template, template_data=tr.to_mongo()) tr.outfile.delete() tr.outfile.new_file(encoding="utf-8") for chunk in out: tr.outfile.write(chunk) tr.outfile.close() tr.save() tr.status = TranslationRequest.SUCCESS tr.finish() logger.warning("Processed") return tr except Exception as ex: raise tr.status = TranslationRequest.ERROR tr.message = str("{} -- {}".format(ex, traceback.format_exc())) tr.finish()