Skip to content

Commit

Permalink
refactor: move session related methods in a dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Nov 6, 2024
1 parent f4e222a commit fe8e116
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 58 deletions.
2 changes: 1 addition & 1 deletion canaille/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def make_session_permanent():

@app.context_processor
def global_processor():
from canaille.app.flask import current_user
from canaille.app.session import current_user

return {
"debug": app.debug or app.config.get("TESTING", False),
Expand Down
49 changes: 1 addition & 48 deletions canaille/app/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,14 @@

from flask import abort
from flask import current_app
from flask import g
from flask import request
from flask import session
from werkzeug.routing import BaseConverter

from canaille.app import models
from canaille.app.i18n import gettext as _
from canaille.app.session import current_user
from canaille.app.themes import render_template


def current_user():
if "user" in g:
return g.user

for user_id in session.get("user_id", [])[::-1]:
user = current_app.backend.instance.get(models.User, user_id)
if user and (
not current_app.backend.has_account_lockability() or not user.locked
):
g.user = user
return g.user

session["user_id"].remove(user_id)

if "user_id" in session and not session["user_id"]:
del session["user_id"]

return None


def login_user(user):
"""Open a session for the user."""
g.user = user
try:
previous = (
session["user_id"]
if isinstance(session["user_id"], list)
else [session["user_id"]]
)
session["user_id"] = previous + [user.id]
except KeyError:
session["user_id"] = [user.id]


def logout_user():
"""Close the user session."""
try:
session["user_id"].pop()
del g.user
if not session["user_id"]:
del session["user_id"]
except (IndexError, KeyError):
pass


def user_needed():
def wrapper(view_function):
@wraps(view_function)
Expand Down
2 changes: 1 addition & 1 deletion canaille/app/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def global_processor():


def locale_selector():
from .flask import current_user
from .session import current_user

user = current_user()
available_language_codes = getattr(g, "available_language_codes", [])
Expand Down
50 changes: 50 additions & 0 deletions canaille/app/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from flask import current_app
from flask import g
from flask import session

from canaille.app import models


def current_user():
if "user" in g:
return g.user

for user_id in session.get("user_id", [])[::-1]:
user = current_app.backend.instance.get(models.User, user_id)
if user and (
not current_app.backend.has_account_lockability() or not user.locked
):
g.user = user
return g.user

session["user_id"].remove(user_id)

if "user_id" in session and not session["user_id"]:
del session["user_id"]

return None


def login_user(user):
"""Open a session for the user."""
g.user = user
try:
previous = (
session["user_id"]
if isinstance(session["user_id"], list)
else [session["user_id"]]
)
session["user_id"] = previous + [user.id]
except KeyError:
session["user_id"] = [user.id]


def logout_user():
"""Close the user session."""
try:
session["user_id"].pop()
del g.user
if not session["user_id"]:
del session["user_id"]
except (IndexError, KeyError):
pass
6 changes: 3 additions & 3 deletions canaille/core/endpoints/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
from canaille.app import default_fields
from canaille.app import models
from canaille.app import obj_to_b64
from canaille.app.flask import current_user
from canaille.app.flask import login_user
from canaille.app.flask import logout_user
from canaille.app.flask import permissions_needed
from canaille.app.flask import render_htmx_template
from canaille.app.flask import request_is_htmx
Expand All @@ -41,6 +38,9 @@
from canaille.app.forms import set_writable
from canaille.app.i18n import gettext as _
from canaille.app.i18n import reload_translations
from canaille.app.session import current_user
from canaille.app.session import login_user
from canaille.app.session import logout_user
from canaille.app.themes import render_template
from canaille.backends import Backend

Expand Down
6 changes: 3 additions & 3 deletions canaille/core/endpoints/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from flask import url_for

from canaille.app import build_hash
from canaille.app.flask import current_user
from canaille.app.flask import login_user
from canaille.app.flask import logout_user
from canaille.app.flask import smtp_needed
from canaille.app.i18n import gettext as _
from canaille.app.session import current_user
from canaille.app.session import login_user
from canaille.app.session import logout_user
from canaille.app.themes import render_template
from canaille.backends import Backend

Expand Down
4 changes: 2 additions & 2 deletions canaille/oidc/endpoints/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

from canaille import csrf
from canaille.app import models
from canaille.app.flask import current_user
from canaille.app.flask import logout_user
from canaille.app.flask import set_parameter_in_url_query
from canaille.app.i18n import gettext as _
from canaille.app.session import current_user
from canaille.app.session import logout_user
from canaille.app.themes import render_template
from canaille.backends import Backend

Expand Down

0 comments on commit fe8e116

Please sign in to comment.