diff --git a/invenio_requests/records/systemfields/expired_state.py b/invenio_requests/records/systemfields/expired_state.py index e3be42b6..06be37e4 100644 --- a/invenio_requests/records/systemfields/expired_state.py +++ b/invenio_requests/records/systemfields/expired_state.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # # Copyright (C) 2021 - 2022 TU Wien. +# Copyright (C) 2025 Graz University of Technology. # # Invenio-Requests is free software; you can redistribute it and/or modify # it under the terms of the MIT License; see LICENSE file for more details. @@ -8,9 +9,9 @@ """Systemfield for calculating the ``is_expired`` property of a request.""" -from datetime import datetime +from datetime import datetime, timezone -import pytz +import arrow from invenio_records_resources.records.systemfields.calculated import CalculatedField @@ -29,10 +30,7 @@ def calculate(self, record): if expires_at is None: return False - # comparing timezone-aware and naive datetimes results in an error - # https://docs.python.org/3/library/datetime.html#determining-if-an-object-is-aware-or-naive # noqa - now = datetime.utcnow() - if expires_at.tzinfo and expires_at.tzinfo.utcoffset(expires_at) is not None: - now = now.replace(tzinfo=pytz.utc) + expires_at = arrow.get(expires_at, tzinfo=timezone.utc).datetime + now = datetime.now(timezone.utc) return expires_at < now diff --git a/invenio_requests/services/schemas.py b/invenio_requests/services/schemas.py index 5fce46a9..10fec382 100644 --- a/invenio_requests/services/schemas.py +++ b/invenio_requests/services/schemas.py @@ -18,6 +18,7 @@ fields, ) from marshmallow_utils import fields as utils_fields +from marshmallow_utils.context import context_schema from invenio_requests.proxies import current_requests @@ -43,14 +44,15 @@ class RequestEventSchema(BaseRecordSchema): def get_permissions(self, obj): """Return permissions to act on comments or empty dict.""" is_comment = obj.type == CommentEventType + current_identity = context_schema.get()["identity"] if is_comment: service = current_requests.request_events_service return { "can_update_comment": service.check_permission( - self.context["identity"], "update_comment", event=obj + current_identity, "update_comment", event=obj ), "can_delete_comment": service.check_permission( - self.context["identity"], "delete_comment", event=obj + current_identity, "delete_comment", event=obj ), } else: diff --git a/invenio_requests/tasks.py b/invenio_requests/tasks.py index 2667bdf5..29ef1279 100644 --- a/invenio_requests/tasks.py +++ b/invenio_requests/tasks.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2022 Graz University of Technology. +# Copyright (C) 2022-2025 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. """Celery tasks for requests.""" -from datetime import datetime +from datetime import datetime, timezone from celery import shared_task from flask import current_app @@ -25,7 +25,7 @@ def check_expired_requests(): """Retrieve expired requests and perform expired action.""" service = current_requests_service - now = datetime.utcnow().isoformat() + now = datetime.now(timezone.utc).isoformat() # using scan to get all requests requests_list = service.scan( diff --git a/tests/records/systemfields/test_calculated_systemfield.py b/tests/records/systemfields/test_calculated_systemfield.py index 0c58baca..2bc5fcec 100644 --- a/tests/records/systemfields/test_calculated_systemfield.py +++ b/tests/records/systemfields/test_calculated_systemfield.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 -*- # # Copyright (C) 2021 TU Wien. +# Copyright (C) 2025 Graz University of Technology. # # Invenio-Requests is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. """Test the calculated systemfields.""" -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone import pytest @@ -16,7 +17,7 @@ def test_expired_systemfield(example_request): """Test if the expired system field works as intended.""" - now = datetime.utcnow() + now = datetime.now(timezone.utc) example_request.expires_at = None example_request.commit() diff --git a/tests/services/requests/test_requests_tasks.py b/tests/services/requests/test_requests_tasks.py index cd4f24ec..b485c2c5 100644 --- a/tests/services/requests/test_requests_tasks.py +++ b/tests/services/requests/test_requests_tasks.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2022 Graz University of Technology. +# Copyright (C) 2022-2025 Graz University of Technology. # # Invenio-Requests is free software; you can redistribute it and/or # modify it under the terms of the MIT License; see LICENSE file for more @@ -8,7 +8,7 @@ """Tasks tests.""" -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from invenio_access.permissions import system_identity from invenio_search.engine import dsl @@ -21,7 +21,7 @@ def test_check_expired_requests( app, identity_simple, create_request, submit_request, requests_service ): """Test if the expired system field works as intended.""" - now = datetime.utcnow() + now = datetime.now(timezone.utc) # created only should not be picked up created_request = create_request(