2015-02-15 16:10:53 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2016-01-18 08:48:14 -06:00
|
|
|
# Copyright (C) 2013 - 2016, The pgAdmin Development Team
|
2015-02-15 16:10:53 -06:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""Utility functions for dealing with AJAX."""
|
|
|
|
|
2015-06-30 00:51:55 -05:00
|
|
|
from flask import Response
|
2016-01-02 03:25:10 -06:00
|
|
|
from flask.ext.babel import gettext as _
|
2016-01-09 06:35:15 -06:00
|
|
|
import simplejson as json
|
2016-02-05 02:48:35 -06:00
|
|
|
import datetime
|
|
|
|
import decimal
|
|
|
|
|
|
|
|
|
|
|
|
class DataTypeJSONEncoder(json.JSONEncoder):
|
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, datetime.datetime):
|
|
|
|
return obj.isoformat()
|
|
|
|
if isinstance(obj, decimal.Decimal):
|
|
|
|
return float(obj)
|
|
|
|
|
|
|
|
return json.JSONEncoder.default(self, obj)
|
2015-02-15 16:10:53 -06:00
|
|
|
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
def make_json_response(success=1, errormsg='', info='', result=None,
|
|
|
|
data=None, status=200):
|
2015-02-18 21:06:12 -06:00
|
|
|
"""Create a HTML response document describing the results of a request and
|
2015-02-15 16:10:53 -06:00
|
|
|
containing the data."""
|
2015-06-30 00:51:55 -05:00
|
|
|
doc = dict()
|
|
|
|
doc['success'] = success
|
|
|
|
doc['errormsg'] = errormsg
|
|
|
|
doc['info'] = info
|
|
|
|
doc['result'] = result
|
|
|
|
doc['data'] = data
|
|
|
|
|
2015-10-20 02:03:18 -05:00
|
|
|
return Response(
|
2016-02-05 02:48:35 -06:00
|
|
|
response=json.dumps(doc, cls=DataTypeJSONEncoder),
|
2016-01-02 03:25:10 -06:00
|
|
|
status=status,
|
|
|
|
mimetype="text/json"
|
|
|
|
)
|
2015-10-20 02:03:18 -05:00
|
|
|
|
2015-06-30 00:51:55 -05:00
|
|
|
|
|
|
|
def make_response(response=None, status=200):
|
|
|
|
"""Create a JSON response handled by the backbone models."""
|
2015-10-20 02:03:18 -05:00
|
|
|
return Response(
|
2016-02-05 02:48:35 -06:00
|
|
|
response=json.dumps(response, cls=DataTypeJSONEncoder),
|
2016-01-02 03:25:10 -06:00
|
|
|
status=status,
|
|
|
|
mimetype="text/json"
|
|
|
|
)
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def internal_server_error(errormsg=''):
|
|
|
|
"""Create a response with HTTP status code 500 - Internal Server Error."""
|
|
|
|
return make_json_response(
|
2016-01-02 03:25:10 -06:00
|
|
|
status=500,
|
|
|
|
success=0,
|
|
|
|
errormsg=errormsg
|
|
|
|
)
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def forbidden(errmsg=''):
|
|
|
|
"""Create a response with HTTP status code 403 - Forbidden."""
|
|
|
|
return make_json_response(
|
|
|
|
status=403,
|
|
|
|
success=0,
|
|
|
|
errormsg=errmsg
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def unauthorized(errormsg=''):
|
|
|
|
"""Create a response with HTTP status code 401 - Unauthorized."""
|
|
|
|
return make_json_response(
|
2016-01-02 03:25:10 -06:00
|
|
|
status=401,
|
|
|
|
success=0,
|
|
|
|
errormsg=errormsg
|
|
|
|
)
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def bad_request(errormsg=''):
|
|
|
|
"""Create a response with HTTP status code 400 - Bad Request."""
|
|
|
|
return make_json_response(
|
2016-01-02 03:25:10 -06:00
|
|
|
status=400,
|
|
|
|
success=0,
|
|
|
|
errormsg=errormsg
|
|
|
|
)
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def precondition_required(errormsg=''):
|
|
|
|
"""Create a response with HTTP status code 428 - Precondition Required."""
|
|
|
|
return make_json_response(
|
2016-01-02 03:25:10 -06:00
|
|
|
status=428,
|
|
|
|
success=0,
|
|
|
|
errormsg=errormsg
|
|
|
|
)
|
2015-10-20 02:03:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def success_return(message=''):
|
|
|
|
"""Create a response with HTTP status code 200 - OK."""
|
|
|
|
return make_json_response(
|
2016-01-02 03:25:10 -06:00
|
|
|
status=200,
|
|
|
|
success=1,
|
|
|
|
info=message
|
|
|
|
)
|
|
|
|
|
2015-12-04 03:17:18 -06:00
|
|
|
|
|
|
|
def gone(errormsg=''):
|
|
|
|
"""Create a response with HTTP status code 410 - GONE."""
|
|
|
|
return make_json_response(
|
2016-01-02 03:25:10 -06:00
|
|
|
status=410,
|
|
|
|
success=0,
|
|
|
|
errormsg=errormsg
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def not_implemented(errormsg=_('It is not implemented!')):
|
|
|
|
"""Create a response with HTTP status code 501 - Not Implemented."""
|
|
|
|
return make_json_response(
|
|
|
|
status=501,
|
|
|
|
success=0,
|
|
|
|
errormsg=errormsg
|
|
|
|
)
|