Files
hsa/Html/apps/views/vscode.py
2025-09-03 17:17:14 +08:00

68 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# myapp/views/vscode.py
import os, uuid, json
from flask import Blueprint, current_app, session, redirect, url_for, render_template, request, jsonify
from ..services.backboard_service import realtime_response
bp = Blueprint("vscode", __name__)
@bp.route("/desktop/<user_uuid>/<course_id>/<chapter_id>")
def desktop(user_uuid, course_id, chapter_id):
username2uuid = current_app.extensions["username2uuid"]
uuid2username = current_app.extensions["uuid2username"]
userid_recorder = current_app.extensions["userid_recorder"]
# session 中放 uuid访客也能进则给一个
if "user_uuid" not in session:
user_uuid = "user_" + str(uuid.uuid4())
session["user_uuid"] = user_uuid
uuid2username[user_uuid] = "guest"+str(uuid.uuid4())
username2uuid[uuid2username[user_uuid]] = user_uuid
# 全局映射
user_id = uuid2username[user_uuid]
userid_recorder[f"{user_id}&{course_id}"] = session["user_uuid"]
# 按课程/章节创建工作目录
base_root = current_app.config["STUDENT_WORKSPACE_ROOT"]
path_dir = os.path.join(base_root, user_id, course_id, chapter_id)
os.makedirs(path_dir, exist_ok=True)
# 写 .config并按需要转换为 WSL 路径)
cfg = current_app.config["VSCODE_WEB_PATH"]
path_for_vscode = path_dir
if cfg.get("is_wsl"):
path_for_vscode = path_for_vscode.replace("\\", "/") \
.replace(cfg.get("windows_path", ""), cfg.get("wsl_path", ""))
config_path = os.path.join(path_dir, ".config")
tmpd = {
"user_uuid": session["user_uuid"],
"course_id": course_id,
"chapter_id": chapter_id,
"path": path_for_vscode
}
with open(config_path, "w", encoding="utf-8") as f:
json.dump(tmpd, f, ensure_ascii=False)
current_app.logger.debug("config file path: %s", config_path)
return render_template(
"desktop.html",
vscode_web_url=current_app.config["VSCODE_WEB_URL"],
user_uuid=session["user_uuid"], course_id=course_id, chapter_id=chapter_id,
workspace_path=path_for_vscode
)
@bp.route("/desktop_nouser/<course_id>/<chapter_id>")
def desktop_nouser(course_id, chapter_id):
if "user_uuid" not in session:
return redirect(url_for("auth.login")) # 如果你有 auth 蓝图
user_uuid = session["user_uuid"]
return redirect(url_for("vscode.desktop", user_uuid=user_uuid, course_id=course_id, chapter_id=chapter_id))
@bp.route("/vscode_data", methods=["POST"])
def vscode_data():
data = request.get_json(force=True)
config = data.get("config") or {}
realtime_response(config, data) # 调用服务层逻辑
return jsonify({"status": "success", "received": data})