Fixed naming in response.py, build_response only if response.data does not exist

This commit is contained in:
Anand Doshi 2014-03-12 19:27:05 +05:30
parent 3444dd4a38
commit 9eb55aa102
5 changed files with 26 additions and 14 deletions

View file

@ -79,7 +79,8 @@ def application(request):
if frappe.local.request.method in ("POST", "PUT") and frappe.db and rollback:
frappe.db.rollback()
if frappe.local.form_dict.cmd or frappe.request.path.startswith("/api/"):
if (frappe.local.form_dict.cmd or frappe.request.path.startswith("/api/")) \
and not response.data:
if not frappe.local.response.get("type"):
frappe.local.response["type"] = "json"

View file

@ -10,6 +10,7 @@ import frappe.utils
import frappe.utils.user
from frappe import conf
from frappe.sessions import Session
from frappe.modules.patch_handler import check_session_stopped
class HTTPRequest:
def __init__(self):
@ -39,9 +40,7 @@ class HTTPRequest:
frappe.local.cookie_manager.init_cookies()
# check status
if frappe.db.get_global("__session_status")=='stop':
frappe.msgprint(frappe.db.get_global("__session_status_message"))
raise frappe.SessionStopped('Session Stopped')
check_session_stopped()
# load user
self.setup_user()

View file

@ -1,3 +1,10 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals
# BEWARE don't put anything in this file except exceptions
class ValidationError(Exception):
http_status_code = 417

View file

@ -114,6 +114,11 @@ def block_user(block):
frappe.db.set_global('__session_status', block and 'stop' or None)
frappe.db.set_global('__session_status_message', block and msg or None)
frappe.db.commit()
def check_session_stopped():
if frappe.db.get_global("__session_status")=='stop':
frappe.msgprint(frappe.db.get_global("__session_status_message"))
raise frappe.SessionStopped('Session Stopped')
def setup():
frappe.db.sql("""CREATE TABLE IF NOT EXISTS `__PatchLog` (

View file

@ -24,25 +24,25 @@ def report_error(status_code, response):
def build_response(response):
response_type_map = {
'csv': print_csv,
'download': print_raw,
'json': print_json,
'page': print_page,
'csv': as_csv,
'download': as_raw,
'json': as_json,
'page': as_page,
'redirect': redirect
}
response_type_map[frappe.response.get('type')](response=response)
def print_page():
def as_page():
"""print web page"""
from frappe.website.render import render
render(frappe.response['page_name'])
def print_json(response):
def as_json(response):
make_logs()
cleanup_docs()
response.headers["Content-Type"] = "text/json; charset: utf-8"
print_zip(json.dumps(frappe.local.response, default=json_handler, separators=(',',':')), response=response)
gzip(json.dumps(frappe.local.response, default=json_handler, separators=(',',':')), response=response)
def redirect(response):
response.data = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
@ -59,14 +59,14 @@ 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 print_csv(response):
def as_csv(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']
def print_raw(response):
def as_raw(response):
response.headers["Content-Type"] = \
mimetypes.guess_type(frappe.response['filename'])[0] or "application/unknown"
response.headers["Content-Disposition"] = \
@ -85,7 +85,7 @@ def make_logs():
if frappe.debug_log and frappe.conf.get("logging") or False:
frappe.response['_debug_messages'] = json.dumps(frappe.local.debug_log)
def print_zip(data, response):
def gzip(data, response):
data = data.encode('utf-8')
orig_len = len(data)
if accept_gzip() and orig_len>512: