Some clumsy coding related fixes reported by SonarQube.

This commit is contained in:
Aditya Toshniwal 2020-06-12 15:24:17 +05:30 committed by Akshay Joshi
parent ee1ea47e88
commit 8c20f0c0d2
14 changed files with 222 additions and 238 deletions

View File

@ -589,8 +589,7 @@ def create_app(app_name=None):
def store_crypt_key(app, user):
# in desktop mode, master password is used to encrypt/decrypt
# and is stored in the keyManager memory
if config.SERVER_MODE:
if 'password' in request.form:
if config.SERVER_MODE and 'password' in request.form:
current_app.keyManager.set(request.form['password'])
@user_logged_out.connect_via(app)

View File

@ -621,8 +621,8 @@ def index():
except Exception:
current_app.logger.exception('Exception when checking for update')
if data is not None:
if data[config.UPGRADE_CHECK_KEY]['version_int'] > \
if data is not None and \
data[config.UPGRADE_CHECK_KEY]['version_int'] > \
config.APP_VERSION_INT:
msg = render_template(
MODULE_NAME + "/upgrade.html",

View File

@ -522,8 +522,8 @@ class ServerNode(PGChildNodeView):
if 'db_res' in data:
data['db_res'] = ','.join(data['db_res'])
if 'hostaddr' in data and data['hostaddr'] and data['hostaddr'] != '':
if not is_valid_ipaddress(data['hostaddr']):
if 'hostaddr' in data and data['hostaddr'] and data['hostaddr'] != '' \
and not is_valid_ipaddress(data['hostaddr']):
return make_json_response(
success=0,
status=400,
@ -741,8 +741,8 @@ class ServerNode(PGChildNodeView):
).format(arg)
)
if 'hostaddr' in data and data['hostaddr'] and data['hostaddr'] != '':
if not is_valid_ipaddress(data['hostaddr']):
if 'hostaddr' in data and data['hostaddr'] and data['hostaddr'] != '' \
and not is_valid_ipaddress(data['hostaddr']):
return make_json_response(
success=0,
status=400,
@ -1287,15 +1287,14 @@ class ServerNode(PGChildNodeView):
# If there is no password found for the server
# then check for pgpass file
if not server.password and not manager.password:
if server.passfile and \
manager.passfile and \
if not server.password and not manager.password and \
server.passfile and manager.passfile and \
server.passfile == manager.passfile:
is_passfile = True
# Check for password only if there is no pgpass file used
if not is_passfile:
if data and ('password' not in data or data['password'] == ''):
if not is_passfile and data and \
('password' not in data or data['password'] == ''):
return make_json_response(
status=400,
success=0,
@ -1490,9 +1489,8 @@ class ServerNode(PGChildNodeView):
errormsg=gettext('Please connect the server.')
)
if not server.password or not manager.password:
if server.passfile and \
manager.passfile and \
if (not server.password or not manager.password) and \
server.passfile and manager.passfile and \
server.passfile == manager.passfile:
is_pgpass = True
return make_json_response(

View File

@ -487,8 +487,7 @@ It may have been removed by another user.
if not status:
return internal_server_error(errormsg=rset)
if scid is not None:
if len(rset['rows']) == 0:
if scid is not None and len(rset['rows']) == 0:
return gone(gettext("""
Could not find the schema in the database.
It may have been removed by another user.

View File

@ -410,27 +410,25 @@ class CollationView(PGChildNodeView, SchemaDiffObjectCompare):
for arg in definition_args:
if (
arg == 'locale' and
(arg not in data or data[arg] == '')
):
if 'copy_collation' not in data and (
(arg not in data or data[arg] == '') and
'copy_collation' not in data and
'lc_collate' not in data and 'lc_type' not in data
):
missing_definition_flag = True
if (
arg == 'copy_collation' and
(arg not in data or data[arg] == '')
):
if 'locale' not in data and (
(arg not in data or data[arg] == '') and
'locale' not in data and
'lc_collate' not in data and 'lc_type' not in data
):
missing_definition_flag = True
if (
(arg == 'lc_collate' or arg == 'lc_type') and
(arg not in data or data[arg] == '')
(arg not in data or data[arg] == '') and
'copy_collation' not in data and 'locale' not in data
):
if 'copy_collation' not in data and 'locale' not in data:
missing_definition_flag = True
return missing_definition_flag

View File

@ -1163,8 +1163,7 @@ class ForeignTableView(PGChildNodeView, DataTypeReader,
data['columns'] = cols['rows']
# Get Inherited table names from their OID
if inherits:
if 'inherits' in data and data['inherits']:
if inherits and 'inherits' in data and data['inherits']:
inherits = tuple([int(x) for x in data['inherits']])
if len(inherits) == 1:
inherits = "(" + str(inherits[0]) + ")"

View File

@ -394,8 +394,8 @@ class FtsDictionaryView(PGChildNodeView, SchemaDiffObjectCompare):
res['rows'][0]['oid'] <= self.datlastsysoid)
# Handle templates and its schema name properly
if res['rows'][0]['template_schema'] is not None:
if res['rows'][0]['template_schema'] != "pg_catalog":
if res['rows'][0]['template_schema'] is not None and \
res['rows'][0]['template_schema'] != "pg_catalog":
res['rows'][0]['template'] = self.qtIdent(
self.conn, res['rows'][0]['template_schema'],
res['rows'][0]['template']
@ -669,8 +669,8 @@ class FtsDictionaryView(PGChildNodeView, SchemaDiffObjectCompare):
data['schema'] = old_data['schema']
# Handle templates and its schema name properly
if old_data['template_schema'] is not None:
if old_data['template_schema'] != "pg_catalog":
if old_data['template_schema'] is not None and \
old_data['template_schema'] != "pg_catalog":
old_data['template'] = self.qtIdent(
self.conn, old_data['template_schema'],
old_data['template']
@ -802,8 +802,8 @@ class FtsDictionaryView(PGChildNodeView, SchemaDiffObjectCompare):
))
# Handle templates and its schema name properly
if res['rows'][0]['template_schema'] is not None:
if res['rows'][0]['template_schema'] != "pg_catalog":
if res['rows'][0]['template_schema'] is not None and \
res['rows'][0]['template_schema'] != "pg_catalog":
res['rows'][0]['template'] = self.qtIdent(
self.conn, res['rows'][0]['template_schema'],
res['rows'][0]['template']

View File

@ -222,8 +222,9 @@ def get_sql(conn, data, did, tid, ctype, cid=None, template_path=None):
for arg in required_args:
if isinstance(arg, list):
for param in arg:
if param in data:
if is_key_str(param, data) or is_key_list(param, data):
if param in data and \
(is_key_str(param, data) or
is_key_list(param, data)):
break
else:
return _('-- definition incomplete'), name

View File

@ -538,8 +538,7 @@ class RuleView(PGChildNodeView, SchemaDiffObjectCompare):
SQL = ''
if data:
if source_schema:
if 'statements' in data:
if source_schema and 'statements' in data:
# Replace the source schema with the target schema
data['statements'] = data['statements'].replace(
source_schema, diff_schema)

View File

@ -1540,8 +1540,8 @@ class BaseTableView(PGChildNodeView, BasePartitionTable):
reset_values = []
for data_row in data[vacuum_key]['changed']:
for old_data_row in old_data[vacuum_key]:
if data_row['name'] == old_data_row['name']:
if 'value' in data_row:
if data_row['name'] == old_data_row['name'] and \
'value' in data_row:
if data_row['value'] is not None:
set_values.append(data_row)
elif data_row['value'] is None and \

View File

@ -946,8 +946,8 @@ class TypeView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare):
)
# Additional checks goes here
# If type is range then check if subtype is defined or not
if data and data[arg] == 'r':
if 'typname' not in data or data['typname'] is None:
if data and data[arg] == 'r' and \
('typname' not in data or data['typname'] is None):
return make_json_response(
status=410,
success=0,
@ -957,13 +957,11 @@ class TypeView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare):
)
# If type is external then check if input/output
# conversion function is defined
if data and data[arg] == 'b':
if (
if data and data[arg] == 'b' and (
'typinput' not in data or
'typoutput' not in data or
data['typinput'] is None or
data['typoutput'] is None
):
data['typoutput'] is None):
return make_json_response(
status=410,
success=0,
@ -1307,19 +1305,17 @@ class TypeView(PGChildNodeView, DataTypeReader, SchemaDiffObjectCompare):
# Additional checks go here
# If type is range then check if subtype is defined or not
if data and data[arg] == 'r':
if 'typname' not in data or data['typname'] is None:
if data and data[arg] == 'r' and \
('typname' not in data or data['typname'] is None):
return "-- definition incomplete"
# If type is external then check if input/output
# conversion function is defined
if data and data[arg] == 'b':
if (
if data and data[arg] == 'b' and (
'typinput' not in data or
'typoutput' not in data or
data['typinput'] is None or
data['typoutput'] is None
):
data['typoutput'] is None):
return "-- definition incomplete"
# Privileges

View File

@ -1637,8 +1637,7 @@ class MViewNode(ViewNode, VacuumSettings):
data['vacuum_data']['reset'] = []
# table vacuum: separate list of changed and reset data for
if 'vacuum_table' in data:
if 'changed' in data['vacuum_table']:
if 'vacuum_table' in data and 'changed' in data['vacuum_table']:
for item in data['vacuum_table']['changed']:
if 'value' in item.keys():
if item['value'] is None:
@ -1651,8 +1650,7 @@ class MViewNode(ViewNode, VacuumSettings):
data['vacuum_data']['changed'].append(item)
# toast autovacuum: separate list of changed and reset data
if 'vacuum_toast' in data:
if 'changed' in data['vacuum_toast']:
if 'vacuum_toast' in data and 'changed' in data['vacuum_toast']:
for item in data['vacuum_toast']['changed']:
if 'value' in item.keys():
toast_key = 'toast_' + item['name']

View File

@ -48,8 +48,7 @@ class JobModule(CollectionNodeModule):
return servers.ServerModule.NODE_TYPE
def BackendSupported(self, manager, **kwargs):
if hasattr(self, 'show_node'):
if not self.show_node:
if hasattr(self, 'show_node') and not self.show_node:
return False
conn = manager.connection()
@ -576,10 +575,9 @@ SELECT EXISTS(
format_schedule_data(changed_schedule)
has_connection_str = self.manager.db_info['pgAgent']['has_connstr']
if 'jsteps' in data and has_connection_str:
if 'changed' in data['jsteps']:
if 'jsteps' in data and has_connection_str and \
'changed' in data['jsteps']:
for changed_step in data['jsteps']['changed']:
if 'jstconntype' not in changed_step and (
'jstdbname' in changed_step or
'jstconnstr' in changed_step):

View File

@ -135,8 +135,8 @@ class RoleView(PGChildNodeView):
else:
data[key] = val
if u'rid' not in kwargs or kwargs['rid'] == -1:
if u'rolname' not in data:
if (u'rid' not in kwargs or kwargs['rid'] == -1) and \
u'rolname' not in data:
return precondition_required(
_("Name must be specified.")
)
@ -275,8 +275,7 @@ rolmembership:{
else:
data[u'admins'].append(r[u'role'])
if self.manager.version >= 90200:
if u'seclabels' in data:
if self.manager.version >= 90200 and u'seclabels' in data:
if u'rid' not in kwargs or kwargs['rid'] == -1:
msg = _("""
Security Label must be passed as an array of JSON objects in the following
@ -521,10 +520,10 @@ rolmembership:{
user = self.manager.user_info
if not user['is_superuser'] and \
not user['can_create_role']:
if action != 'update' or 'rid' in kwargs:
if kwargs['rid'] != -1:
if user['id'] != kwargs['rid']:
not user['can_create_role'] and \
(action != 'update' or 'rid' in kwargs) and \
kwargs['rid'] != -1 and \
user['id'] != kwargs['rid']:
return forbidden(forbidden_msg)
if fetch_name: