补充遗漏翻译,新增启动脚本,整理import

This commit is contained in:
2026-05-09 16:42:17 +08:00
parent e542c482d7
commit 75ceec0798
16 changed files with 152 additions and 103 deletions

View File

@@ -1,14 +1,18 @@
from flask import Blueprint, render_template, request, jsonify, flash, redirect, url_for, current_app, Response
from flask_login import login_required, current_user
from websockets.sync.client import connect as ws_connect
import os
import websockets.exceptions
import threading
import requests
from urllib.parse import urlparse
from app.models import SystemConfig, db
import uuid
import traceback
from werkzeug.utils import secure_filename
from flask import Blueprint, render_template, request, jsonify, flash, redirect, url_for, current_app, Response
from flask_login import login_required, current_user
from flask_sock import Server, ConnectionClosed
from websockets.sync.client import connect as ws_connect
from urllib.parse import urlparse, urlencode
from app.models import SystemConfig, db, PrintFile
from app.utils.octoprint_client import OctoPrintClient
from app.models import PrintFile
import os
from app.utils.gcode_parser import get_gcode_metadata
printer_bp = Blueprint('printer', __name__, url_prefix='/printer')
@@ -21,17 +25,17 @@ def get_octo_client():
def _enrich_job_data(job_data):
if job_data and job_data.get('job', {}).get('file', {}).get('name'):
from app.models import PrintFile
internal_name = job_data['job']['file']['name']
internal_stl_name = str(internal_name)[:-5]+"stl"
user_files = {}
print_file = None
for f in PrintFile.query.filter_by(user_id=current_user.id, status='sliced').all():
user_files[f.filename] = f.original_filename
if internal_stl_name in user_files.keys():
print_file = user_files[str(internal_name)[:-5]+"stl"]
if print_file:
job_data['job']['file']['display_name'] = print_file
if current_user.is_authenticated and current_user.is_admin:
pf = PrintFile.query.filter_by(filename=internal_stl_name).first()
elif current_user.is_authenticated:
pf = PrintFile.query.filter_by(filename=internal_stl_name, user_id=current_user.id).first()
else:
pf = None
if pf:
job_data['job']['file']['display_name'] = pf.original_filename
else:
job_data['job']['file']['display_name'] = internal_name
return job_data
@@ -81,9 +85,6 @@ def get_gcode_dir():
@printer_bp.route('/prepare')
@login_required
def prepare():
from app.models import PrintFile
import os
from app.utils.gcode_parser import get_gcode_metadata
# Query only the sliced GCode files belonging to the current user
user_files = PrintFile.query.filter_by(user_id=current_user.id, status='sliced').order_by(PrintFile.created_at.desc()).all()
@@ -151,7 +152,6 @@ def prepare():
def check_printer_control_permission(client):
from flask_login import current_user
if current_user.is_admin:
return True, None
@@ -167,7 +167,6 @@ def check_printer_control_permission(client):
if not internal_name:
return False, "现在有任务正在运行,非管理员无法进行控制。"
from app.models import PrintFile
pf = PrintFile.query.filter_by(filename=internal_name).first()
if pf and pf.user_id == current_user.id:
return True, None
@@ -204,7 +203,6 @@ def control():
try:
raw_url = client.get_webcam_stream_url()
# If it's an absolute url pointing to the base url, strip it or proxy it via octo_proxy
from urllib.parse import urlparse, urlencode
parsed_raw = urlparse(raw_url)
base_config = SystemConfig.query.filter_by(key='octoprint_url').first()
if base_config and base_config.value:
@@ -256,11 +254,6 @@ def api_command():
@printer_bp.route('/api/upload_gcode', methods=['POST'])
@login_required
def upload_gcode():
from app.models import PrintFile
import os
import uuid
from werkzeug.utils import secure_filename
if 'file' not in request.files:
return jsonify({"success": False, "error": "No file part"}), 400
@@ -366,8 +359,6 @@ def octo_proxy(path):
# --- WebSocket Proxy Logic ---
if request.headers.get('Upgrade', '').lower() == 'websocket':
from flask_sock import Server, ConnectionClosed
# Check if environment supports WebSockets
try:
ws = Server(request.environ)
@@ -413,7 +404,6 @@ def octo_proxy(path):
remote_ws = ws_connect(target_url, additional_headers=ws_headers)
print("WS Proxy connected to remote.")
except Exception as e:
import traceback
traceback.print_exc()
print(f"Remote WS Connection Error: {e}")
ws.close(1011, str(e))
@@ -473,7 +463,6 @@ def octo_proxy(path):
return WebSocketResponse()
# --- Standard HTTP Proxy Logic ---
# from urllib.parse import urlparse
target_url = f"{base_url}/{path}"
if request.query_string: