Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DeprecationWarning: datetime.datetime.utcnow #39

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 8 additions & 4 deletions invenio_banners/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"""Models."""

from datetime import datetime
from datetime import datetime, timezone

import sqlalchemy as sa
from flask import current_app
Expand Down Expand Up @@ -36,7 +36,11 @@ class BannerModel(db.Model, Timestamp):
category = db.Column(db.String(20), nullable=False)
"""Category of the message, for styling messages per category."""

start_datetime = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
start_datetime = db.Column(
db.DateTime,
nullable=False,
default=lambda: datetime.now(timezone.utc).replace(tzinfo=None),
)
"""Start date and time (UTC), can be immediate or delayed."""

end_datetime = db.Column(db.DateTime, nullable=True)
Expand Down Expand Up @@ -90,7 +94,7 @@ def delete(cls, banner):
@classmethod
def get_active(cls, url_path):
"""Return active banners."""
now = datetime.utcnow()
now = datetime.now(timezone.utc).replace(tzinfo=None)

query = (
db.session.query(cls)
Expand Down Expand Up @@ -130,7 +134,7 @@ def search(cls, search_params, filters):
@classmethod
def disable_expired(cls):
"""Disable any old still active messages to keep everything clean."""
now = datetime.utcnow()
now = datetime.now(timezone.utc).replace(tzinfo=None)

query = (
db.session.query(cls)
Expand Down
7 changes: 6 additions & 1 deletion invenio_banners/services/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022-2023 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio-Banners is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand All @@ -22,7 +23,11 @@ class BannerSchema(BaseRecordSchema):
category = fields.String(required=True, metadata={"default": "info"})
start_datetime = fields.DateTime(
required=True,
metadata={"default": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")},
metadata={
"default": datetime.now(timezone.utc)
.replace(tzinfo=None)
.strftime("%Y-%m-%d %H:%M:%S")
},
)
end_datetime = fields.DateTime(allow_none=True)
active = fields.Boolean(required=True, metadata={"default": True})
Expand Down
16 changes: 11 additions & 5 deletions tests/records/test_disable.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio-Banners 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 disable expired."""

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

from invenio_banners.records.models import BannerModel


def _now():
return datetime.now(timezone.utc).replace(tzinfo=None)


banners = {
"valid": {
"message": "valid",
"url_path": "/valid",
"category": "warning",
"end_datetime": datetime.utcnow() + timedelta(days=1),
"end_datetime": _now() + timedelta(days=1),
"active": True,
},
"everywhere": {
"message": "everywhere",
"url_path": None,
"category": "info",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": True,
},
"sub_records_only": {
"message": "sub_records_only",
"url_path": "/resources/sub",
"category": "info",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": True,
},
"expired": {
"message": "expired",
"url_path": "/expired",
"category": "info",
"end_datetime": datetime.utcnow() - timedelta(days=1),
"end_datetime": _now() - timedelta(days=1),
"active": True,
},
}
Expand Down
22 changes: 14 additions & 8 deletions tests/records/test_models.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020-2023 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio-Banners 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 models."""

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

import pytest

from invenio_banners.records.models import BannerModel
from invenio_banners.services.errors import BannerNotExistsError


def _now():
return datetime.now(timezone.utc).replace(tzinfo=None)


banners = {
"valid": {
"message": "valid",
"url_path": "/valid",
"category": "info",
"end_datetime": datetime.utcnow() + timedelta(days=1),
"end_datetime": _now() + timedelta(days=1),
"active": True,
},
"everywhere": {
"message": "everywhere",
"url_path": None,
"category": "info",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": True,
},
"with_end_datetime": {
"message": "with_end_datetime",
"url_path": "/with_end_datetime",
"category": "info",
"end_datetime": datetime.utcnow() - timedelta(days=1),
"end_datetime": _now() - timedelta(days=1),
"active": True,
},
"records_only": {
"message": "records_only",
"url_path": "/resources",
"category": "info",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": True,
},
"sub_records_only": {
"message": "sub_records_only",
"url_path": "/resources/sub",
"category": "warning",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": True,
},
"disabled": {
"message": "disabled",
"url_path": "/disabled",
"category": "info",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": False,
},
"expired": {
"message": "expired",
"url_path": "/expired",
"category": "warning",
"end_datetime": datetime.utcnow() - timedelta(days=1),
"end_datetime": _now() - timedelta(days=1),
"active": True,
},
}
Expand Down
27 changes: 12 additions & 15 deletions tests/resources/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,51 @@
# under the terms of the MIT License; see LICENSE file for more details.

"""Banner resource tests."""
from datetime import date, datetime, timedelta
from datetime import date, datetime, timedelta, timezone

import pytest
from invenio_db import db
from invenio_records_resources.services.errors import PermissionDeniedError

from invenio_banners.records import BannerModel


def _now():
return datetime.now(timezone.utc).replace(tzinfo=None)


banners = {
"banner0": {
"message": "banner0",
"url_path": "/banner0",
"category": "info",
"active": True,
"start_datetime": date(2022, 7, 20).strftime("%Y-%m-%d %H:%M:%S"),
"end_datetime": (datetime.utcnow() - timedelta(days=20)).strftime(
"%Y-%m-%d %H:%M:%S"
),
"end_datetime": (_now() - timedelta(days=20)).strftime("%Y-%m-%d %H:%M:%S"),
},
"banner1": {
"message": "banner1",
"url_path": "/banner1",
"category": "info",
"active": True,
"start_datetime": date(2022, 7, 20).strftime("%Y-%m-%d %H:%M:%S"),
"end_datetime": (datetime.utcnow() + timedelta(days=20)).strftime(
"%Y-%m-%d %H:%M:%S"
),
"end_datetime": (_now() + timedelta(days=20)).strftime("%Y-%m-%d %H:%M:%S"),
},
"banner2": {
"message": "banner2",
"url_path": "/banner2",
"category": "other",
"active": False,
"start_datetime": date(2022, 12, 15).strftime("%Y-%m-%d %H:%M:%S"),
"end_datetime": (datetime.utcnow() + timedelta(days=10)).strftime(
"%Y-%m-%d %H:%M:%S"
),
"end_datetime": (_now() + timedelta(days=10)).strftime("%Y-%m-%d %H:%M:%S"),
},
"banner3": {
"message": "banner3",
"url_path": "/banner3",
"category": "warning",
"active": True,
"start_datetime": date(2023, 1, 20).strftime("%Y-%m-%d %H:%M:%S"),
"end_datetime": (datetime.utcnow() + timedelta(days=30)).strftime(
"%Y-%m-%d %H:%M:%S"
),
"end_datetime": (_now() + timedelta(days=30)).strftime("%Y-%m-%d %H:%M:%S"),
},
}

Expand Down Expand Up @@ -138,7 +135,7 @@ def test_update_banner(client, admin, headers):
admin.login(client)

new_data = {
"start_datetime": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
"start_datetime": _now().strftime("%Y-%m-%d %H:%M:%S"),
"active": True,
"message": "New banner message",
"category": "info",
Expand All @@ -162,7 +159,7 @@ def test_disable_expired_after_update_action(client, admin, headers):
admin.login(client)

new_data = {
"start_datetime": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
"start_datetime": _now().strftime("%Y-%m-%d %H:%M:%S"),
"active": True,
"message": "New banner message",
"category": "info",
Expand Down
23 changes: 13 additions & 10 deletions tests/services/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"""Service level tests for Banners."""

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

import pytest
from invenio_db import db
Expand All @@ -18,15 +18,18 @@
from invenio_banners.records import BannerModel
from invenio_banners.services.errors import BannerNotExistsError


def _now():
return datetime.now(timezone.utc).replace(tzinfo=None)


banners = {
"active": {
"message": "active",
"url_path": "/active",
"category": "info",
"start_datetime": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
"end_datetime": (datetime.utcnow() + timedelta(days=1)).strftime(
"%Y-%m-%d %H:%M:%S"
),
"start_datetime": _now().strftime("%Y-%m-%d %H:%M:%S"),
"end_datetime": (_now() + timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S"),
"active": True,
},
"inactive": {
Expand All @@ -39,28 +42,28 @@
"message": "other",
"url_path": "/other",
"category": "warning",
"end_datetime": datetime.utcnow() + timedelta(days=5),
"end_datetime": _now() + timedelta(days=5),
"active": True,
},
"expired": {
"message": "expired",
"url_path": "/expired",
"category": "info",
"end_datetime": datetime.utcnow() - timedelta(days=1),
"end_datetime": _now() - timedelta(days=1),
"active": True,
},
"sub_records_only": {
"message": "sub_records_only",
"url_path": "/resources/sub",
"category": "warning",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": True,
},
"records_only": {
"message": "records_only",
"url_path": "/resources",
"category": "info",
"start_datetime": datetime.utcnow() - timedelta(days=1),
"start_datetime": _now() - timedelta(days=1),
"active": True,
},
}
Expand Down Expand Up @@ -90,7 +93,7 @@ def test_update_banner(app, superuser_identity):

new_data = {
"active": True,
"start_datetime": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),
"start_datetime": _now().strftime("%Y-%m-%d %H:%M:%S"),
"message": "New banner message",
"category": "info",
}
Expand Down
5 changes: 3 additions & 2 deletions tests/test_macro.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020-2023 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# Invenio-Banners 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 views."""

from datetime import datetime
from datetime import datetime, timezone

import pytest
from flask import url_for
Expand All @@ -23,7 +24,7 @@ def _create_banner(message, category, url_path=None):
"message": message,
"category": category,
"url_path": url_path,
"start_datetime": datetime.utcnow(),
"start_datetime": datetime.now(timezone.utc).replace(tzinfo=None),
"active": True,
}
)
Expand Down