From b5cb3ec2ea8ccbafe1ac182f3c05a720d690fdbf Mon Sep 17 00:00:00 2001 From: Hrafn Malmquist Date: Mon, 25 Nov 2024 12:34:24 +0000 Subject: [PATCH 1/2] Add logged in user as default creator in submission form --- pyproject.toml | 1 + site/ic_data_repo/config/settings.py | 2 ++ site/ic_data_repo/config/utils.py | 38 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 site/ic_data_repo/config/utils.py diff --git a/pyproject.toml b/pyproject.toml index cc9d395..9d5cea4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ exclude = "test_data/.*\\.py" [[tool.mypy.overrides]] module = [ + "flask_login.*", "flask_oauthlib.*", "invenio_app.*", "invenio_assets.*", diff --git a/site/ic_data_repo/config/settings.py b/site/ic_data_repo/config/settings.py index 72542be..790835e 100644 --- a/site/ic_data_repo/config/settings.py +++ b/site/ic_data_repo/config/settings.py @@ -12,6 +12,7 @@ from invenio_oauthclient.views.client import auto_redirect_login from .custom_fields import * # noqa: F401,F403 +from .utils import get_user # Flask # ===== @@ -113,6 +114,7 @@ } ], "publisher": "Imperial College London", + "creators": lambda: get_user(), } # See: diff --git a/site/ic_data_repo/config/utils.py b/site/ic_data_repo/config/utils.py new file mode 100644 index 0000000..27207fe --- /dev/null +++ b/site/ic_data_repo/config/utils.py @@ -0,0 +1,38 @@ +"""Utilities for settings.""" + +from typing import Any, List, Optional + +from flask_login import current_user + + +def get_user() -> Optional[List[dict[str, Any]]]: + """Format the current user profile for the submission form. + + The default user profile schema has two string properties; + full name and affiliations. More details (identifiers?) would + have to come from the SSO. + https://github.com/inveniosoftware/invenio-accounts/blob/master/invenio_accounts/profiles/schemas.py + """ + affiliations = [] + + try: + name = current_user.user_profile["full_name"] + given_name = name.split(", ")[1] + family_name = name.split(", ")[0] + except KeyError: + return [] + + if "affiliations" in current_user.user_profile: + affiliations.append({"name": current_user.user_profile["affiliations"]}) + + return [ + { + "person_or_org": { + "type": "personal", + "name": name, + "given_name": given_name, + "family_name": family_name, + }, + "affiliations": affiliations, + }, + ] From 0971f69798b248ed5b757944097124031fc36270 Mon Sep 17 00:00:00 2001 From: Hrafn Malmquist Date: Mon, 9 Dec 2024 10:16:22 +0000 Subject: [PATCH 2/2] Remove optional typing hint, rename helper function --- site/ic_data_repo/config/settings.py | 4 ++-- site/ic_data_repo/config/utils.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/site/ic_data_repo/config/settings.py b/site/ic_data_repo/config/settings.py index 790835e..fea3efe 100644 --- a/site/ic_data_repo/config/settings.py +++ b/site/ic_data_repo/config/settings.py @@ -12,7 +12,7 @@ from invenio_oauthclient.views.client import auto_redirect_login from .custom_fields import * # noqa: F401,F403 -from .utils import get_user +from .utils import get_user_form_default # Flask # ===== @@ -114,7 +114,7 @@ } ], "publisher": "Imperial College London", - "creators": lambda: get_user(), + "creators": lambda: get_user_form_default(), } # See: diff --git a/site/ic_data_repo/config/utils.py b/site/ic_data_repo/config/utils.py index 27207fe..27e2fa1 100644 --- a/site/ic_data_repo/config/utils.py +++ b/site/ic_data_repo/config/utils.py @@ -1,11 +1,11 @@ """Utilities for settings.""" -from typing import Any, List, Optional +from typing import Any, List from flask_login import current_user -def get_user() -> Optional[List[dict[str, Any]]]: +def get_user_form_default() -> List[dict[str, Any]]: """Format the current user profile for the submission form. The default user profile schema has two string properties;