use a dict for project list

this avoid finding duplicates, and we only need id -> name

also, use a common function to avoid duplicate code

fix #1081
This commit is contained in:
Glandos 2022-10-22 22:49:34 +02:00
parent cc18986b76
commit dc40c8fc14
2 changed files with 18 additions and 20 deletions

View file

@ -105,10 +105,10 @@
<li><a class="dropdown-item" href="{{ url_for("main.edit_project") }}">{{ _("Settings") }}</a></li> <li><a class="dropdown-item" href="{{ url_for("main.edit_project") }}">{{ _("Settings") }}</a></li>
{% endif %} {% endif %}
{% if session['projects'] and not ((session['projects'] | length) == 1 and g.project and session['projects'][0][0] == g.project.id) %} {% if session['projects'] and not ((session['projects'] | length) == 1 and g.project and g.project.id in session['projects']) %}
<li class="dropdown-divider"></li> <li class="dropdown-divider"></li>
<li class="dropdown-header">{{ _('Other projects :') }}</li> <li class="dropdown-header">{{ _('Other projects :') }}</li>
{% for id, name in session['projects'] %} {% for id, name in session['projects'].items() %}
{% if not g.project or id != g.project.id %} {% if not g.project or id != g.project.id %}
<li><a class="dropdown-item" href="{{ url_for("main.list_bills", project_id=id) }}">{{ _("switch to") }} {{ name }}</a></li> <li><a class="dropdown-item" href="{{ url_for("main.list_bills", project_id=id) }}">{{ _("switch to") }} {{ name }}</a></li>
{% endif %} {% endif %}

View file

@ -205,6 +205,20 @@ def admin():
) )
def set_authorized_project(project: Project):
# maintain a list of visited projects
new_project = {project.id: project.name}
if "projects" not in session:
session["projects"] = new_project
else:
# add the project on the top of the list
session["projects"] = {**new_project, **session["projects"]}
session[project.id] = True
# Set session to permanent to make language choice persist
session.permanent = True
session.update()
@main.route("/<project_id>/join/<string:token>", methods=["GET"]) @main.route("/<project_id>/join/<string:token>", methods=["GET"])
def join_project(token): def join_project(token):
project_id = g.project.id project_id = g.project.id
@ -215,15 +229,7 @@ def join_project(token):
flash(_("Provided token is invalid"), "danger") flash(_("Provided token is invalid"), "danger")
return redirect("/") return redirect("/")
# maintain a list of visited projects set_authorized_project(g.project)
if "projects" not in session:
session["projects"] = []
# add the project on the top of the list
session["projects"].insert(0, (project_id, g.project.name))
session[project_id] = True
# Set session to permanent to make language choice persist
session.permanent = True
session.update()
return redirect(url_for(".list_bills")) return redirect(url_for(".list_bills"))
@ -252,15 +258,7 @@ def authenticate(project_id=None):
# else do form authentication authentication # else do form authentication authentication
is_post_auth = request.method == "POST" and form.validate() is_post_auth = request.method == "POST" and form.validate()
if is_post_auth and check_password_hash(project.password, form.password.data): if is_post_auth and check_password_hash(project.password, form.password.data):
# maintain a list of visited projects set_authorized_project(project)
if "projects" not in session:
session["projects"] = []
# add the project on the top of the list
session["projects"].insert(0, (project_id, project.name))
session[project_id] = True
# Set session to permanent to make language choice persist
session.permanent = True
session.update()
setattr(g, "project", project) setattr(g, "project", project)
return redirect(url_for(".list_bills")) return redirect(url_for(".list_bills"))
if is_post_auth and not check_password_hash(project.password, form.password.data): if is_post_auth and not check_password_hash(project.password, form.password.data):