Commit 7d00ec63 authored by uuo00_n's avatar uuo00_n

fix(dashboard): 修复考勤统计中缺少课程ID的问题

在homeroom_current_summary函数中添加current_lesson_id,确保考勤查询包含课程ID条件
```

```msg
refactor(init_db): 将硬编码密码改为环境变量配置

添加密码相关的环境变量配置,并从环境变量中读取密码值,提高安全性
parent 05ab73d7
...@@ -8,4 +8,4 @@ api_router.include_router(auth.router, prefix="/auth", tags=["认证"]) ...@@ -8,4 +8,4 @@ api_router.include_router(auth.router, prefix="/auth", tags=["认证"])
api_router.include_router(conversation.router, prefix="/conversations", tags=["对话"]) api_router.include_router(conversation.router, prefix="/conversations", tags=["对话"])
api_router.include_router(admin.router, prefix="/admin", tags=["管理员"]) api_router.include_router(admin.router, prefix="/admin", tags=["管理员"])
api_router.include_router(dashboard.router, prefix="/dashboard", tags=["仪表盘"]) api_router.include_router(dashboard.router, prefix="/dashboard", tags=["仪表盘"])
api_router.include_router(students.router, prefix="/students", tags=["学生"]) api_router.include_router(students.router, prefix="/students", tags=["学生"])
\ No newline at end of file
...@@ -83,6 +83,7 @@ async def homeroom_current_summary(current_user: Dict[str, Any]) -> Dict[str, An ...@@ -83,6 +83,7 @@ async def homeroom_current_summary(current_user: Dict[str, Any]) -> Dict[str, An
today = await _today_iso() today = await _today_iso()
weekday = await _weekday() weekday = await _weekday()
period = await _current_period() period = await _current_period()
current_lesson_id = f"W{weekday}-P{period}"
uid = ObjectId(current_user["_id"]) uid = ObjectId(current_user["_id"])
classes = [] classes = []
cursor = db.db.classes.find({"head_teacher_id": uid}) cursor = db.db.classes.find({"head_teacher_id": uid})
...@@ -108,7 +109,12 @@ async def homeroom_current_summary(current_user: Dict[str, Any]) -> Dict[str, An ...@@ -108,7 +109,12 @@ async def homeroom_current_summary(current_user: Dict[str, Any]) -> Dict[str, An
rates: List[Dict[str, Any]] = [] rates: List[Dict[str, Any]] = []
for c in classes: for c in classes:
total = c.get("students_count", 0) total = c.get("students_count", 0)
present = await db.db.attendance.count_documents({"class_id": c.get("class_id"), "date": today, "status": "出勤"}) present = await db.db.attendance.count_documents({
"class_id": c.get("class_id"),
"date": today,
"lesson_id": current_lesson_id,
"status": "出勤",
})
rate = (present / total) if total else 0 rate = (present / total) if total else 0
rates.append({"class_id": c.get("class_id"), "present": present, "total": total, "rate": rate}) rates.append({"class_id": c.get("class_id"), "present": present, "total": total, "rate": rate})
......
...@@ -17,6 +17,16 @@ MONGODB_URL = os.getenv("MONGODB_URL", "mongodb://localhost:27017") ...@@ -17,6 +17,16 @@ MONGODB_URL = os.getenv("MONGODB_URL", "mongodb://localhost:27017")
DB_NAME = os.getenv("DB_NAME", "llm_filter_db") DB_NAME = os.getenv("DB_NAME", "llm_filter_db")
# 运行模式:仅运行教育版或企业版之一(不混合) # 运行模式:仅运行教育版或企业版之一(不混合)
APP_MODE = (os.getenv("APP_MODE", "edu") or "edu").lower() APP_MODE = (os.getenv("APP_MODE", "edu") or "edu").lower()
ADMIN_EDU_PASSWORD = os.getenv("ADMIN_EDU_PASSWORD", "admin123")
USER_EDU_PASSWORD = os.getenv("USER_EDU_PASSWORD", "user123")
ADMIN_BIZ_PASSWORD = os.getenv("ADMIN_BIZ_PASSWORD", "adminbiz123")
USER_BIZ_PASSWORD = os.getenv("USER_BIZ_PASSWORD", "userbiz123")
MANAGER_EDU_PASSWORD = os.getenv("MANAGER_EDU_PASSWORD", "manager123")
LEADER_EDU_PASSWORD = os.getenv("LEADER_EDU_PASSWORD", "leader123")
MASTER_EDU_PASSWORD = os.getenv("MASTER_EDU_PASSWORD", "master123")
MANAGER_BIZ_PASSWORD = os.getenv("MANAGER_BIZ_PASSWORD", "managerbiz123")
LEADER_BIZ_PASSWORD = os.getenv("LEADER_BIZ_PASSWORD", "leaderbiz123")
MASTER_BIZ_PASSWORD = os.getenv("MASTER_BIZ_PASSWORD", "masterbiz123")
async def init_db(): async def init_db():
# 连接到MongoDB # 连接到MongoDB
...@@ -41,7 +51,7 @@ async def init_db(): ...@@ -41,7 +51,7 @@ async def init_db():
"_id": admin_id, "_id": admin_id,
"username": "admin", "username": "admin",
"email": "admin@example.com", "email": "admin@example.com",
"hashed_password": pwd_context.hash("admin123"), "hashed_password": pwd_context.hash(ADMIN_EDU_PASSWORD),
"role": "administrator", # 统一使用标准角色名,兼容旧数据中的 "admin" "role": "administrator", # 统一使用标准角色名,兼容旧数据中的 "admin"
"role_level": 5, # 映射到最高等级 "role_level": 5, # 映射到最高等级
"edition": "edu", # 默认教育版 "edition": "edu", # 默认教育版
...@@ -53,7 +63,7 @@ async def init_db(): ...@@ -53,7 +63,7 @@ async def init_db():
"_id": user_id, "_id": user_id,
"username": "user", "username": "user",
"email": "user@example.com", "email": "user@example.com",
"hashed_password": pwd_context.hash("user123"), "hashed_password": pwd_context.hash(USER_EDU_PASSWORD),
"role": "user", "role": "user",
"role_level": 1, "role_level": 1,
"edition": "edu", "edition": "edu",
...@@ -65,7 +75,7 @@ async def init_db(): ...@@ -65,7 +75,7 @@ async def init_db():
"_id": ObjectId(), "_id": ObjectId(),
"username": "manager_edu", "username": "manager_edu",
"email": "manager_edu@example.com", "email": "manager_edu@example.com",
"hashed_password": pwd_context.hash("manager123"), "hashed_password": pwd_context.hash(MANAGER_EDU_PASSWORD),
"role": "manager", "role": "manager",
"role_level": 2, "role_level": 2,
"edition": "edu", "edition": "edu",
...@@ -76,7 +86,7 @@ async def init_db(): ...@@ -76,7 +86,7 @@ async def init_db():
"_id": ObjectId(), "_id": ObjectId(),
"username": "leader_edu", "username": "leader_edu",
"email": "leader_edu@example.com", "email": "leader_edu@example.com",
"hashed_password": pwd_context.hash("leader123"), "hashed_password": pwd_context.hash(LEADER_EDU_PASSWORD),
"role": "leader", "role": "leader",
"role_level": 3, "role_level": 3,
"edition": "edu", "edition": "edu",
...@@ -87,7 +97,7 @@ async def init_db(): ...@@ -87,7 +97,7 @@ async def init_db():
"_id": ObjectId(), "_id": ObjectId(),
"username": "master_edu", "username": "master_edu",
"email": "master_edu@example.com", "email": "master_edu@example.com",
"hashed_password": pwd_context.hash("master123"), "hashed_password": pwd_context.hash(MASTER_EDU_PASSWORD),
"role": "master", "role": "master",
"role_level": 4, "role_level": 4,
"edition": "edu", "edition": "edu",
...@@ -99,7 +109,7 @@ async def init_db(): ...@@ -99,7 +109,7 @@ async def init_db():
"_id": user_biz_id, "_id": user_biz_id,
"username": "user_biz", "username": "user_biz",
"email": "user_biz@example.com", "email": "user_biz@example.com",
"hashed_password": pwd_context.hash("userbiz123"), "hashed_password": pwd_context.hash(USER_BIZ_PASSWORD),
"role": "user", "role": "user",
"role_level": 1, "role_level": 1,
"edition": "biz", "edition": "biz",
...@@ -110,7 +120,7 @@ async def init_db(): ...@@ -110,7 +120,7 @@ async def init_db():
"_id": ObjectId(), "_id": ObjectId(),
"username": "manager_biz", "username": "manager_biz",
"email": "manager_biz@example.com", "email": "manager_biz@example.com",
"hashed_password": pwd_context.hash("managerbiz123"), "hashed_password": pwd_context.hash(MANAGER_BIZ_PASSWORD),
"role": "manager", "role": "manager",
"role_level": 2, "role_level": 2,
"edition": "biz", "edition": "biz",
...@@ -121,7 +131,7 @@ async def init_db(): ...@@ -121,7 +131,7 @@ async def init_db():
"_id": ObjectId(), "_id": ObjectId(),
"username": "leader_biz", "username": "leader_biz",
"email": "leader_biz@example.com", "email": "leader_biz@example.com",
"hashed_password": pwd_context.hash("leaderbiz123"), "hashed_password": pwd_context.hash(LEADER_BIZ_PASSWORD),
"role": "leader", "role": "leader",
"role_level": 3, "role_level": 3,
"edition": "biz", "edition": "biz",
...@@ -132,7 +142,7 @@ async def init_db(): ...@@ -132,7 +142,7 @@ async def init_db():
"_id": ObjectId(), "_id": ObjectId(),
"username": "master_biz", "username": "master_biz",
"email": "master_biz@example.com", "email": "master_biz@example.com",
"hashed_password": pwd_context.hash("masterbiz123"), "hashed_password": pwd_context.hash(MASTER_BIZ_PASSWORD),
"role": "master", "role": "master",
"role_level": 4, "role_level": 4,
"edition": "biz", "edition": "biz",
...@@ -143,7 +153,7 @@ async def init_db(): ...@@ -143,7 +153,7 @@ async def init_db():
"_id": ObjectId(), "_id": ObjectId(),
"username": "administrator_biz", "username": "administrator_biz",
"email": "administrator_biz@example.com", "email": "administrator_biz@example.com",
"hashed_password": pwd_context.hash("adminbiz123"), "hashed_password": pwd_context.hash(ADMIN_BIZ_PASSWORD),
"role": "administrator", "role": "administrator",
"role_level": 5, "role_level": 5,
"edition": "biz", "edition": "biz",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment