Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions invenio_requests/records/systemfields/expired_state.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# -*- 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.

"""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


Expand All @@ -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
6 changes: 4 additions & 2 deletions invenio_requests/services/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions invenio_requests/tasks.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions tests/records/systemfields/test_calculated_systemfield.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions tests/services/requests/test_requests_tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- 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
# details.

"""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
Expand All @@ -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(
Expand Down
Loading