[fix] use response.mimetype and response.charset instead of setting 'Content-Type' directly in response.header

This commit is contained in:
Anand Doshi 2016-04-28 15:26:22 +05:30
parent 2556fa1a56
commit 763263d157
2 changed files with 13 additions and 9 deletions

View file

@ -43,14 +43,15 @@ def build_response(response_type=None):
def as_csv():
response = Response()
response.headers[b"Content-Type"] = b"text/csv; charset: utf-8"
response.mimetype = 'text/csv'
response.charset = 'utf-8'
response.headers[b"Content-Disposition"] = ("attachment; filename=\"%s.csv\"" % frappe.response['doctype'].replace(' ', '_')).encode("utf-8")
response.data = frappe.response['result']
return response
def as_raw():
response = Response()
response.headers[b"Content-Type"] = frappe.response.get("content_type") or mimetypes.guess_type(frappe.response['filename'])[0] or b"application/unknown"
response.mimetype = frappe.response.get("content_type") or mimetypes.guess_type(frappe.response['filename'])[0] or b"application/unknown"
response.headers[b"Content-Disposition"] = ("filename=\"%s\"" % frappe.response['filename'].replace(' ', '_')).encode("utf-8")
response.data = frappe.response['filecontent']
return response
@ -62,7 +63,8 @@ def as_json():
response.status_code = frappe.local.response['http_status_code']
del frappe.local.response['http_status_code']
response.headers[b"Content-Type"] = b"application/json; charset: utf-8"
response.mimetype = 'application/json'
response.charset = 'utf-8'
response.data = json.dumps(frappe.local.response, default=json_handler, separators=(',',':'))
return response
@ -150,7 +152,7 @@ def send_private_file(path):
# no need for content disposition and force download. let browser handle its opening.
# response.headers.add(b'Content-Disposition', b'attachment', filename=filename.encode("utf-8"))
response.headers[b'Content-Type'] = mimetypes.guess_type(filename)[0] or b'application/octet-stream'
response.mimetype = mimetypes.guess_type(filename)[0] or b'application/octet-stream'
return response

View file

@ -172,17 +172,19 @@ def resolve_from_map(path):
def set_content_type(response, data, path):
if isinstance(data, dict):
response.headers[b"Content-Type"] = b"application/json; charset: utf-8"
response.mimetype = 'application/json'
response.charset = 'utf-8'
data = json.dumps(data)
return data
response.headers[b"Content-Type"] = b"text/html; charset: utf-8"
response.mimetype = 'text/html'
response.charset = 'utf-8'
if "." in path:
content_type, encoding = mimetypes.guess_type(path)
if not content_type:
content_type = "text/html; charset: utf-8"
response.headers[b"Content-Type"] = content_type.encode("utf-8")
if content_type:
response.mimetype = content_type
response.charset = encoding
return data