Skip to content

Commit

Permalink
public ui: fix menu username
Browse files Browse the repository at this point in the history
* Builds the menu during the first request.
* Store the logged user name in the session.
* Makes the translations and the logged user dynamic.
* Puts the menu generation code in a specific file.
* Closes: #2168.

Co-Authored-by: Johnny Mariéthoz <[email protected]>
  • Loading branch information
jma committed Aug 11, 2021
1 parent 21e46e8 commit 487f8c1
Show file tree
Hide file tree
Showing 4 changed files with 381 additions and 319 deletions.
6 changes: 6 additions & 0 deletions rero_ils/modules/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jinja2
from flask import Blueprint
from flask_bootstrap import Bootstrap
from flask_login.signals import user_loaded_from_cookie, user_logged_in
from flask_wiki import Wiki
from invenio_circulation.signals import loan_state_changed
from invenio_indexer.signals import before_record_index
Expand Down Expand Up @@ -57,6 +58,7 @@
from .sru.views import SRUDocumentsSearch
from .templates.listener import prepare_template_data
from .users.views import UsersCreateResource, UsersResource
from .utils import set_user_name
from ..filter import empty_data, format_date_filter, jsondumps, node_assets, \
text_to_id, to_pretty_json

Expand Down Expand Up @@ -215,3 +217,7 @@ def register_signals(self, app):

# invenio-userprofiles signal
after_profile_update.connect(update_from_profile)

# store the username in the session
user_logged_in.connect(set_user_name)
user_loaded_from_cookie.connect(set_user_name)
20 changes: 19 additions & 1 deletion rero_ils/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import requests
import sqlalchemy
from dateutil import parser
from flask import current_app
from flask import current_app, session
from flask_login import current_user
from invenio_cache.proxies import current_cache
from invenio_pidstore.models import PersistentIdentifier
from invenio_records_rest.utils import obj_or_import_string
Expand Down Expand Up @@ -1032,3 +1033,20 @@ def write(self, data):
def close(self):
"""Close file."""
self.__del__()


def set_user_name(sender, user):
"""Set the username in the current flask session."""
from .patrons.api import current_librarian, current_patrons
user_name = None
if current_librarian:
user_name = current_librarian.formatted_name
elif current_patrons:
user_name = current_patrons[0].formatted_name
else:
try:
user_name = current_user.email
# AnonymousUser
except AttributeError:
pass
session['user_name'] = user_name
Loading

0 comments on commit 487f8c1

Please sign in to comment.