Initial commit: Daily News App
企業內部新聞彙整與分析系統 - 自動新聞抓取 (Digitimes, 經濟日報, 工商時報) - AI 智慧摘要 (OpenAI/Claude/Ollama) - 群組管理與訂閱通知 - 已清理 Python 快取檔案 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
130
scripts/init_db_sqlite.py
Normal file
130
scripts/init_db_sqlite.py
Normal file
@@ -0,0 +1,130 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
資料庫初始化腳本
|
||||
建立所有表格並插入預設資料
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add project root to python path
|
||||
sys.path.append(os.getcwd())
|
||||
|
||||
from app.db.session import init_db, SessionLocal
|
||||
from app.models import Role, User, NewsSource, SourceType
|
||||
from app.core.security import get_password_hash
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
def seed_default_data():
|
||||
"""插入預設資料"""
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
# 1. 建立角色
|
||||
roles_data = [
|
||||
{"code": "admin", "name": "系統管理員", "description": "LLM 設定、AD 整合、群組管理、用戶管理、系統設定"},
|
||||
{"code": "editor", "name": "市場分析專員", "description": "新聞抓取管理、篩選編輯、報告發布、群組內容設定"},
|
||||
{"code": "reader", "name": "讀者", "description": "訂閱群組、閱讀報告、留言討論、個人收藏、匯出 PDF"},
|
||||
]
|
||||
|
||||
for role_data in roles_data:
|
||||
existing = db.query(Role).filter(Role.code == role_data["code"]).first()
|
||||
if not existing:
|
||||
role = Role(**role_data)
|
||||
db.add(role)
|
||||
print(f" 建立角色: {role_data['name']}")
|
||||
|
||||
db.commit()
|
||||
|
||||
# 2. 建立管理員帳號
|
||||
admin_role = db.query(Role).filter(Role.code == "admin").first()
|
||||
existing_admin = db.query(User).filter(User.username == "admin").first()
|
||||
|
||||
if not existing_admin and admin_role:
|
||||
admin_user = User(
|
||||
username="admin",
|
||||
password_hash=get_password_hash(settings.admin_password),
|
||||
display_name="系統管理員",
|
||||
email="admin@example.com",
|
||||
auth_type="local",
|
||||
role_id=admin_role.id,
|
||||
is_active=True
|
||||
)
|
||||
db.add(admin_user)
|
||||
print(f" 建立管理員帳號: admin (密碼: {settings.admin_password})")
|
||||
|
||||
db.commit()
|
||||
|
||||
# 3. 建立新聞來源
|
||||
sources_data = [
|
||||
{
|
||||
"code": "digitimes",
|
||||
"name": "Digitimes",
|
||||
"base_url": "https://www.digitimes.com.tw",
|
||||
"source_type": SourceType.SUBSCRIPTION,
|
||||
"is_active": True,
|
||||
},
|
||||
{
|
||||
"code": "udn",
|
||||
"name": "經濟日報",
|
||||
"base_url": "https://money.udn.com",
|
||||
"source_type": SourceType.PUBLIC,
|
||||
"is_active": True,
|
||||
},
|
||||
{
|
||||
"code": "ctee",
|
||||
"name": "工商時報",
|
||||
"base_url": "https://ctee.com.tw",
|
||||
"source_type": SourceType.PUBLIC,
|
||||
"is_active": True,
|
||||
},
|
||||
]
|
||||
|
||||
for source_data in sources_data:
|
||||
existing = db.query(NewsSource).filter(NewsSource.code == source_data["code"]).first()
|
||||
if not existing:
|
||||
source = NewsSource(**source_data)
|
||||
db.add(source)
|
||||
print(f" 建立新聞來源: {source_data['name']}")
|
||||
|
||||
db.commit()
|
||||
print("預設資料插入完成!")
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(f"插入預設資料失敗: {e}")
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("=" * 50)
|
||||
print("每日報導 APP - 資料庫初始化")
|
||||
print("=" * 50)
|
||||
|
||||
print("\n1. 建立資料庫表格...")
|
||||
try:
|
||||
init_db()
|
||||
print("資料庫表格建立成功!")
|
||||
except Exception as e:
|
||||
print(f"建立表格失敗: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
print("\n2. 插入預設資料...")
|
||||
try:
|
||||
seed_default_data()
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("初始化完成!")
|
||||
print("=" * 50)
|
||||
print("\n登入資訊:")
|
||||
print(f" 帳號: admin")
|
||||
print(f" 密碼: {settings.admin_password}")
|
||||
print("\n啟動應用程式:")
|
||||
print(" python run.py")
|
||||
print("=" * 50)
|
||||
Reference in New Issue
Block a user