Skip to content

Commit

Permalink
membership-requests [#855]: serialize expires_at to UTC-offset dateti…
Browse files Browse the repository at this point in the history
…me string
  • Loading branch information
fenekku committed Jul 30, 2024
1 parent 3b877d2 commit 090dc0e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
19 changes: 15 additions & 4 deletions invenio_communities/members/services/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
CommunityInvitationDeclineNotificationBuilder,
CommunityInvitationExpireNotificationBuilder,
CommunityInvitationSubmittedNotificationBuilder,
CommunityMembershipRequestAcceptNotificationBuilder,
CommunityMembershipRequestCancelNotificationBuilder,
CommunityMembershipRequestDeclineNotificationBuilder,
CommunityMembershipRequestExpireNotificationBuilder,
CommunityMembershipRequestSubmittedNotificationBuilder,
)
from invenio_communities.proxies import current_communities
Expand Down Expand Up @@ -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,
}

Expand Down
3 changes: 2 additions & 1 deletion tests/members/test_members_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 090dc0e

Please sign in to comment.