Replaced deprecated usages of datetime.utcnow() with datetime.now(UTC).

This commit is contained in:
Fabio Manganiello 2024-05-31 02:30:48 +02:00
parent d6185ddb1e
commit fa318882a5
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
9 changed files with 46 additions and 30 deletions

View file

@ -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]

View file

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

View file

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

View file

@ -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(

View file

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

View file

@ -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
)
for srv in device.services
]

View file

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

View file

@ -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)

View file

@ -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,
)