Skip to content

Commit

Permalink
user.py - string.format to fstrings #2634
Browse files Browse the repository at this point in the history
Incidental update from older % string formatter included.
  • Loading branch information
phillxnet committed Jul 9, 2024
1 parent 5b1a6e2 commit f02ea7f
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/rockstor/storageadmin/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def _validate_input(cls, request):
input_fields = {}
username = request.data.get("username", None)
if username is None or re.match(settings.USERNAME_REGEX, username) is None:
e_msg = ("Username is invalid. It must conform to the regex: ({}).").format(
settings.USERNAME_REGEX
)
e_msg = f"Username is invalid. It must conform to the regex: ({settings.USERNAME_REGEX})."
handle_exception(Exception(e_msg), request, status_code=400)
if len(username) > 30:
e_msg = "Username cannot be more than 30 characters long."
Expand All @@ -104,30 +102,28 @@ def _validate_input(cls, request):
input_fields["admin"] = admin
shell = request.data.get("shell", "/bin/bash")
if shell not in settings.VALID_SHELLS:
e_msg = ("Element shell ({}) is not valid. Valid shells are {}.").format(
shell, settings.VALID_SHELLS
)
e_msg = f"Element shell ({shell}) is not valid. Valid shells are {settings.VALID_SHELLS}."
handle_exception(Exception(e_msg), request, status_code=400)
input_fields["shell"] = shell
email = request.data.get("email", None)
input_fields["email"] = email
input_fields["homedir"] = request.data.get("homedir", "/home/%s" % username)
input_fields["homedir"] = request.data.get("homedir", f"/home/{username}")
input_fields["uid"] = request.data.get("uid", None)
if input_fields["uid"] is not None:
try:
input_fields["uid"] = int(input_fields["uid"])
except ValueError as e:
e_msg = ("UID must be an integer, try again. Exception: ({}).").format(
e.__str__()
e_msg = (
f"UID must be an integer, try again. Exception: ({e.__str__()})."
)
handle_exception(Exception(e_msg), request, status_code=400)
input_fields["gid"] = request.data.get("gid", None)
if input_fields["gid"] is not None:
try:
input_fields["gid"] = int(input_fields["gid"])
except ValueError as e:
e_msg = ("GID must be an integer, try again. Exception: ({}).").format(
e.__str__()
e_msg = (
f"GID must be an integer, try again. Exception: ({e.__str__()})."
)
handle_exception(Exception(e_msg), request, status_code=400)
input_fields["group"] = request.data.get("group", None)
Expand All @@ -153,17 +149,13 @@ def get_queryset(self, *args, **kwargs):
@transaction.atomic
def post(self, request):
with self._handle_exception(request):

invar = self._validate_input(request)
# Check that a django user with the same name does not exist
e_msg = (
"User ({}) already exists. Please choose a different username."
).format(invar["username"])
e_msg = f"User ({invar['username']}) already exists. Please choose a different username."
if (
DjangoUser.objects.filter(username=invar["username"]).exists()
or User.objects.filter(username=invar["username"]).exists()
):

handle_exception(Exception(e_msg), request, status_code=400)
users = combined_users()
groups = combined_groups()
Expand All @@ -187,9 +179,7 @@ def post(self, request):
if u.username == invar["username"]:
handle_exception(Exception(e_msg), request, status_code=400)
elif u.uid == invar["uid"]:
e_msg = (
"UID ({}) already exists. Please choose a different one."
).format(invar["uid"])
e_msg = f"UID ({invar['uid']}) already exists. Please choose a different one."
handle_exception(Exception(e_msg), request)

if invar["admin"]:
Expand Down Expand Up @@ -254,9 +244,7 @@ def put(self, request, username):
with self._handle_exception(request):
if username in self.exclude_list:
if username != "root":
e_msg = ("Editing restricted user ({}) is not supported.").format(
username
)
e_msg = f"Editing restricted user ({username}) is not supported."
handle_exception(Exception(e_msg), request)
email = request.data.get("email", None)
new_pw = request.data.get("password", None)
Expand Down Expand Up @@ -313,7 +301,7 @@ def put(self, request, username):
add_ssh_key(username, public_key, cur_public_key)
break
if suser is None:
e_msg = "User ({}) does not exist.".format(username)
e_msg = f"User ({username}) does not exist."
handle_exception(Exception(e_msg), request)

return Response(SUserSerializer(suser).data)
Expand All @@ -326,9 +314,7 @@ def delete(self, request, username):
handle_exception(Exception(e_msg), request)

if username in self.exclude_list:
e_msg = ("Delete of restricted user ({}) is not supported.").format(
username
)
e_msg = f"Delete of restricted user ({username}) is not supported."
handle_exception(Exception(e_msg), request)

gid = None
Expand All @@ -346,7 +332,7 @@ def delete(self, request, username):
found = True
break
if found is False:
e_msg = "User ({}) does not exist.".format(username)
e_msg = f"User ({username}) does not exist."
handle_exception(Exception(e_msg), request)

for g in combined_groups():
Expand All @@ -365,8 +351,8 @@ def delete(self, request, username):
except Exception as e:
logger.exception(e)
e_msg = (
"A low level error occurred while deleting the user ({})."
).format(username)
f"A low level error occurred while deleting the user ({username})."
)
handle_exception(Exception(e_msg), request)

return Response()

0 comments on commit f02ea7f

Please sign in to comment.