Fixed cognitive complexity issues reported by SonarQube.

This commit is contained in:
Aditya Toshniwal 2020-09-03 18:10:57 +05:30 committed by Akshay Joshi
parent a67f6e4f9c
commit 974d395a48
3 changed files with 198 additions and 188 deletions

View File

@ -672,10 +672,10 @@ class Filemanager(object):
@staticmethod @staticmethod
def check_access_permission(in_dir, path): def check_access_permission(in_dir, path):
if not config.SERVER_MODE:
return
if config.SERVER_MODE: in_dir = '' if in_dir is None else in_dir
if in_dir is None:
in_dir = ""
orig_path = Filemanager.get_abs_path(in_dir, path) orig_path = Filemanager.get_abs_path(in_dir, path)
# This translates path with relative path notations # This translates path with relative path notations
@ -695,7 +695,6 @@ class Filemanager(object):
if not orig_path.startswith(in_dir): if not orig_path.startswith(in_dir):
raise InternalServerError( raise InternalServerError(
gettext("Access denied ({0})").format(path)) gettext("Access denied ({0})").format(path))
return True
@staticmethod @staticmethod
def get_abs_path(in_dir, path): def get_abs_path(in_dir, path):

View File

@ -174,6 +174,79 @@ def acl_list(sid, did):
mimetype="application/json") mimetype="application/json")
def _get_rows_for_type(conn, ntype, server_prop, node_id):
"""
Used internally by properties to get rows for an object type
:param conn: connection object
:param ntype: object type
:param server_prop: server properties
:param node_id: oid
:return: status, execute response
"""
function_sql_url = '/sql/function.sql'
status, res = True, []
if ntype in ['function']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='function')
status, res = conn.execute_dict(sql)
# Fetch procedures only if server type is EPAS or PG >= 11
elif len(server_prop) > 0 and (
server_prop['server_type'] == 'ppas' or (
server_prop['server_type'] == 'pg' and
server_prop['version'] >= 11000
)
) and ntype in ['procedure']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='procedure')
status, res = conn.execute_dict(sql)
# Fetch trigger functions
elif ntype in ['trigger_function']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='trigger_function')
status, res = conn.execute_dict(sql)
# Fetch Sequences against schema
elif ntype in ['sequence']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/sequence.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
# Fetch Tables against schema
elif ntype in ['table']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/table.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
# Fetch Views against schema
elif ntype in ['view']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='v')
status, res = conn.execute_dict(sql)
# Fetch Materialzed Views against schema
elif ntype in ['mview']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='m')
status, res = conn.execute_dict(sql)
return status, res
@blueprint.route( @blueprint.route(
'/<int:sid>/<int:did>/<int:node_id>/<node_type>/', '/<int:sid>/<int:did>/<int:node_id>/<node_type>/',
methods=['GET'], endpoint='objects' methods=['GET'], endpoint='objects'
@ -185,7 +258,6 @@ def properties(sid, did, node_id, node_type):
and render into selection page of wizard and render into selection page of wizard
""" """
function_sql_url = '/sql/function.sql'
get_schema_sql_url = '/sql/get_schemas.sql' get_schema_sql_url = '/sql/get_schemas.sql'
# unquote encoded url parameter # unquote encoded url parameter
@ -198,130 +270,66 @@ def properties(sid, did, node_id, node_type):
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid) manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
conn = manager.connection(did=did) conn = manager.connection(did=did)
node_types = []
show_sysobj = blueprint.show_system_objects().get() show_sysobj = blueprint.show_system_objects().get()
if node_type == 'database': if node_type == 'database':
# Fetch list of schemas
# Get sys_obj_values and get list of schemas
ntype = 'schema'
sql = render_template("/".join( sql = render_template("/".join(
[server_prop['template_path'], get_schema_sql_url]), [server_prop['template_path'], get_schema_sql_url]),
show_sysobj=show_sysobj) show_sysobj=show_sysobj)
status, res = conn.execute_dict(sql) ntype = 'schema'
if not status:
return internal_server_error(errormsg=res)
node_types = res['rows']
else: else:
sql = render_template("/".join( sql = render_template("/".join(
[server_prop['template_path'], get_schema_sql_url]), [server_prop['template_path'], get_schema_sql_url]),
nspid=node_id, show_sysobj=False) show_sysobj=show_sysobj, nspid=node_id)
ntype = node_type
status, res = conn.execute_dict(sql) status, res = conn.execute_dict(sql)
if not status: if not status:
return internal_server_error(errormsg=res) return internal_server_error(errormsg=res)
node_types = res['rows'] node_types = res['rows']
ntype = node_type
def _append_rows(status, res, disp_type):
if not status:
current_app.logger.error(res)
failed_objects.append(disp_type)
else:
res_data.extend(res['rows'])
for row in node_types: for row in node_types:
if 'oid' in row: if 'oid' in row:
node_id = row['oid'] node_id = row['oid']
# Fetch functions against schema if ntype == 'schema':
if ntype in ['schema', 'function']: status, res = _get_rows_for_type(
sql = render_template("/".join( conn, 'function', server_prop, node_id)
[server_prop['template_path'], function_sql_url]), _append_rows(status, res, 'function')
node_id=node_id, type='function')
status, res = conn.execute_dict(sql) status, res = _get_rows_for_type(
if not status: conn, 'procedure', server_prop, node_id)
current_app.logger.error(res) _append_rows(status, res, 'procedure')
failed_objects.append('function')
status, res = _get_rows_for_type(
conn, 'trigger_function', server_prop, node_id)
_append_rows(status, res, 'trigger function')
status, res = _get_rows_for_type(
conn, 'sequence', server_prop, node_id)
_append_rows(status, res, 'sequence')
status, res = _get_rows_for_type(
conn, 'table', server_prop, node_id)
_append_rows(status, res, 'table')
status, res = _get_rows_for_type(
conn, 'view', server_prop, node_id)
_append_rows(status, res, 'view')
status, res = _get_rows_for_type(
conn, 'mview', server_prop, node_id)
_append_rows(status, res, 'materialized view')
else: else:
res_data.extend(res['rows']) status, res = _get_rows_for_type(conn, ntype, server_prop, node_id)
_append_rows(status, res, 'function')
# Fetch procedures only if server type is EPAS or PG >= 11
if (len(server_prop) > 0 and
(server_prop['server_type'] == 'ppas' or
(server_prop['server_type'] == 'pg' and
server_prop['version'] >= 11000)) and
ntype in ['schema', 'procedure']):
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='procedure')
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('procedure')
else:
res_data.extend(res['rows'])
# Fetch trigger functions
if ntype in ['schema', 'trigger_function']:
sql = render_template("/".join(
[server_prop['template_path'], function_sql_url]),
node_id=node_id, type='trigger_function')
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('trigger function')
else:
res_data.extend(res['rows'])
# Fetch Sequences against schema
if ntype in ['schema', 'sequence']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/sequence.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('sequence')
else:
res_data.extend(res['rows'])
# Fetch Tables against schema
if ntype in ['schema', 'table']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/table.sql']),
node_id=node_id)
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('table')
else:
res_data.extend(res['rows'])
# Fetch Views against schema
if ntype in ['schema', 'view']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='v')
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('view')
else:
res_data.extend(res['rows'])
# Fetch Materialzed Views against schema
if ntype in ['schema', 'mview']:
sql = render_template("/".join(
[server_prop['template_path'], '/sql/view.sql']),
node_id=node_id, node_type='m')
status, res = conn.execute_dict(sql)
if not status:
current_app.logger.error(res)
failed_objects.append('materialized view')
else:
res_data.extend(res['rows'])
msg = None msg = None
if len(failed_objects) > 0: if len(failed_objects) > 0:

View File

@ -1316,16 +1316,26 @@ def save_file():
) )
@login_required @login_required
def start_query_download_tool(trans_id): def start_query_download_tool(trans_id):
sync_conn = None
(status, error_msg, sync_conn, trans_obj, (status, error_msg, sync_conn, trans_obj,
session_obj) = check_transaction_status(trans_id) session_obj) = check_transaction_status(trans_id)
if status and sync_conn is not None and \ if not status or sync_conn is None or trans_obj is None or \
trans_obj is not None and session_obj is not None: session_obj is None:
return internal_server_error(
errormsg=gettext("Transaction status check failed.")
)
data = request.values if request.values else None data = request.values if request.values else None
if data is None or (data and 'query' not in data):
return make_json_response(
status=410,
success=0,
errormsg=gettext(
"Could not find the required parameter (query)."
)
)
try: try:
if data and 'query' in data:
sql = data['query'] sql = data['query']
# This returns generator of records. # This returns generator of records.
@ -1352,13 +1362,10 @@ def start_query_download_tool(trans_id):
else 'text/plain' else 'text/plain'
) )
if 'filename' in data and data['filename'] != "":
filename = data['filename']
else:
import time import time
filename = '{0}.{1}'. \ extn = 'csv' if blueprint.csv_field_separator.get() == ',' else 'txt'
format(int(time.time()), 'csv' if blueprint. filename = data['filename'] if data.get('filename', '') != "" else \
csv_field_separator.get() == ',' else 'txt') '{0}.{1}'.format(int(time.time()), extn)
# We will try to encode report file name with latin-1 # We will try to encode report file name with latin-1
# If it fails then we will fallback to default ascii file name # If it fails then we will fallback to default ascii file name
@ -1381,10 +1388,6 @@ def start_query_download_tool(trans_id):
err_msg = "Error: {0}".format( err_msg = "Error: {0}".format(
e.strerror if hasattr(e, 'strerror') else str(e)) e.strerror if hasattr(e, 'strerror') else str(e))
return internal_server_error(errormsg=err_msg) return internal_server_error(errormsg=err_msg)
else:
return internal_server_error(
errormsg=gettext("Transaction status check failed.")
)
@blueprint.route( @blueprint.route(