Skip to content

Commit

Permalink
refactor: replace deprecated datetime.utcnow with datetime.now(timezo…
Browse files Browse the repository at this point in the history
…ne.utc)

* Updated datetime.utcnow() to datetime.now(timezone.utc) following its deprecated since version 3.12.
* See <https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow>
  • Loading branch information
Samk13 committed Oct 14, 2024
1 parent 037e2ec commit f1780d7
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
5 changes: 3 additions & 2 deletions invenio_accounts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# This file is part of Invenio.
# Copyright (C) 2015-2023 CERN.
# Copyright (C) 2024 Graz University of Technology.
# Copyright (C) 2024 KTH Royal Institute 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.

"""Command Line Interface for accounts."""

import json
from datetime import datetime
from datetime import datetime, timezone
from functools import wraps

import click
Expand Down Expand Up @@ -71,7 +72,7 @@ def users_create(email, password, active, confirm, profile):
kwargs["password"] = hash_password(kwargs["password"])
kwargs["active"] = active
if confirm:
kwargs["confirmed_at"] = datetime.utcnow()
kwargs["confirmed_at"] = datetime.now(timezone.utc)
if profile:
kwargs["user_profile"] = json.loads(profile)
_datastore.create_user(**kwargs)
Expand Down
9 changes: 5 additions & 4 deletions invenio_accounts/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2024 CERN.
# Copyright (C) 2024 KTH Royal Institute 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.

"""Session-aware datastore."""

from datetime import datetime
from datetime import datetime, timezone

from flask import current_app
from flask_security import SQLAlchemyUserDatastore, user_confirmed
Expand All @@ -25,7 +26,7 @@ class SessionAwareSQLAlchemyUserDatastore(SQLAlchemyUserDatastore):

def verify_user(self, user):
"""Verify a user."""
now = datetime.utcnow()
now = datetime.now(timezone.utc)
user.blocked_at = None
user.verified_at = now
user.active = True
Expand All @@ -35,7 +36,7 @@ def verify_user(self, user):

def block_user(self, user):
"""Verify a user."""
now = datetime.utcnow()
now = datetime.now(timezone.utc)
user.blocked_at = now
user.verified_at = None
user.active = False
Expand All @@ -47,7 +48,7 @@ def activate_user(self, user):
res = super().activate_user(user)
user.blocked_at = None
if user.confirmed_at is None:
user.confirmed_at = datetime.utcnow()
user.confirmed_at = datetime.now(timezone.utc)
user_confirmed.send(current_app._get_current_object(), user=user)
return res

Expand Down
8 changes: 4 additions & 4 deletions invenio_accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2024 CERN.
# Copyright (C) 2022 KTH Royal Institute of Technology
# Copyright (C) 2022-2024 KTH Royal Institute 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.

"""Database models for accounts."""

import uuid
from datetime import datetime
from datetime import datetime, timezone

from flask import current_app, session
from flask_babel import refresh
Expand Down Expand Up @@ -168,7 +168,7 @@ class User(db.Model, Timestamp, UserMixin):
def __init__(self, *args, **kwargs):
"""Constructor."""
self.verified_at = (
datetime.utcnow()
datetime.now(timezone.utc)
if current_app.config.get("ACCOUNTS_DEFAULT_USERS_VERIFIED")
else None
)
Expand Down Expand Up @@ -414,7 +414,7 @@ class SessionActivity(db.Model, Timestamp):
def query_by_expired(cls):
"""Query to select all expired sessions."""
lifetime = current_app.permanent_session_lifetime
expired_moment = datetime.utcnow() - lifetime
expired_moment = datetime.now(timezone.utc) - lifetime
return cls.query.filter(cls.created < expired_moment)

@classmethod
Expand Down
7 changes: 4 additions & 3 deletions invenio_accounts/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2024 KTH Royal Institute 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.

"""Background tasks for accounts."""

from datetime import datetime
from datetime import datetime, timezone

from celery import shared_task
from flask import current_app
Expand Down Expand Up @@ -61,7 +62,7 @@ def clean_session_table():
def delete_ips():
"""Automatically remove login_info.last_login_ip older than 30 days."""
expiration_date = (
datetime.utcnow() - current_app.config["ACCOUNTS_RETENTION_PERIOD"]
datetime.now(timezone.utc) - current_app.config["ACCOUNTS_RETENTION_PERIOD"]
)

LoginInformation.query.filter(
Expand Down Expand Up @@ -126,7 +127,7 @@ def update_domain_status():

# Commit batches of 500 updates
batch_size = 500
now = datetime.utcnow()
now = datetime.now(timezone.utc)

# Process updates in batches
for i in range(0, len(domain_updates), batch_size):
Expand Down
5 changes: 3 additions & 2 deletions invenio_accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2017-2024 CERN.
# Copyright (C) 2024 KTH Royal Institute 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.
Expand All @@ -11,7 +12,7 @@
import enum
import re
import uuid
from datetime import datetime
from datetime import datetime, timezone
from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit

from flask import current_app, request, session, url_for
Expand Down Expand Up @@ -69,7 +70,7 @@ def jwt_create_token(user_id=None, additional_data=None):
# Create an ID
uid = str(uuid.uuid4())
# The time in UTC now
now = datetime.utcnow()
now = datetime.now(timezone.utc)
# Build the token data
token_data = {
"exp": now + current_app.config["ACCOUNTS_JWT_EXPIRATION_DELTA"],
Expand Down
7 changes: 4 additions & 3 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2024 KTH Royal Institute 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.


"""Module tests."""

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from time import sleep

from flask import url_for
Expand Down Expand Up @@ -113,11 +114,11 @@ def test():
def test_delete_ips(task_app):
"""Test if ips are deleted after 30 days."""
last_login_at1 = (
datetime.utcnow()
datetime.now(timezone.utc)
- task_app.config["ACCOUNTS_RETENTION_PERIOD"]
- timedelta(days=1)
)
last_login_at2 = datetime.utcnow()
last_login_at2 = datetime.now(timezone.utc)

with task_app.app_context():
user1 = create_test_user(
Expand Down

0 comments on commit f1780d7

Please sign in to comment.