76 lines
2.8 KiB
Python
76 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
測試通知發送功能
|
|
"""
|
|
|
|
from app import create_app, db
|
|
from app.models import TranslationJob, User
|
|
from app.services.notification_service import NotificationService
|
|
from app.models import NotificationType
|
|
|
|
def test_notification_sending():
|
|
"""測試通知發送功能"""
|
|
try:
|
|
app = create_app()
|
|
with app.app_context():
|
|
print("Testing notification sending...")
|
|
|
|
# 查找一個用戶
|
|
user = User.query.first()
|
|
if not user:
|
|
print("No users found, cannot test notification")
|
|
return
|
|
|
|
print(f"Found user: {user.username} (ID: {user.id})")
|
|
|
|
# 創建一個模擬的翻譯任務
|
|
from datetime import datetime
|
|
from uuid import uuid4
|
|
|
|
mock_job = TranslationJob(
|
|
job_uuid=str(uuid4()),
|
|
user_id=user.id,
|
|
original_filename="test_document.docx",
|
|
status="COMPLETED",
|
|
target_languages=["zh-TW", "en"],
|
|
total_cost=0.05,
|
|
completed_at=datetime.now()
|
|
)
|
|
|
|
# 不保存到資料庫,只用於測試通知
|
|
print(f"Created mock job: {mock_job.job_uuid}")
|
|
|
|
# 測試通知服務
|
|
notification_service = NotificationService()
|
|
|
|
# 測試直接發送通知(不檢查狀態)
|
|
print("Testing direct notification sending...")
|
|
notification = notification_service.send_job_completion_db_notification_direct(mock_job)
|
|
|
|
if notification:
|
|
print(f"✅ Notification created successfully!")
|
|
print(f" - ID: {notification.notification_uuid}")
|
|
print(f" - Title: {notification.title}")
|
|
print(f" - Message: {notification.message}")
|
|
print(f" - Type: {notification.type}")
|
|
print(f" - User ID: {notification.user_id}")
|
|
else:
|
|
print("❌ Failed to create notification")
|
|
|
|
# 檢查資料庫中的通知數量
|
|
from app.models import Notification
|
|
total_notifications = Notification.query.count()
|
|
user_notifications = Notification.query.filter_by(user_id=user.id).count()
|
|
|
|
print(f"\nDatabase status:")
|
|
print(f" - Total notifications: {total_notifications}")
|
|
print(f" - Notifications for user {user.username}: {user_notifications}")
|
|
|
|
except Exception as e:
|
|
print(f"Error testing notification sending: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
if __name__ == '__main__':
|
|
test_notification_sending() |