mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2024-11-27 11:10:19 -06:00
5dbbd8e638
1) No handling for INTERVAL type datetime.
For example: executing query
SELECT INTERVAL '15 minutes';
throws json serialization error, because it returns time in timedelta format which is not handled.
Added support to handle timedelta datetime format in DataTypeJSONEncoder class
2) When we try to get BC dates from database raises ValueError: year is out of range
For eg:
SELECT TIMESTAMP '0044-03-15 10:00:00 BC',
It is because pyscopg2 doesn't handle BC datetime format.
So we have defined our method which type cast the datetime value to string in pyscopg2 overriding default behaviour.
Reference:
http://initd.org/psycopg/docs/advanced.html#type-casting-from-sql-to-python
ccf3693be6/pgcli/pgexecute.py
129 lines
3.5 KiB
Python
129 lines
3.5 KiB
Python
##########################################################################
|
|
#
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
#
|
|
# Copyright (C) 2013 - 2016, The pgAdmin Development Team
|
|
# This software is released under the PostgreSQL Licence
|
|
#
|
|
##########################################################################
|
|
|
|
"""Utility functions for dealing with AJAX."""
|
|
|
|
from flask import Response
|
|
from flask.ext.babel import gettext as _
|
|
import simplejson as json
|
|
import datetime
|
|
import decimal
|
|
|
|
|
|
class DataTypeJSONEncoder(json.JSONEncoder):
|
|
def default(self, obj):
|
|
if isinstance(obj, datetime.datetime) \
|
|
or hasattr(obj, 'isoformat'):
|
|
return obj.isoformat()
|
|
elif isinstance(obj, datetime.timedelta):
|
|
return (datetime.datetime.min + obj).time().isoformat()
|
|
if isinstance(obj, decimal.Decimal):
|
|
return float(obj)
|
|
|
|
return json.JSONEncoder.default(self, obj)
|
|
|
|
|
|
def make_json_response(success=1, errormsg='', info='', result=None,
|
|
data=None, status=200):
|
|
"""Create a HTML response document describing the results of a request and
|
|
containing the data."""
|
|
doc = dict()
|
|
doc['success'] = success
|
|
doc['errormsg'] = errormsg
|
|
doc['info'] = info
|
|
doc['result'] = result
|
|
doc['data'] = data
|
|
|
|
return Response(
|
|
response=json.dumps(doc, cls=DataTypeJSONEncoder),
|
|
status=status,
|
|
mimetype="text/json"
|
|
)
|
|
|
|
|
|
def make_response(response=None, status=200):
|
|
"""Create a JSON response handled by the backbone models."""
|
|
return Response(
|
|
response=json.dumps(response, cls=DataTypeJSONEncoder),
|
|
status=status,
|
|
mimetype="text/json"
|
|
)
|
|
|
|
|
|
def internal_server_error(errormsg=''):
|
|
"""Create a response with HTTP status code 500 - Internal Server Error."""
|
|
return make_json_response(
|
|
status=500,
|
|
success=0,
|
|
errormsg=errormsg
|
|
)
|
|
|
|
|
|
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(
|
|
status=401,
|
|
success=0,
|
|
errormsg=errormsg
|
|
)
|
|
|
|
|
|
def bad_request(errormsg=''):
|
|
"""Create a response with HTTP status code 400 - Bad Request."""
|
|
return make_json_response(
|
|
status=400,
|
|
success=0,
|
|
errormsg=errormsg
|
|
)
|
|
|
|
|
|
def precondition_required(errormsg=''):
|
|
"""Create a response with HTTP status code 428 - Precondition Required."""
|
|
return make_json_response(
|
|
status=428,
|
|
success=0,
|
|
errormsg=errormsg
|
|
)
|
|
|
|
|
|
def success_return(message=''):
|
|
"""Create a response with HTTP status code 200 - OK."""
|
|
return make_json_response(
|
|
status=200,
|
|
success=1,
|
|
info=message
|
|
)
|
|
|
|
|
|
def gone(errormsg=''):
|
|
"""Create a response with HTTP status code 410 - GONE."""
|
|
return make_json_response(
|
|
status=410,
|
|
success=0,
|
|
errormsg=errormsg
|
|
)
|
|
|
|
|
|
def not_implemented(errormsg=_('Not implemented.')):
|
|
"""Create a response with HTTP status code 501 - Not Implemented."""
|
|
return make_json_response(
|
|
status=501,
|
|
success=0,
|
|
errormsg=errormsg
|
|
)
|