164 lines
5.1 KiB
Python
164 lines
5.1 KiB
Python
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
|
# MIT License. See license.txt
|
|
|
|
from __future__ import unicode_literals
|
|
import json
|
|
import datetime
|
|
import gzip, cStringIO
|
|
import mimetypes
|
|
import os
|
|
import frappe
|
|
from frappe import _
|
|
from frappe.model.doc import Document
|
|
import frappe.utils
|
|
import frappe.sessions
|
|
import frappe.model.utils
|
|
import werkzeug.utils
|
|
from werkzeug.local import LocalProxy
|
|
from werkzeug.wsgi import wrap_file
|
|
from werkzeug.wrappers import Response
|
|
from werkzeug.exceptions import NotFound, Forbidden
|
|
|
|
def report_error(status_code):
|
|
if status_code!=404 or frappe.conf.logging:
|
|
frappe.errprint(frappe.utils.get_traceback())
|
|
|
|
response = build_response("json")
|
|
response.status_code = status_code
|
|
return response
|
|
|
|
def build_response(response_type=None):
|
|
response_type_map = {
|
|
'csv': as_csv,
|
|
'download': as_raw,
|
|
'json': as_json,
|
|
'page': as_page,
|
|
'redirect': redirect
|
|
}
|
|
|
|
return response_type_map[frappe.response.get('type') or response_type]()
|
|
|
|
def as_csv():
|
|
response = Response()
|
|
response.headers["Content-Type"] = "text/csv; charset: utf-8"
|
|
response.headers["Content-Disposition"] = "attachment; filename=%s.csv" % frappe.response['doctype'].replace(' ', '_')
|
|
response.data = frappe.response['result']
|
|
return response
|
|
|
|
def as_raw():
|
|
response = Response()
|
|
response.headers["Content-Type"] = mimetypes.guess_type(frappe.response['filename'])[0] or "application/unknown"
|
|
response.headers["Content-Disposition"] = "filename=%s" % frappe.response['filename'].replace(' ', '_')
|
|
response.data = frappe.response['filecontent']
|
|
return response
|
|
|
|
def as_json():
|
|
make_logs()
|
|
cleanup_docs()
|
|
response = Response()
|
|
response.headers["Content-Type"] = "application/json; charset: utf-8"
|
|
response = gzip(json.dumps(frappe.local.response, default=json_handler, separators=(',',':')),
|
|
response=response)
|
|
return response
|
|
|
|
def make_logs():
|
|
"""make strings for msgprint and errprint"""
|
|
if frappe.error_log:
|
|
# frappe.response['exc'] = json.dumps("\n".join([cstr(d) for d in frappe.error_log]))
|
|
frappe.response['exc'] = json.dumps([frappe.utils.cstr(d) for d in frappe.local.error_log])
|
|
|
|
if frappe.local.message_log:
|
|
frappe.response['_server_messages'] = json.dumps([frappe.utils.cstr(d) for d in frappe.local.message_log])
|
|
|
|
if frappe.debug_log and frappe.conf.get("logging") or False:
|
|
frappe.response['_debug_messages'] = json.dumps(frappe.local.debug_log)
|
|
|
|
def cleanup_docs():
|
|
if frappe.response.get('docs') and type(frappe.response['docs'])!=dict:
|
|
frappe.response['docs'] = frappe.model.utils.compress(frappe.response['docs'])
|
|
|
|
def gzip(data, response):
|
|
data = data.encode('utf-8')
|
|
orig_len = len(data)
|
|
if accept_gzip() and orig_len>512:
|
|
data = compressBuf(data)
|
|
response.headers["Content-Encoding"] = "gzip"
|
|
|
|
response.headers["Content-Length"] = str(len(data))
|
|
response.data = data
|
|
return response
|
|
|
|
def accept_gzip():
|
|
if "gzip" in frappe.get_request_header("HTTP_ACCEPT_ENCODING", ""):
|
|
return True
|
|
|
|
def compressBuf(buf):
|
|
zbuf = cStringIO.StringIO()
|
|
zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf, compresslevel = 5)
|
|
zfile.write(buf)
|
|
zfile.close()
|
|
return zbuf.getvalue()
|
|
|
|
def json_handler(obj):
|
|
"""serialize non-serializable data for json"""
|
|
|
|
# serialize date
|
|
if isinstance(obj, (datetime.date, datetime.timedelta, datetime.datetime)):
|
|
return unicode(obj)
|
|
elif isinstance(obj, LocalProxy):
|
|
return unicode(obj)
|
|
elif isinstance(obj, Document):
|
|
return obj.fields
|
|
else:
|
|
raise TypeError, """Object of type %s with value of %s is not JSON serializable""" % \
|
|
(type(obj), repr(obj))
|
|
|
|
def as_page():
|
|
"""print web page"""
|
|
from frappe.website.render import render
|
|
return render(frappe.response['page_name'], http_status_code=frappe.response.get("http_status_code"))
|
|
|
|
def redirect():
|
|
return werkzeug.utils.redirect(frappe.response.location)
|
|
|
|
def download_backup(path):
|
|
try:
|
|
frappe.only_for(("System Manager", "Administrator"))
|
|
except frappe.PermissionError:
|
|
raise Forbidden(_("You need to be logged in and have System Manager Role to be able to access backups."))
|
|
|
|
return send_private_file(path)
|
|
|
|
def send_private_file(path):
|
|
path = os.path.join(frappe.local.conf.get('private_path', 'private'), path.strip("/"))
|
|
|
|
if frappe.local.request.headers.get('X-Use-X-Accel-Redirect'):
|
|
path = '/' + path
|
|
response = Response()
|
|
response.headers['X-Accel-Redirect'] = path
|
|
else:
|
|
filename = os.path.basename(path)
|
|
filepath = frappe.utils.get_site_path(path)
|
|
try:
|
|
f = open(filepath, 'rb')
|
|
except IOError:
|
|
raise NotFound
|
|
|
|
response = Response(wrap_file(frappe.local.request.environ, f))
|
|
response.headers.add('Content-Disposition', 'attachment', filename=filename)
|
|
response.headers['Content-Type'] = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
|
|
|
|
return response
|
|
|
|
def handle_session_stopped():
|
|
response = Response("""<html>
|
|
<body style="background-color: #EEE;">
|
|
<h3 style="width: 900px; background-color: #FFF; border: 2px solid #AAA; padding: 20px; font-family: Arial; margin: 20px auto">
|
|
Updating.
|
|
We will be back in a few moments...
|
|
</h3>
|
|
</body>
|
|
</html>""")
|
|
response.status_code = 503
|
|
response.content_type = 'text/html'
|
|
return res
|