feat: Deprecate datetime.utcnow() in favor of datetime.now(timezone.utc).replace(tzinfo=None) for better timezone handling (#3408) (#3416)

This commit is contained in:
LIU HONGWEI
2024-04-12 16:22:24 +08:00
committed by GitHub
parent 4d54637921
commit c227f3d985
32 changed files with 112 additions and 112 deletions

View File

@@ -2,7 +2,7 @@ import base64
import logging
import secrets
import uuid
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from hashlib import sha256
from typing import Any, Optional
@@ -59,8 +59,8 @@ class AccountService:
available_ta.current = True
db.session.commit()
if datetime.utcnow() - account.last_active_at > timedelta(minutes=10):
account.last_active_at = datetime.utcnow()
if datetime.now(timezone.utc).replace(tzinfo=None) - account.last_active_at > timedelta(minutes=10):
account.last_active_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit()
return account
@@ -70,7 +70,7 @@ class AccountService:
def get_account_jwt_token(account):
payload = {
"user_id": account.id,
"exp": datetime.utcnow() + timedelta(days=30),
"exp": datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(days=30),
"iss": current_app.config['EDITION'],
"sub": 'Console API Passport',
}
@@ -91,7 +91,7 @@ class AccountService:
if account.status == AccountStatus.PENDING.value:
account.status = AccountStatus.ACTIVE.value
account.initialized_at = datetime.utcnow()
account.initialized_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit()
if account.password is None or not compare_password(password, account.password, account.password_salt):
@@ -163,7 +163,7 @@ class AccountService:
# If it exists, update the record
account_integrate.open_id = open_id
account_integrate.encrypted_token = "" # todo
account_integrate.updated_at = datetime.utcnow()
account_integrate.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
else:
# If it does not exist, create a new record
account_integrate = AccountIntegrate(account_id=account.id, provider=provider, open_id=open_id,
@@ -197,7 +197,7 @@ class AccountService:
@staticmethod
def update_last_login(account: Account, request) -> None:
"""Update last login time and ip"""
account.last_login_at = datetime.utcnow()
account.last_login_at = datetime.now(timezone.utc).replace(tzinfo=None)
account.last_login_ip = get_remote_ip(request)
db.session.add(account)
db.session.commit()
@@ -431,7 +431,7 @@ class RegisterService:
password=password
)
account.status = AccountStatus.ACTIVE.value if not status else status.value
account.initialized_at = datetime.utcnow()
account.initialized_at = datetime.now(timezone.utc).replace(tzinfo=None)
if open_id is not None or provider is not None:
AccountService.link_account_integrate(provider, open_id, account)

View File

@@ -415,7 +415,7 @@ class AppAnnotationService:
raise NotFound("App annotation not found")
annotation_setting.score_threshold = args['score_threshold']
annotation_setting.updated_user_id = current_user.id
annotation_setting.updated_at = datetime.datetime.utcnow()
annotation_setting.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
db.session.add(annotation_setting)
db.session.commit()

View File

@@ -1,6 +1,6 @@
import json
import logging
from datetime import datetime
from datetime import datetime, timezone
from typing import cast
import yaml
@@ -251,7 +251,7 @@ class AppService:
app.description = args.get('description', '')
app.icon = args.get('icon')
app.icon_background = args.get('icon_background')
app.updated_at = datetime.utcnow()
app.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit()
return app
@@ -264,7 +264,7 @@ class AppService:
:return: App instance
"""
app.name = name
app.updated_at = datetime.utcnow()
app.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit()
return app
@@ -279,7 +279,7 @@ class AppService:
"""
app.icon = icon
app.icon_background = icon_background
app.updated_at = datetime.utcnow()
app.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit()
return app
@@ -295,7 +295,7 @@ class AppService:
return app
app.enable_site = enable_site
app.updated_at = datetime.utcnow()
app.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit()
return app
@@ -311,7 +311,7 @@ class AppService:
return app
app.enable_api = enable_api
app.updated_at = datetime.utcnow()
app.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit()
return app

View File

@@ -415,7 +415,7 @@ class DocumentService:
# update document to be paused
document.is_paused = True
document.paused_by = current_user.id
document.paused_at = datetime.datetime.utcnow()
document.paused_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
db.session.add(document)
db.session.commit()
@@ -739,7 +739,7 @@ class DocumentService:
document.parsing_completed_at = None
document.cleaning_completed_at = None
document.splitting_completed_at = None
document.updated_at = datetime.datetime.utcnow()
document.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
document.created_from = created_from
document.doc_form = document_data['doc_form']
db.session.add(document)
@@ -1062,8 +1062,8 @@ class SegmentService:
word_count=len(content),
tokens=tokens,
status='completed',
indexing_at=datetime.datetime.utcnow(),
completed_at=datetime.datetime.utcnow(),
indexing_at=datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
completed_at=datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
created_by=current_user.id
)
if document.doc_form == 'qa_model':
@@ -1078,7 +1078,7 @@ class SegmentService:
except Exception as e:
logging.exception("create segment index failed")
segment_document.enabled = False
segment_document.disabled_at = datetime.datetime.utcnow()
segment_document.disabled_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
segment_document.status = 'error'
segment_document.error = str(e)
db.session.commit()
@@ -1128,8 +1128,8 @@ class SegmentService:
word_count=len(content),
tokens=tokens,
status='completed',
indexing_at=datetime.datetime.utcnow(),
completed_at=datetime.datetime.utcnow(),
indexing_at=datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
completed_at=datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
created_by=current_user.id
)
if document.doc_form == 'qa_model':
@@ -1147,7 +1147,7 @@ class SegmentService:
logging.exception("create segment index failed")
for segment_document in segment_data_list:
segment_document.enabled = False
segment_document.disabled_at = datetime.datetime.utcnow()
segment_document.disabled_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
segment_document.status = 'error'
segment_document.error = str(e)
db.session.commit()
@@ -1208,10 +1208,10 @@ class SegmentService:
segment.word_count = len(content)
segment.tokens = tokens
segment.status = 'completed'
segment.indexing_at = datetime.datetime.utcnow()
segment.completed_at = datetime.datetime.utcnow()
segment.indexing_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
segment.completed_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
segment.updated_by = current_user.id
segment.updated_at = datetime.datetime.utcnow()
segment.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
if document.doc_form == 'qa_model':
segment.answer = args['answer']
db.session.add(segment)
@@ -1221,7 +1221,7 @@ class SegmentService:
except Exception as e:
logging.exception("update segment index failed")
segment.enabled = False
segment.disabled_at = datetime.datetime.utcnow()
segment.disabled_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
segment.status = 'error'
segment.error = str(e)
db.session.commit()

View File

@@ -81,7 +81,7 @@ class FileService:
mime_type=file.mimetype,
created_by_role=('account' if isinstance(user, Account) else 'end_user'),
created_by=user.id,
created_at=datetime.datetime.utcnow(),
created_at=datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
used=False,
hash=hashlib.sha3_256(file_content).hexdigest()
)
@@ -111,10 +111,10 @@ class FileService:
extension='txt',
mime_type='text/plain',
created_by=current_user.id,
created_at=datetime.datetime.utcnow(),
created_at=datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None),
used=True,
used_by=current_user.id,
used_at=datetime.datetime.utcnow()
used_at=datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
)
db.session.add(upload_file)

View File

@@ -1,6 +1,6 @@
import json
import time
from datetime import datetime
from datetime import datetime, timezone
from typing import Optional
from core.app.apps.advanced_chat.app_config_manager import AdvancedChatAppConfigManager
@@ -93,7 +93,7 @@ class WorkflowService:
workflow.graph = json.dumps(graph)
workflow.features = json.dumps(features)
workflow.updated_by = account.id
workflow.updated_at = datetime.utcnow()
workflow.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
# commit db session changes
db.session.commit()
@@ -123,7 +123,7 @@ class WorkflowService:
tenant_id=app_model.tenant_id,
app_id=app_model.id,
type=draft_workflow.type,
version=str(datetime.utcnow()),
version=str(datetime.now(timezone.utc).replace(tzinfo=None)),
graph=draft_workflow.graph,
features=draft_workflow.features,
created_by=account.id
@@ -202,8 +202,8 @@ class WorkflowService:
elapsed_time=time.perf_counter() - start_at,
created_by_role=CreatedByRole.ACCOUNT.value,
created_by=account.id,
created_at=datetime.utcnow(),
finished_at=datetime.utcnow()
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
finished_at=datetime.now(timezone.utc).replace(tzinfo=None)
)
db.session.add(workflow_node_execution)
db.session.commit()
@@ -230,8 +230,8 @@ class WorkflowService:
elapsed_time=time.perf_counter() - start_at,
created_by_role=CreatedByRole.ACCOUNT.value,
created_by=account.id,
created_at=datetime.utcnow(),
finished_at=datetime.utcnow()
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
finished_at=datetime.now(timezone.utc).replace(tzinfo=None)
)
else:
# create workflow node execution
@@ -249,8 +249,8 @@ class WorkflowService:
elapsed_time=time.perf_counter() - start_at,
created_by_role=CreatedByRole.ACCOUNT.value,
created_by=account.id,
created_at=datetime.utcnow(),
finished_at=datetime.utcnow()
created_at=datetime.now(timezone.utc).replace(tzinfo=None),
finished_at=datetime.now(timezone.utc).replace(tzinfo=None)
)
db.session.add(workflow_node_execution)