move download_backup to frappe.utils.response

This commit is contained in:
Pratik Vyas 2014-03-07 00:56:50 +05:30
parent 4370aede98
commit be715d277b
3 changed files with 33 additions and 31 deletions

View file

@ -15,6 +15,7 @@ import frappe
import frappe.handler
import frappe.auth
import frappe.api
import frappe.utils.response
import frappe.website.render
from frappe.utils import get_site_name
@ -58,8 +59,8 @@ def application(request):
frappe.handler.handle()
elif frappe.request.path.startswith("/api/"):
frappe.api.handle()
elif request.path.startswith('/backups'):
frappe.utils.download_backup(request.path)
elif frappe.request.path.startswith('/backups'):
frappe.utils.response.download_backup(request.path)
elif frappe.local.request.method in ('GET', 'HEAD'):
frappe.website.render.render(frappe.request.path[1:])
else:

View file

@ -7,11 +7,7 @@ from __future__ import unicode_literals
from werkzeug.test import Client
import os
import re
import mimetypes
import urllib
from werkzeug.wsgi import wrap_file
from werkzeug.wrappers import Response
from werkzeug.exceptions import NotFound, Unauthorized
import frappe
no_value_fields = ['Section Break', 'Column Break', 'HTML', 'Table', 'FlexTable',
@ -933,28 +929,3 @@ def touch_file(path):
def get_test_client():
from frappe.app import application
return Client(application)
def download_backup(path):
try:
frappe.only_for(("System Manager", "Administrator"))
except frappe.PermissionError:
raise Unauthorized
send_private_file(path)
def send_private_file(path):
path = path[1:] if path.startswith('/') else path
path = os.path.join(frappe.local.conf.get('private_path', 'private'), path)
if frappe.local.request.headers.get('X-Use-X-Accel-Redirect'):
path = '/' + path
frappe.local._response.headers['X-Accel-Redirect'] = path
else:
filename = os.path.basename(path)
filepath = get_site_path(path)
try:
f = open(filepath, 'rb')
except IOError:
raise NotFound
frappe.local._response = Response(wrap_file(frappe.local.request.environ, f))
frappe.local._response.headers.add('Content-Disposition', 'attachment', filename=filename)
frappe.local._response.headers['Content-Type'] = mimetypes.guess_type(filename)[0] or 'application/octet-stream'

View file

@ -5,11 +5,16 @@ from __future__ import unicode_literals
import json
import datetime
import gzip, cStringIO
import mimetypes
import os
import frappe
import frappe.utils
import frappe.sessions
import frappe.model.utils
from werkzeug.local import LocalProxy
from werkzeug.wsgi import wrap_file
from werkzeug.wrappers import Response
from werkzeug.exceptions import NotFound, Unauthorized
def report_error(status_code):
if status_code!=404 or frappe.conf.logging:
@ -113,3 +118,28 @@ def compressBuf(buf):
zfile.write(buf)
zfile.close()
return zbuf.getvalue()
def download_backup(path):
try:
frappe.only_for(("System Manager", "Administrator"))
except frappe.PermissionError:
raise Unauthorized
send_private_file(path)
def send_private_file(path):
path = path[1:] if path.startswith('/') else path
path = os.path.join(frappe.local.conf.get('private_path', 'private'), path)
if frappe.local.request.headers.get('X-Use-X-Accel-Redirect'):
path = '/' + path
frappe.local._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
frappe.local._response = Response(wrap_file(frappe.local.request.environ, f))
frappe.local._response.headers.add('Content-Disposition', 'attachment', filename=filename)
frappe.local._response.headers['Content-Type'] = mimetypes.guess_type(filename)[0] or 'application/octet-stream'