From 08b8d4d6b52a457d105436742bf31986693b9f4a Mon Sep 17 00:00:00 2001 From: Guillaume Viger Date: Mon, 29 Jul 2024 10:56:00 -0400 Subject: [PATCH] membership-requests [#855]: serialize expires_at to UTC-offset datetime string --- .../members/services/schemas.py | 19 +++++++++++++++---- .../details/members/membership_requests.html | 2 +- tests/conftest.py | 8 ++++++++ tests/members/test_members_services.py | 3 ++- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/invenio_communities/members/services/schemas.py b/invenio_communities/members/services/schemas.py index 8807215b0..d03b13806 100644 --- a/invenio_communities/members/services/schemas.py +++ b/invenio_communities/members/services/schemas.py @@ -12,6 +12,7 @@ from datetime import timezone from types import SimpleNamespace +import arrow from invenio_i18n import lazy_gettext as _ from invenio_users_resources.proxies import ( current_groups_service, @@ -54,12 +55,22 @@ class RequestSchema(Schema): id = fields.String() status = fields.String() is_open = fields.Boolean() - # TODO: expires_at is dumped in the index and thus a string. This is - # because the relations field doesn't properly load data from the index - # (it should have converted expires_at into a datetime object). - expires_at = fields.String() + expires_at = fields.Method(serialize="serialize_expires_at") type = fields.String() + def serialize_expires_at(self, obj): + """Makes sure that the expires_at datetime is serialized into a UTC offset str. + + As of writing, the input `expires_at` is a naive datetime string because + relations field doesn't convert loaded data from the index. We want an aware + UTC timezoned datetime string in ISO format. + + :param obj: Request dict + :return: ISO datetime string + """ + aware = arrow.get(obj["expires_at"]).to(timezone.utc) + return aware.isoformat() + # # Schemas used for validation diff --git a/invenio_communities/templates/semantic-ui/invenio_communities/details/members/membership_requests.html b/invenio_communities/templates/semantic-ui/invenio_communities/details/members/membership_requests.html index 5577f02bf..b86f3de4a 100644 --- a/invenio_communities/templates/semantic-ui/invenio_communities/details/members/membership_requests.html +++ b/invenio_communities/templates/semantic-ui/invenio_communities/details/members/membership_requests.html @@ -15,8 +15,8 @@ {{ webpack['invenio-communities-membership-requests.js'] }} {%- endblock javascript %} -{% set active_members_menu_item = 'membership_requests' %} {% set active_community_header_menu_item= 'members' %} +{% set active_members_menu_item = 'membership_requests' %} {%- block settings_body %} diff --git a/tests/conftest.py b/tests/conftest.py index a1d004952..449edf515 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,6 +42,10 @@ CommunityInvitationDeclineNotificationBuilder, CommunityInvitationExpireNotificationBuilder, CommunityInvitationSubmittedNotificationBuilder, + CommunityMembershipRequestAcceptNotificationBuilder, + CommunityMembershipRequestCancelNotificationBuilder, + CommunityMembershipRequestDeclineNotificationBuilder, + CommunityMembershipRequestExpireNotificationBuilder, CommunityMembershipRequestSubmittedNotificationBuilder, ) from invenio_communities.proxies import current_communities @@ -124,6 +128,10 @@ def app_config(app_config): CommunityInvitationDeclineNotificationBuilder.type: CommunityInvitationDeclineNotificationBuilder, CommunityInvitationExpireNotificationBuilder.type: CommunityInvitationExpireNotificationBuilder, CommunityInvitationSubmittedNotificationBuilder.type: CommunityInvitationSubmittedNotificationBuilder, + CommunityMembershipRequestAcceptNotificationBuilder.type: CommunityMembershipRequestAcceptNotificationBuilder, + CommunityMembershipRequestCancelNotificationBuilder.type: CommunityMembershipRequestCancelNotificationBuilder, + CommunityMembershipRequestDeclineNotificationBuilder.type: CommunityMembershipRequestDeclineNotificationBuilder, + CommunityMembershipRequestExpireNotificationBuilder.type: CommunityMembershipRequestExpireNotificationBuilder, CommunityMembershipRequestSubmittedNotificationBuilder.type: CommunityMembershipRequestSubmittedNotificationBuilder, } diff --git a/tests/members/test_members_services.py b/tests/members/test_members_services.py index 7283ec909..a27c0b3fa 100644 --- a/tests/members/test_members_services.py +++ b/tests/members/test_members_services.py @@ -1402,7 +1402,8 @@ def test_request_membership_expire_flow( hit = hits["hits"][0] assert "expired" == hit["request"]["status"] assert hit["request"]["is_open"] is False - + # `expires_at`` is UTC-offset + assert hit["request"]["expires_at"].endswith("+00:00") # # Change notifications