From fa318882a52bb8529a9ca1812f106f4c2486191a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 31 May 2024 02:30:48 +0200 Subject: [PATCH] Replaced deprecated usages of `datetime.utcnow()` with `datetime.now(UTC)`. --- platypush/backend/http/app/routes/login.py | 14 +++++++----- platypush/backend/http/app/routes/register.py | 22 ++++++++++++++----- platypush/entities/__init__.py | 6 ++--- platypush/entities/_base.py | 8 +++---- platypush/entities/managers/__init__.py | 4 ++-- .../plugins/bluetooth/_ble/_event_handler.py | 12 +++++----- platypush/plugins/google/calendar/__init__.py | 2 +- platypush/plugins/rss/__init__.py | 2 +- platypush/user/__init__.py | 6 ++--- 9 files changed, 46 insertions(+), 30 deletions(-) diff --git a/platypush/backend/http/app/routes/login.py b/platypush/backend/http/app/routes/login.py index ab93e0e214..a375aa645b 100644 --- a/platypush/backend/http/app/routes/login.py +++ b/platypush/backend/http/app/routes/login.py @@ -17,7 +17,7 @@ __routes__ = [ @login.route('/login', methods=['GET', 'POST']) def login(): - """ Login page """ + """Login page""" user_manager = UserManager() session_token = request.cookies.get('session_token') @@ -37,11 +37,15 @@ def login(): username = request.form.get('username') password = request.form.get('password') remember = request.form.get('remember') - expires = datetime.datetime.utcnow() + datetime.timedelta(days=365) \ - if remember else None + expires = ( + datetime.datetime.now(datetime.UTC) + datetime.timedelta(days=365) + if remember + else None + ) - session = user_manager.create_user_session(username=username, password=password, - expires_at=expires) + session = user_manager.create_user_session( + username=username, password=password, expires_at=expires + ) if session: redirect_target = redirect(redirect_page, 302) # lgtm [py/url-redirection] diff --git a/platypush/backend/http/app/routes/register.py b/platypush/backend/http/app/routes/register.py index 2404925675..8590c2fcaf 100644 --- a/platypush/backend/http/app/routes/register.py +++ b/platypush/backend/http/app/routes/register.py @@ -17,7 +17,7 @@ __routes__ = [ @register.route('/register', methods=['GET', 'POST']) def register(): - """ Registration page """ + """Registration page""" user_manager = UserManager() redirect_page = request.args.get('redirect') if not redirect_page: @@ -34,7 +34,9 @@ def register(): return redirect(redirect_page, 302) # lgtm [py/url-redirection] if user_manager.get_user_count() > 0: - return redirect('/login?redirect=' + redirect_page, 302) # lgtm [py/url-redirection] + return redirect( + '/login?redirect=' + redirect_page, 302 + ) # lgtm [py/url-redirection] if request.form: username = request.form.get('username') @@ -44,12 +46,20 @@ def register(): if password == confirm_password: user_manager.create_user(username=username, password=password) - session = user_manager.create_user_session(username=username, password=password, - expires_at=datetime.datetime.utcnow() + datetime.timedelta(days=1) - if not remember else None) + session = user_manager.create_user_session( + username=username, + password=password, + expires_at=( + datetime.datetime.now(datetime.UTC) + datetime.timedelta(days=1) + if not remember + else None + ), + ) if session: - redirect_target = redirect(redirect_page, 302) # lgtm [py/url-redirection] + redirect_target = redirect( + redirect_page, 302 + ) # lgtm [py/url-redirection] response = make_response(redirect_target) response.set_cookie('session_token', session.session_token) return response diff --git a/platypush/entities/__init__.py b/platypush/entities/__init__.py index 5548987486..35bae65152 100644 --- a/platypush/entities/__init__.py +++ b/platypush/entities/__init__.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, UTC import logging from threading import Event from typing import Collection, Optional @@ -45,8 +45,8 @@ def get_entities_engine(timeout: Optional[float] = None) -> EntitiesEngine: :param timeout: Timeout in seconds (default: None). """ - time_start = datetime.utcnow() - while not timeout or (datetime.utcnow() - time_start < timedelta(seconds=timeout)): + time_start = datetime.now(UTC) + while not timeout or (datetime.now(UTC) - time_start < timedelta(seconds=timeout)): if _engine: break diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index 661fa636da..9238c7b572 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -6,7 +6,7 @@ import pathlib import subprocess import sys import types -from datetime import datetime +from datetime import datetime, UTC from typing import Callable, Dict, List, Optional, Set, Type, Tuple, Any import pkgutil @@ -83,12 +83,12 @@ if not is_defined('entity'): image_url = Column(String) created_at = Column( - DateTime(timezone=False), default=datetime.utcnow(), nullable=False + DateTime(timezone=False), default=datetime.now(UTC), nullable=False ) updated_at = Column( DateTime(timezone=False), - default=datetime.utcnow(), - onupdate=datetime.utcnow(), + default=datetime.now(UTC), + onupdate=datetime.now(UTC), ) parent = relationship( diff --git a/platypush/entities/managers/__init__.py b/platypush/entities/managers/__init__.py index 243c3d98f3..030bd94644 100644 --- a/platypush/entities/managers/__init__.py +++ b/platypush/entities/managers/__init__.py @@ -1,7 +1,7 @@ import inspect import json from abc import ABC, abstractmethod -from datetime import datetime +from datetime import datetime, UTC from typing import Any, Optional, Dict, Collection, Type from platypush.config import Config @@ -77,7 +77,7 @@ class EntityManager(ABC): entity.id = None # type: ignore entity.plugin = get_plugin_name_by_class(self.__class__) # type: ignore - entity.updated_at = datetime.utcnow() # type: ignore + entity.updated_at = datetime.now(UTC) # type: ignore entity.children = self._normalize_entities(entity.children) return entities diff --git a/platypush/plugins/bluetooth/_ble/_event_handler.py b/platypush/plugins/bluetooth/_ble/_event_handler.py index c8ca08df1a..3397462d3c 100644 --- a/platypush/plugins/bluetooth/_ble/_event_handler.py +++ b/platypush/plugins/bluetooth/_ble/_event_handler.py @@ -1,4 +1,4 @@ -from datetime import datetime, timedelta +from datetime import datetime, timedelta, UTC from logging import getLogger from queue import Queue from typing import Callable, Collection, Dict, Final, List, Optional, Type @@ -98,7 +98,7 @@ event_matchers: Dict[ ) and ( not (old and old.updated_at) - or datetime.utcnow() - old.updated_at + or datetime.now(UTC) - old.updated_at >= timedelta(seconds=_rssi_update_interval) ) ), @@ -227,9 +227,11 @@ class EventHandler: return True mapped_uuids = [ - int(str(srv.uuid).split('-', maxsplit=1)[0], 16) & 0xFFFF - if isinstance(srv.uuid, UUID) - else srv.uuid + ( + int(str(srv.uuid).split('-', maxsplit=1)[0], 16) & 0xFFFF + if isinstance(srv.uuid, UUID) + else srv.uuid + ) for srv in device.services ] diff --git a/platypush/plugins/google/calendar/__init__.py b/platypush/plugins/google/calendar/__init__.py index 4f06e15389..2dcf9693c5 100644 --- a/platypush/plugins/google/calendar/__init__.py +++ b/platypush/plugins/google/calendar/__init__.py @@ -72,7 +72,7 @@ class GoogleCalendarPlugin(GooglePlugin, CalendarInterface): :meth:`platypush.plugins.calendar.CalendarPlugin.get_upcoming_events`. """ - now = datetime.datetime.utcnow().isoformat() + 'Z' + now = datetime.datetime.now(datetime.UTC).isoformat() + 'Z' service = self.get_service('calendar', 'v3') result = ( service.events() diff --git a/platypush/plugins/rss/__init__.py b/platypush/plugins/rss/__init__.py index 8f5d697f38..b03b43b374 100644 --- a/platypush/plugins/rss/__init__.py +++ b/platypush/plugins/rss/__init__.py @@ -289,7 +289,7 @@ class RssPlugin(RunnablePlugin): title = ElementTree.Element('title') title.text = 'Platypush feed subscriptions' created = ElementTree.Element('dateCreated') - created.text = self._datetime_to_string(datetime.datetime.utcnow()) + created.text = self._datetime_to_string(datetime.datetime.now(datetime.UTC)) head.append(title) head.append(created) diff --git a/platypush/user/__init__.py b/platypush/user/__init__.py index 5691fa1c1d..c9bbac3a83 100644 --- a/platypush/user/__init__.py +++ b/platypush/user/__init__.py @@ -78,7 +78,7 @@ class UserManager: ), password_salt=password_salt.hex(), hmac_iterations=hmac_iterations, - created_at=datetime.datetime.utcnow(), + created_at=datetime.datetime.now(datetime.UTC), **kwargs, ) @@ -117,7 +117,7 @@ class UserManager: if not user_session or ( user_session.expires_at - and user_session.expires_at < datetime.datetime.utcnow() + and user_session.expires_at < datetime.datetime.now(datetime.UTC) ): return None, None @@ -171,7 +171,7 @@ class UserManager: user_id=user.user_id, session_token=self.generate_session_token(), csrf_token=self.generate_session_token(), - created_at=datetime.datetime.utcnow(), + created_at=datetime.datetime.now(datetime.UTC), expires_at=expires_at, )