dm/python/app.py

389 lines
13 KiB
Python
Raw Normal View History

2024-07-01 10:43:22 +08:00
import subprocess
import threading
import time
import paramiko
2024-06-19 13:13:34 +08:00
from teacher_func import *
from student_func import *
2024-07-01 10:43:22 +08:00
from k8s_func import *
2024-06-19 19:23:01 +08:00
from flask import Flask, render_template, request, jsonify, send_from_directory, session
2024-06-19 13:13:34 +08:00
from flask_cors import CORS
2024-07-01 10:43:22 +08:00
from flask_socketio import SocketIO, emit, disconnect
import logging
2024-07-03 14:08:43 +08:00
import os
print(os.getcwd())
2024-07-01 10:43:22 +08:00
logging.basicConfig(level=logging.DEBUG)
2024-06-19 19:23:01 +08:00
app = Flask(__name__, static_folder="public")
2024-07-01 10:43:22 +08:00
CORS(app)
cors = CORS(app, resource={
r"/*":{
"origins":"*"
}
})
2024-06-19 13:13:34 +08:00
app.config['SECRET_KEY'] = '350625'
2024-07-01 10:43:22 +08:00
socketio = SocketIO(app, cors_allowed_origins="*")
2024-06-19 13:13:34 +08:00
2024-07-01 10:43:22 +08:00
clients = {}
2024-06-19 13:13:34 +08:00
2024-06-20 14:55:36 +08:00
@app.route('/api/student', methods=['POST']) # 检测历史是否登录过登录则直接进入index反之则进去login界面
2024-06-19 13:13:34 +08:00
def student(): # 判断是否已经登录
ID_data = request.json
ID = ID_data['student_ID']
student_succeed = student_succeed_func(ID)
student_lesson = get_lesson_func(ID)
return jsonify({'student_succeed': student_succeed, 'student_lesson': student_lesson})
2024-06-20 14:55:36 +08:00
@app.route('/api/login', methods=['POST']) # 登录功能
2024-06-19 13:13:34 +08:00
def login():
login_data = request.json # 获取前端发送过来的数据
ID = login_data['ID'] # 解析数据
password = login_data['password']
if len(ID) == 8: # 根据长度判断,长度为十则是学生
try:
en_password, password = student_login_func(ID, password)
print(en_password, password)
# 使用数据库查询并将查询结果用login_password接受
if en_password == password: # 进行判断并返回
session['id'] = ID
print('True')
print(ID)
return jsonify({'result': '登录成功', 'user': 'user'})
else:
print('False')
return jsonify({'result': '密码错误'})
except IndexError:
return jsonify({'result': '账号不存在,请先注册'})
elif len(ID) == 6: # 长度为6则是老师
try:
if password == teacher_login_func(ID):
session['id'] = ID
return jsonify({'result': '登录成功', 'user': 'teacher'})
else:
return jsonify({'result': '密码错误'})
except IndexError:
return jsonify({'result': '账号不存在,请先注册'})
elif len(ID) == 9: # 若长度为9则是管理员账号根据数据库进行修改即可
try:
if password == administrator_login_func(ID):
session['id'] = ID
return jsonify({'result': '登录成功', 'user': 'Administrator'})
else:
return jsonify({'result': '密码错误'})
except IndexError:
return jsonify({'result': '账号不存在,请先注册'})
else:
return jsonify({'result': '账号不存在'})
# postman
2024-06-20 14:55:36 +08:00
@app.route('/api/logout', methods=['POST']) # 退出登录功能
2024-06-19 13:13:34 +08:00
def logout(): # 退出登录
ID = session['id']
print(ID)
session.pop('id', None) # 删除id
return jsonify({'result': '注销成功'})
2024-06-20 14:55:36 +08:00
@app.route('/api/register', methods=['POST']) # 注册功能实现
2024-06-19 13:13:34 +08:00
def register():
register_data = request.json # 获取前端发送过来的数据
name = register_data['name']
ID = register_data['ID']
password = register_data['password']
Class = register_data['Class']
gender = register_data['gender']
try:
register_func(name, ID, password, Class, gender)
except:
return jsonify({'result': '注册失败'})
return jsonify({'result': '注册成功'})
2024-06-20 14:55:36 +08:00
@app.route('/api/student/get_lesson', methods=['GET'])
2024-06-19 13:13:34 +08:00
def get_lesson(): # 获取课程
student_id = session.get('id')
student_lesson = get_lesson_func(student_id)
print(student_lesson)
return jsonify({"student_lesson": student_lesson})
# 学生获取试卷
2024-06-20 14:55:36 +08:00
@app.route('/api/student/get_test', methods=['POST'])
2024-06-19 13:13:34 +08:00
def get_test(): # 获取试卷以及历史试卷
data = request.json
ID = data['student_ID']
2024-06-29 11:58:02 +08:00
result1=fetch_test_func(ID)
result2=falseTest_func(ID)
return jsonify({"True":result1,'FalseTest':result2})
2024-06-29 17:07:59 +08:00
@app.route('/api/student/TestData',methods=['POST'])
def TestData(): # 获取试卷ID
examID=request.json['examId']
result=TestDataFunc(examID)
return result
2024-06-19 13:13:34 +08:00
2024-06-20 14:55:36 +08:00
@app.route('/api/student/fetch_result', methods=['POST']) # 查找成绩
2024-06-19 13:13:34 +08:00
def fetch_result():
data = request.json
ID = data['student_ID']
return jsonify({'result': fetch_result_func(ID)})
2024-06-29 17:07:59 +08:00
2024-06-19 13:13:34 +08:00
2024-06-20 14:55:36 +08:00
@app.route('/api/student/score_entry', methods=['POST'])
2024-06-19 13:13:34 +08:00
def score_entry():
data = request.json
ID = data['student_ID']
score = data['score']
testID = data['examId']
testID = int(testID)
score_entry_func(score, testID, ID)
return jsonify({'result': '成功'})
2024-06-20 15:30:16 +08:00
@app.route('/api/student/FetchTrainTitle',methods=['POST'])
2024-06-20 14:54:17 +08:00
def FetchTrainTitle():
data = request.json
ID = data['student_ID']
title=FetchTrainTitleFunc(ID)
return jsonify({'title': title})
2024-06-21 14:40:55 +08:00
@app.route('/api/student/TrainData',methods=['POST'])
def TrainData():
2024-06-24 19:51:39 +08:00
data=request.json
examID=data['operateID']
2024-06-21 14:40:55 +08:00
result=FetchTrainTestFunc(examID)
return result
2024-06-24 19:51:39 +08:00
@app.route('/api/student/HistoryTrain',methods=['POST'])
def HistoryTrain():
ID = request.json['student_ID']
result=HistoryTrainFunc(ID)
return jsonify({'HistoryTrain': result})
2024-06-29 11:58:02 +08:00
@app.route('/api/student/FindTrain1Src',methods=['POST'])
def FindTrain1Src():
ID = request.json['student_ID']
testID=request.json['operateID']
result=FindTrain1SrcFunc(ID,testID)
return jsonify({'Src':result})
2024-06-21 14:40:55 +08:00
2024-06-20 14:54:17 +08:00
2024-06-19 13:13:34 +08:00
# 以下为教师功能—————————————————————————————————————————————————————————————————————
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/return_question', methods=['POST'])
2024-06-19 13:13:34 +08:00
def return_question():
data = request.json
ID = data['teacher_ID']
choice_question = choice_question_func(ID) # 调取相应老师科目的题库
completion_question = completion_question_func(ID)
t_or_f_question = t_or_f_question_func(ID)
2024-06-26 19:33:02 +08:00
return jsonify({'choice': choice_question, 'completion': completion_question, 'judge': t_or_f_question})
2024-06-19 13:13:34 +08:00
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/select_class', methods=['POST'])
2024-06-19 13:13:34 +08:00
def select_class(): # 查找每个老师管理的班级 以及在考试管理查找默认班级
teacher_id = request.json['teacher_ID']
# testID =request.json['testID']
class_dic = find_classboss_succeed_func(teacher_id)
default_class = list(class_dic.keys())[0]
# print(testID)
return jsonify({'Class': class_dic,'default': find_default_class_func(default_class, teacher_id)})
2024-07-01 21:48:29 +08:00
@app.route('/api/teacher/marktrain',methods=['POST'])
def mark(): # 批阅试卷
ClassID=request.json['ClassID']
teacher_ID=request.json['teacher_ID']
return jsonify({'MarkClass': MarkTrainFunc(ClassID,teacher_ID)})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/change_class', methods=['POST'])
2024-06-19 13:13:34 +08:00
def change_class(): # 查找每个老师管理的班级 以及在考试管理查找默认班级
teacher_id = request.json['teacher_ID']
ClassID=request.json['selectedValue']
return jsonify({'default': find_default_class_func(ClassID,teacher_id)})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/detaileddata', methods=['POST'])
2024-06-19 13:13:34 +08:00
def detaileddata():
data=request.json
ClassID=data['selectedValue']
testID=data['testID']
return jsonify({'data':detailed_class(testID,ClassID)})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/', methods=['POST'])
2024-06-19 13:13:34 +08:00
def find_test():
return
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/find_student', methods=['POST'])
2024-06-19 13:13:34 +08:00
def find_student(): # 获取班级学生的具体信息
student_list = []
Data = request.json
ClassID = Data['key']
student_succeed = find_class_student(ClassID)
for i in student_succeed:
student_list.append(find_student_succeed_func(i))
return [student_succeed, student_list]
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/accept_test', methods=['POST']) # 在前端发布成功之后要存入数据库
2024-06-19 13:13:34 +08:00
def accept_test():
data = request.json
2024-06-26 19:33:02 +08:00
choice_list = data['Choice']
completion_list = data['Completion']
judge_list = data['Judge']
2024-06-19 13:13:34 +08:00
hour = data['HourValue']
min = data['MinValue']
stop_time = data['StopTime']
class_list = data['selectedItems']
ID = data['teacher_ID']
save_test(choice_list, completion_list, judge_list, hour, min, stop_time, class_list, ID)
print('添加成功')
return jsonify({'result': '发布成功'})
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/fetch_train_question',methods=["POST"])
2024-06-19 13:13:34 +08:00
def fetch_train_question():
return train_question()
2024-06-20 14:55:36 +08:00
@app.route('/api/teacher/SendTrainTest',methods=["POST"])
2024-06-19 13:13:34 +08:00
def SendTrainTest():
data=request.json
TrainChoice=data['TrainChoice']
TrainCompletion=data['TrainCompletion']
TrainJudge=data['TrainJudge']
Hour=data['HourValue']
Min=data['MinValue']
2024-07-03 14:08:43 +08:00
startTime=convert_iso_to_database_format(data['startDate'])
endTime=convert_iso_to_database_format(data['endDate'])
2024-06-19 13:13:34 +08:00
Class=data['selectedItems']
Train=data['Train']
teacher_id=data['teacher_ID']
2024-07-03 14:08:43 +08:00
SendTrainTestFunc(TrainChoice, TrainCompletion, TrainJudge, Hour, Min,Class,Train,teacher_id,startTime,endTime)
SendLink()
TeacherMark(Class, teacher_id)
2024-06-19 13:13:34 +08:00
return '发布成功'
2024-06-25 12:51:35 +08:00
2024-06-26 11:24:33 +08:00
@app.route('/api/teacher/Find_details',methods=["POST"])
2024-06-25 14:49:31 +08:00
def Find_details():
2024-06-26 11:24:33 +08:00
data=request.json
ID=data['ID']
result=Find_details_Func(ID)
print(result)
return jsonify({'TestScore':result})
2024-07-03 14:08:43 +08:00
@app.route('/api/teacher/NotMarkTest',methods=["POST"])
def NotMarkTest():
testID=request.json['testID']
result=NotMarkTestFunc(testID)
return jsonify({'result':result})
@app.route('/api/teacher/UpdateTest',methods=["POST"])
def UpdateTest():
testID=request.json['testID']
score=request.json['score']
2024-07-04 10:00:31 +08:00
suggestion=request.json['suggestion']
submitScoreFunc(score,testID,suggestion)
2024-07-03 14:08:43 +08:00
print('修改成功')
return '修改成功'
@app.route('/api/teacher/getTrain',methods=["POST"])
def GetTrain():
teacherID=request.json['teacher_ID']
data=getTrainFunc(teacherID)
return jsonify({'data':data})
2024-07-02 13:00:00 +08:00
@app.route("/api/teacher/list_pods") # 列出pod
2024-07-01 10:43:22 +08:00
def teacher_list_pods():
2024-07-03 14:08:43 +08:00
return jsonify(list_pods())
2024-07-02 13:00:00 +08:00
@app.route("/api/teacher/list_services") # 列出服务
2024-07-01 10:43:22 +08:00
def teacher_list_services():
return list_services()
2024-07-03 14:08:43 +08:00
@app.route("/api/teacher/create_pod") # 创建服务 1为项目实训 0为安装实训
2024-07-01 10:43:22 +08:00
def teacher_create_pod():
2024-07-03 14:08:43 +08:00
create_pod(1, "test2")
2024-07-01 10:43:22 +08:00
2024-07-02 13:00:00 +08:00
@app.route("/api/teacher/delete_pod") # 删除服务
def teacher_delete_pod():
2024-07-03 14:08:43 +08:00
return delete_pod("test2")
2024-06-25 12:51:35 +08:00
2024-07-04 10:00:31 +08:00
@app.route("/api/student/check_pod",methods=['POST']) # 检测数据库是否安装成功 若成功返回OK 否则返回NO
2024-07-02 13:00:00 +08:00
def teacher_check_pod():
2024-07-04 10:00:31 +08:00
name=request.json["student_ID"]
testID=request.json["operateID"]
name= 'n' + str(name) + '-' + str(testID)
result = check_dm(name)
return jsonify({'result':result})
2024-06-25 12:51:35 +08:00
2024-06-19 19:23:01 +08:00
@app.route('/')
@app.route('/<path:path>')
def catch_all(path = "index.html"):
return send_from_directory("public", path)
2024-07-01 10:43:22 +08:00
@socketio.on('connect_ssh')
def handle_connect_ssh(data):
ip = data['ip']
port = int(data['port'])
password = data['password']
2024-07-02 13:00:00 +08:00
user = data['user']
2024-07-01 10:43:22 +08:00
client_id = request.sid
2024-07-02 13:00:00 +08:00
print("connected to " + ip + " " + str(port) + " " + password)
2024-07-01 10:43:22 +08:00
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
2024-07-02 13:00:00 +08:00
ssh_client.connect(ip, port=port, username=user, password=password)
2024-07-01 10:43:22 +08:00
transport = ssh_client.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.invoke_shell()
clients[client_id] = {
'ssh_client': ssh_client,
'channel': channel
}
socketio.start_background_task(target=read_from_channel, channel=channel, client_id=client_id)
except Exception as e:
emit('output', f'Connection failed: {str(e)}')
def read_from_channel(channel, client_id):
while True:
if channel.recv_ready():
data = channel.recv(1024).decode('utf-8')
socketio.emit('output', data, room=client_id)
@socketio.on('input')
def handle_input(data):
client_id = request.sid
if client_id in clients:
clients[client_id]['channel'].send(data)
@socketio.on('disconnect')
def handle_disconnect():
client_id = request.sid
if client_id in clients:
clients[client_id]['channel'].close()
clients[client_id]['ssh_client'].close()
del clients[client_id]
2024-06-19 19:23:01 +08:00
2024-06-19 13:13:34 +08:00
if __name__ == '__main__':
2024-07-02 13:00:21 +08:00
socketio.run(app, host='0.0.0.0', port=5000 ,allow_unsafe_werkzeug=True, debug=True)
2024-07-01 10:43:22 +08:00