Hi, The Security Labels and the Variables are used in many modules under the Database Node. The functions to parse and return them into desired format, are kept in the database utility file (databases/utils.py), so every node can use them.
I have applied the related changes in the database module. Please find attached patch for the same. Thanks, Khushboo
diff --git a/web/pgadmin/browser/server_groups/servers/databases/__init__.py b/web/pgadmin/browser/server_groups/servers/databases/__init__.py index 53792c6..0357e1f 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/__init__.py +++ b/web/pgadmin/browser/server_groups/servers/databases/__init__.py @@ -17,6 +17,8 @@ from pgadmin.browser.server_groups.servers.utils import parse_priv_from_db, \ from pgadmin.browser.collection import CollectionNodeModule import pgadmin.browser.server_groups.servers as servers from pgadmin.utils.ajax import precondition_required, gone +from pgadmin.browser.server_groups.servers.databases.utils import \ + parse_sec_labels_from_db, parse_variables_from_db from pgadmin.utils.driver import get_driver from config import PG_DEFAULT_DRIVER from functools import wraps @@ -273,43 +275,21 @@ class DatabaseView(PGChildNodeView): if not status: return internal_server_error(errormsg=res1) - # sending result to formtter - frmtd_reslt = self.formatter(result, res1) - # mergeing formated result with main result again - result.update(frmtd_reslt) + + # Get Formatted Security Labels + frmtd_sec_labels = parse_sec_labels_from_db(result['seclabels']) + result.update(frmtd_sec_labels) + + # Get Formatted Variables + frmtd_variables = parse_variables_from_db(res1['rows']) + result.update(frmtd_variables) + return ajax_response( response=result, status=200 ) @staticmethod - def formatter(result, varibles_rset): - """ We will use this function to format our output for - security label & variables""" - frmtd_result = dict() - sec_lbls = [] - if 'seclabels' in result and result['seclabels'] is not None: - for sec in result['seclabels']: - sec = re.search(r'([^=]+)=(.*$)', sec) - sec_lbls.append({ - 'provider': sec.group(1), - 'security_label': sec.group(2) - }) - frmtd_result.update({"seclabels" :sec_lbls}) - - variablesLst = [] - for row in varibles_rset['rows']: - for d in row['setconfig']: - var_name, var_value = d.split("=") - # Because we save as boolean string in db so it needs conversion - if var_value == 'false' or var_value == 'off': - var_value = False - variablesLst.append({'role': row['user_name'], 'name': var_name, 'value': var_value, 'database': row['db_name']}) - frmtd_result.update({"variables" : variablesLst}) - # returning final result - return frmtd_result - - @staticmethod def formatdbacl(res, dbacl): for row in dbacl: priv = parse_priv_from_db(row) @@ -847,8 +827,13 @@ class DatabaseView(PGChildNodeView): if not status: return internal_server_error(errormsg=res1) - frmtd_reslt = self.formatter(result, res1) - result.update(frmtd_reslt) + # Get Formatted Security Labels + frmtd_sec_labels = parse_sec_labels_from_db(result['seclabels']) + result.update(frmtd_sec_labels) + + # Get Formatted Variables + frmtd_variables = parse_variables_from_db(res1['rows']) + result.update(frmtd_variables) SQL = self.get_new_sql(gid, sid, result, did) diff --git a/web/pgadmin/browser/server_groups/servers/databases/utils.py b/web/pgadmin/browser/server_groups/servers/databases/utils.py new file mode 100644 index 0000000..2e7be6d --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/utils.py @@ -0,0 +1,85 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2016, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +"""Database helper utilities""" + +def parse_sec_labels_from_db(db_sec_labels): + """ + Function to format the output for security label. + + Args: + db_sec_labels : Security Label Array in (provider=label) format + + Returns: + Security Label Object in below format: + {'seclabels': [{'provider': 'provider_name', 'security_label': + 'security_label'},...]} + """ + sec_lbls = [] + + if db_sec_labels is not None: + for sec in db_sec_labels: + sec = re.search(r'([^=]+)=(.*$)', sec) + sec_lbls.append({ + 'provider': sec.group(1), + 'security_label': sec.group(2) + }) + + return {"seclabels": sec_lbls} + + +def parse_variables_from_db(db_varibles): + """ + Function to format the output for variables. + + Args: + db_varibles: Variable object + + Expected Object Format: + [ + { + 'setconfig': Variable Config Parameters, + 'user_name': User Name, + 'db_name': Database Name + },... + ] + where: + user_name and database are optional + Returns: + Variable Object in below format: + { + 'variables': [ + {'name': 'var_name', 'value': 'var_value', + 'user_name': 'user_name', 'database': 'database_name'}, + ...] + } + where: + user_name and database are optional + """ + variables_lst = [] + + if db_varibles is not None: + for row in db_varibles: + if 'setconfig' in row and row['setconfig'] is not None: + for d in row['setconfig']: + var_name, var_value = d.split("=") + # Because we save as boolean string in db so it needs + # conversion + if var_value == 'false' or var_value == 'off': + var_value = False + + var_dict = {'name': var_name, 'value': var_value} + if 'user_name' in row: + var_dict['role'] = row['user_name'] + if 'db_name' in row: + var_dict['database'] = row['db_name'] + + variables_lst.append(var_dict) + + return {"variables": variables_lst}
-- Sent via pgadmin-hackers mailing list (pgadmin-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgadmin-hackers