完成大作业服务端代码,新建管理端,管理端不可用
This commit is contained in:
9
Project/Manager/func/config.py
Normal file
9
Project/Manager/func/config.py
Normal file
@@ -0,0 +1,9 @@
|
||||
db = {
|
||||
'host':'localhost',
|
||||
'user':'kejingfan',
|
||||
'password':'KJF2811879',
|
||||
'database':'TESTDB'
|
||||
}
|
||||
|
||||
SECRET_KEY = 'ILOVEDATABASETECH'
|
||||
|
||||
32
Project/Manager/func/index.py
Normal file
32
Project/Manager/func/index.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from flask import render_template, request, g, redirect, url_for, session
|
||||
from .config import db
|
||||
import pymysql
|
||||
|
||||
def index():
|
||||
if request.method == 'GET':
|
||||
if not g.user:
|
||||
return redirect(url_for("login"))
|
||||
flightID = request.args.get('flightID')
|
||||
|
||||
conn = pymysql.connect(**db)
|
||||
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
||||
|
||||
search_sql = """ """
|
||||
cursor.execute(search_sql, (flightID, ))
|
||||
flights = cursor.fetchall()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
return render_template(
|
||||
'search.html',
|
||||
flights=flights,
|
||||
username=g.user
|
||||
)
|
||||
|
||||
def logout():
|
||||
session.clear()
|
||||
session.pop('user_id', None)
|
||||
return redirect(url_for('login'))
|
||||
|
||||
def modify():
|
||||
pass
|
||||
34
Project/Manager/func/login.py
Normal file
34
Project/Manager/func/login.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from flask import request, jsonify, session, url_for, render_template
|
||||
from .config import db
|
||||
import pymysql
|
||||
|
||||
|
||||
def connect(managerID, encrypted_password):
|
||||
conn = pymysql.connect(**db)
|
||||
cursor = conn.cursor(pymysql.cursors.DictCursor)
|
||||
args = (managerID, encrypted_password)
|
||||
verify_sql = "SELECT COUNT(*) FROM Managers WHERE ID = %s AND `Password` = %s;"
|
||||
cursor.execute(verify_sql, args)
|
||||
verified = cursor.fetchone()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return verified['COUNT(*)'] > 0
|
||||
|
||||
def login():
|
||||
if request.method == 'GET':
|
||||
return render_template('login.html')
|
||||
|
||||
if request.method == 'POST':
|
||||
session.pop('user_id', None)
|
||||
managerID = request.json.get('username')
|
||||
encrypted_password = request.json.get('password')
|
||||
try:
|
||||
user = connect(managerID, encrypted_password)
|
||||
if not user:
|
||||
return jsonify({'message': '账号或密码错误'}), 401
|
||||
session['user_id'] = managerID
|
||||
session.modified = True
|
||||
return jsonify({'redirect': url_for('index')})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return jsonify({'message': '数据库错误,请稍后再试'}), 500
|
||||
Reference in New Issue
Block a user