Skip to content

Commit

Permalink
login: simplify login view logic for generating return_url
Browse files Browse the repository at this point in the history
The old code checked request.method == "POST" twice, it can be simplified now to only check that once, and still do exactly the same thing.
  • Loading branch information
robvdl committed Mar 11, 2024
1 parent a998da3 commit f512c14
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/sambal/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@
@forbidden_view_config(accept="text/html", renderer="login.jinja2")
def login(request):
"""Login form."""
# Avoid looping the login page if accessed directly.
# Because the app also uses traversal request.matched_route can be None.
if request.method == "POST":
return_url = request.POST.get("return_url", request.path)
elif request.matched_route and request.matched_route.name == "login":
return_url = request.route_path("home")
else:
return_url = request.path

if request.method == "POST":
if (form := LoginForm(request.POST)) and form.validate():
username = form.username.data
password = form.password.data
Expand All @@ -35,6 +28,13 @@ def login(request):
msg = e.args[1]
request.session.flash(f"Login failed: {msg}", queue="error")
else:
# Avoid looping the login page if accessed directly.
# Also, as the app uses traversal request.matched_route can be None.
if request.matched_route and request.matched_route.name == "login":
return_url = request.route_path("home")
else:
return_url = request.path

form = LoginForm()

return {
Expand Down

0 comments on commit f512c14

Please sign in to comment.