Fixed code smell 'String literals should not be duplicated'.

Create a constant.py file which contains the common Constants.
This commit is contained in:
Khushboo Vashi
2020-08-19 14:16:02 +05:30
committed by Akshay Joshi
parent 4b56962c1b
commit cc5a7ea334
11 changed files with 148 additions and 137 deletions

View File

@@ -24,9 +24,12 @@ from pgadmin.utils.menu import MenuItem
from sqlalchemy import exc
from pgadmin.model import db, ServerGroup
SG_NOT_FOUND_ERROR = 'The specified server group could not be found.'
class ServerGroupModule(BrowserPluginModule):
_NODE_TYPE = "server_group"
node_icon = "icon-%s" % _NODE_TYPE
def get_nodes(self, *arg, **kwargs):
"""Return a JSON document listing the server groups for the user"""
@@ -37,7 +40,7 @@ class ServerGroupModule(BrowserPluginModule):
yield self.generate_browser_node(
"%d" % (group.id), None,
group.name,
"icon-%s" % self.node_type,
self.node_icon,
True,
self.node_type,
can_delete=True if idx > 0 else False
@@ -92,6 +95,9 @@ blueprint = ServerGroupModule(__name__)
class ServerGroupView(NodeView):
node_type = ServerGroupModule._NODE_TYPE
node_icon = ServerGroupModule.node_icon
node_label = "Server Group"
parent_ids = []
ids = [{'type': 'int', 'id': 'gid'}]
@@ -136,9 +142,7 @@ class ServerGroupView(NodeView):
return make_json_response(
status=410,
success=0,
errormsg=gettext(
'The specified server group could not be found.'
)
errormsg=gettext(SG_NOT_FOUND_ERROR)
)
else:
try:
@@ -169,9 +173,7 @@ class ServerGroupView(NodeView):
return make_json_response(
status=417,
success=0,
errormsg=gettext(
'The specified server group could not be found.'
)
errormsg=gettext(SG_NOT_FOUND_ERROR)
)
else:
try:
@@ -194,7 +196,7 @@ class ServerGroupView(NodeView):
gid,
None,
servergroup.name,
"icon-%s" % self.node_type,
self.node_icon,
True,
self.node_type,
can_delete=True # This is user created hence can deleted
@@ -214,9 +216,7 @@ class ServerGroupView(NodeView):
return make_json_response(
status=410,
success=0,
errormsg=gettext(
'The specified server group could not be found.'
)
errormsg=gettext(SG_NOT_FOUND_ERROR)
)
else:
return ajax_response(
@@ -246,7 +246,7 @@ class ServerGroupView(NodeView):
"%d" % sg.id,
None,
sg.name,
"icon-%s" % self.node_type,
self.node_icon,
True,
self.node_type,
# This is user created hence can deleted
@@ -306,7 +306,7 @@ class ServerGroupView(NodeView):
"%d" % group.id,
None,
group.name,
"icon-%s" % self.node_type,
self.node_icon,
True,
self.node_type
)
@@ -322,7 +322,7 @@ class ServerGroupView(NodeView):
nodes = self.blueprint.generate_browser_node(
"%d" % (group.id), None,
group.name,
"icon-%s" % self.node_type,
self.node_icon,
True,
self.node_type
)

View File

@@ -30,6 +30,7 @@ from pgadmin.utils.exception import CryptKeyMissing
from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry
from psycopg2 import Error as psycopg2_Error, OperationalError
from pgadmin.browser.server_groups.servers.utils import is_valid_ipaddress
from pgadmin.utils.constants import UNAUTH_REQ
def has_any(data, keys):
@@ -240,6 +241,7 @@ blueprint = ServerModule(__name__)
class ServerNode(PGChildNodeView):
node_type = ServerModule._NODE_TYPE
node_label = "Server"
parent_ids = [{'type': 'int', 'id': 'gid'}]
ids = [{'type': 'int', 'id': 'sid'}]
@@ -658,7 +660,7 @@ class ServerNode(PGChildNodeView):
return make_json_response(
status=410,
success=0,
errormsg=gettext("Could not find the required server.")
errormsg=self.not_found_error_msg
)
sg = ServerGroup.query.filter_by(
@@ -1004,15 +1006,15 @@ class ServerNode(PGChildNodeView):
# Fetch Server Details
server = Server.query.filter_by(id=sid).first()
if server is None:
return bad_request(gettext("Server not found."))
return bad_request(self.not_found_error_msg)
if current_user and hasattr(current_user, 'id'):
# Fetch User Details.
user = User.query.filter_by(id=current_user.id).first()
if user is None:
return unauthorized(gettext("Unauthorized request."))
return unauthorized(gettext(UNAUTH_REQ))
else:
return unauthorized(gettext("Unauthorized request."))
return unauthorized(gettext(UNAUTH_REQ))
data = {}
if request.form:
@@ -1179,7 +1181,7 @@ class ServerNode(PGChildNodeView):
server = Server.query.filter_by(id=sid).first()
if server is None:
return bad_request(gettext("Server not found."))
return bad_request(self.not_found_error_msg)
# Release Connection
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
@@ -1287,12 +1289,12 @@ class ServerNode(PGChildNodeView):
# Fetch Server Details
server = Server.query.filter_by(id=sid).first()
if server is None:
return bad_request(gettext("Server not found."))
return bad_request(self.not_found_error_msg)
# Fetch User Details.
user = User.query.filter_by(id=current_user.id).first()
if user is None:
return unauthorized(gettext("Unauthorized request."))
return unauthorized(gettext(UNAUTH_REQ))
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
conn = manager.connection()
@@ -1407,7 +1409,7 @@ class ServerNode(PGChildNodeView):
if server is None:
return make_json_response(
success=0,
errormsg=gettext("Could not find the required server.")
errormsg=self.not_found_error_msg
)
try:
@@ -1491,7 +1493,7 @@ class ServerNode(PGChildNodeView):
if server is None:
return make_json_response(
success=0,
errormsg=gettext("Could not find the required server.")
errormsg=self.not_found_error_msg
)
try:
@@ -1566,7 +1568,7 @@ class ServerNode(PGChildNodeView):
if server is None:
return make_json_response(
success=0,
info=gettext("Could not find the required server.")
info=self.not_found_error_msg
)
setattr(server, 'password', None)
@@ -1605,7 +1607,7 @@ class ServerNode(PGChildNodeView):
if server is None:
return make_json_response(
success=0,
info=gettext("Could not find the required server.")
info=self.not_found_error_msg
)
setattr(server, 'tunnel_password', None)

View File

@@ -97,6 +97,7 @@ blueprint = DatabaseModule(__name__)
class DatabaseView(PGChildNodeView):
node_type = blueprint.node_type
node_label = "Database"
parent_ids = [
{'type': 'int', 'id': 'gid'},
@@ -380,7 +381,7 @@ class DatabaseView(PGChildNodeView):
status=200
)
return gone(errormsg=_("Could not find the database on the server."))
return gone(errormsg=self.not_found_error_msg)
@check_precondition(action="properties")
def properties(self, gid, sid, did):
@@ -396,7 +397,7 @@ class DatabaseView(PGChildNodeView):
if len(res['rows']) == 0:
return gone(
_("Could not find the database on the server.")
self.not_found_error_msg
)
SQL = render_template(
@@ -801,7 +802,7 @@ class DatabaseView(PGChildNodeView):
if len(rset['rows']) == 0:
return gone(
_("Could not find the database on the server.")
self.not_found_error_msg
)
res = rset['rows'][0]
@@ -930,7 +931,7 @@ class DatabaseView(PGChildNodeView):
if len(rset['rows']) == 0:
return gone(
_("Could not find the database on the server.")
self.not_found_error_msg
)
data['old_name'] = (rset['rows'][0])['name']
@@ -1101,7 +1102,7 @@ class DatabaseView(PGChildNodeView):
if len(res['rows']) == 0:
return gone(
_("Could not find the database on the server.")
self.not_found_error_msg
)
SQL = render_template(

View File

@@ -149,6 +149,7 @@ class EventTriggerView(PGChildNodeView, SchemaDiffObjectCompare):
"""
node_type = blueprint.node_type
node_icon = "icon-%s" % blueprint.node_type
parent_ids = [
{'type': 'int', 'id': 'gid'},
@@ -258,7 +259,7 @@ class EventTriggerView(PGChildNodeView, SchemaDiffObjectCompare):
row['oid'],
did,
row['name'],
icon="icon-%s" % self.node_type
self.node_icon
))
return make_json_response(
@@ -292,7 +293,7 @@ class EventTriggerView(PGChildNodeView, SchemaDiffObjectCompare):
row['oid'],
did,
row['name'],
icon="icon-%s" % self.node_type
self.node_icon
),
status=200
)
@@ -431,7 +432,7 @@ class EventTriggerView(PGChildNodeView, SchemaDiffObjectCompare):
etid,
did,
data['name'],
icon="icon-%s" % self.node_type
self.node_icon
)
)
except Exception as e:
@@ -478,7 +479,7 @@ class EventTriggerView(PGChildNodeView, SchemaDiffObjectCompare):
etid,
did,
data['name'],
icon="icon-%s" % self.node_type
self.node_icon
)
)
else:

View File

@@ -163,6 +163,7 @@ class ForeignServerView(PGChildNodeView, SchemaDiffObjectCompare):
"""
node_type = blueprint.node_type
node_label = "Foreign Server"
parent_ids = [
{'type': 'int', 'id': 'gid'},
@@ -356,9 +357,7 @@ class ForeignServerView(PGChildNodeView, SchemaDiffObjectCompare):
return False, internal_server_error(errormsg=res)
if len(res['rows']) == 0:
return False, gone(
gettext("Could not find the foreign server information.")
)
return False, gone(self.not_found_error_msg)
res['rows'][0]['is_sys_obj'] = (
res['rows'][0]['oid'] <= self.datlastsysoid)
@@ -651,9 +650,7 @@ class ForeignServerView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
return gone(
gettext("Could not find the foreign server information.")
)
return gone(self.not_found_error_msg)
if res['rows'][0]['fsrvoptions'] is not None:
res['rows'][0]['fsrvoptions'] = tokenize_options(
@@ -758,9 +755,7 @@ class ForeignServerView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
return gone(
gettext("Could not find the foreign server information.")
)
return gone(self.not_found_error_msg)
if fid is None and 'fdwid' in res['rows'][0]:
fid = res['rows'][0]['fdwid']

View File

@@ -179,6 +179,7 @@ class UserMappingView(PGChildNodeView, SchemaDiffObjectCompare):
"""
node_type = blueprint.node_type
node_label = "User Mapping"
parent_ids = [
{'type': 'int', 'id': 'gid'},
@@ -374,9 +375,7 @@ class UserMappingView(PGChildNodeView, SchemaDiffObjectCompare):
return False, internal_server_error(errormsg=res)
if len(res['rows']) == 0:
return False, gone(
gettext("Could not find the user mapping information.")
)
return False, gone(self.not_found_error_msg)
res['rows'][0]['is_sys_obj'] = (
res['rows'][0]['oid'] <= self.datlastsysoid)
@@ -672,9 +671,7 @@ class UserMappingView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
return gone(
gettext("Could not find the user mapping information.")
)
return gone(self.not_found_error_msg)
if res['rows'][0]['umoptions'] is not None:
res['rows'][0]['umoptions'] = tokenize_options(
@@ -769,9 +766,7 @@ class UserMappingView(PGChildNodeView, SchemaDiffObjectCompare):
if not status:
return internal_server_error(errormsg=res)
if len(res['rows']) == 0:
return gone(
gettext("Could not find the user mapping information.")
)
return gone(self.not_found_error_msg)
if fsid is None and 'fsid' in res['rows'][0]:
fsid = res['rows'][0]['fsid']