mirror of
https://github.com/langgenius/dify.git
synced 2026-05-05 18:08:07 +08:00
chore(api): remove backend utcnow usage (#34131)
This commit is contained in:
@ -6,6 +6,7 @@ from typing import Any
|
||||
|
||||
from graphon.nodes.human_input.entities import FormInput
|
||||
from graphon.nodes.human_input.enums import TimeoutUnit
|
||||
from libs.datetime_utils import naive_utc_now
|
||||
|
||||
|
||||
# Exceptions
|
||||
@ -49,7 +50,7 @@ class HumanInputForm:
|
||||
timeout: int
|
||||
timeout_unit: TimeoutUnit
|
||||
form_token: str | None = None
|
||||
created_at: datetime = field(default_factory=datetime.utcnow)
|
||||
created_at: datetime = field(default_factory=naive_utc_now)
|
||||
expires_at: datetime | None = None
|
||||
submitted_at: datetime | None = None
|
||||
submitted_data: dict[str, Any] | None = None
|
||||
@ -61,7 +62,7 @@ class HumanInputForm:
|
||||
|
||||
@property
|
||||
def is_expired(self) -> bool:
|
||||
return self.expires_at is not None and datetime.utcnow() > self.expires_at
|
||||
return self.expires_at is not None and naive_utc_now() > self.expires_at
|
||||
|
||||
@property
|
||||
def is_submitted(self) -> bool:
|
||||
@ -70,7 +71,7 @@ class HumanInputForm:
|
||||
def mark_submitted(self, inputs: dict[str, Any], action: str) -> None:
|
||||
self.submitted_data = inputs
|
||||
self.submitted_action = action
|
||||
self.submitted_at = datetime.utcnow()
|
||||
self.submitted_at = naive_utc_now()
|
||||
|
||||
def submit(self, inputs: dict[str, Any], action: str) -> None:
|
||||
self.mark_submitted(inputs, action)
|
||||
@ -107,7 +108,7 @@ class FormSubmissionData:
|
||||
form_id: str
|
||||
inputs: dict[str, Any]
|
||||
action: str
|
||||
submitted_at: datetime = field(default_factory=datetime.utcnow)
|
||||
submitted_at: datetime = field(default_factory=naive_utc_now)
|
||||
|
||||
@classmethod
|
||||
def from_request(cls, form_id: str, request: FormSubmissionRequest) -> FormSubmissionData: # type: ignore
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
Unit tests for FormService.
|
||||
"""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
@ -142,7 +142,7 @@ class TestFormService:
|
||||
|
||||
# Manually expire the form by modifying expiry time
|
||||
form = form_service.get_form_by_id("form-123")
|
||||
form.expires_at = datetime.utcnow() - timedelta(hours=1)
|
||||
form.expires_at = naive_utc_now() - timedelta(hours=1)
|
||||
form_service.repository.save(form)
|
||||
|
||||
# Should raise FormExpiredError
|
||||
@ -227,7 +227,7 @@ class TestFormService:
|
||||
|
||||
# Manually expire the form
|
||||
form = form_service.get_form_by_id("form-123")
|
||||
form.expires_at = datetime.utcnow() - timedelta(hours=1)
|
||||
form.expires_at = naive_utc_now() - timedelta(hours=1)
|
||||
form_service.repository.save(form)
|
||||
|
||||
# Try to submit expired form
|
||||
|
||||
@ -14,6 +14,7 @@ from graphon.nodes.human_input.enums import (
|
||||
FormInputType,
|
||||
TimeoutUnit,
|
||||
)
|
||||
from libs.datetime_utils import naive_utc_now
|
||||
|
||||
from .support import FormSubmissionData, FormSubmissionRequest, HumanInputForm
|
||||
|
||||
@ -83,7 +84,7 @@ class TestHumanInputForm:
|
||||
def test_form_expiry_property_expired(self, sample_form_data):
|
||||
"""Test is_expired property for expired form."""
|
||||
# Create form with past expiry
|
||||
past_time = datetime.utcnow() - timedelta(hours=1)
|
||||
past_time = naive_utc_now() - timedelta(hours=1)
|
||||
sample_form_data["created_at"] = past_time
|
||||
|
||||
form = HumanInputForm(**sample_form_data)
|
||||
@ -111,9 +112,9 @@ class TestHumanInputForm:
|
||||
"""Test form submit method."""
|
||||
form = HumanInputForm(**sample_form_data)
|
||||
|
||||
submission_time_before = datetime.utcnow()
|
||||
submission_time_before = naive_utc_now()
|
||||
form.submit({"input": "test value"}, "submit")
|
||||
submission_time_after = datetime.utcnow()
|
||||
submission_time_after = naive_utc_now()
|
||||
|
||||
assert form.is_submitted
|
||||
assert form.submitted_data == {"input": "test value"}
|
||||
@ -213,11 +214,11 @@ class TestFormSubmissionData:
|
||||
|
||||
def test_submission_data_timestamps(self):
|
||||
"""Test submission data timestamp handling."""
|
||||
before_time = datetime.utcnow()
|
||||
before_time = naive_utc_now()
|
||||
|
||||
submission_data = FormSubmissionData(form_id="form-123", inputs={"test": "value"}, action="submit")
|
||||
|
||||
after_time = datetime.utcnow()
|
||||
after_time = naive_utc_now()
|
||||
|
||||
assert before_time <= submission_data.submitted_at <= after_time
|
||||
|
||||
|
||||
Reference in New Issue
Block a user