Add files via upload

This commit is contained in:
你是一个一个一个开发者啊 2023-06-20 12:03:01 +08:00 committed by GitHub
commit 0b5c459120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 593 additions and 0 deletions

Binary file not shown.

204
school system/app.py Normal file
View File

@ -0,0 +1,204 @@
from flask import Flask, request, redirect,render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://db_experiment3:12345@192.168.15.179/db_experiment3"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Student(db.Model):
sno = db.Column(db.String(10), primary_key=True)
sname = db.Column(db.String(20))
ssex = db.Column(db.String(4))
sage = db.Column(db.Integer)
pwd = db.Column(db.String(20))
class Teacher(db.Model):
tno = db.Column(db.String(7), primary_key=True)
tname = db.Column(db.String(20))
tposition = db.Column(db.String(3))
tsalary = db.Column(db.Integer)
pwd = db.Column(db.String(20))
class Course(db.Model):
cno = db.Column(db.String(4), primary_key=True)
cname = db.Column(db.String(40))
tno = db.Column(db.String(7))
ccredit = db.Column(db.Integer)
class SC(db.Model):
cno = db.Column(db.String(4),db.ForeignKey('course.cno'), primary_key=True)
sno = db.Column(db.String(10),db.ForeignKey('student.sno'), primary_key=True)
grade = db.Column(db.Integer)
tno = db.Column(db.String(7))
class Admin(db.Model):
ano = db.Column(db.String(10), primary_key=True)
pwd = db.Column(db.String(20))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == "GET":
return render_template('login.html')
else:
if request.form['login-method'] == 'student':
to_login = db.session.query(Student).get(request.form['account'])
if to_login is None:
return "没有此用户"
if to_login.pwd == request.form['password']:
print(to_login.sno + "登录成功")
return redirect('/'+request.form['login-method']+'/'+request.form['account'])
else:
print(to_login.sno + "密码错误")
return "密码错误"
elif request.form['login-method'] == 'teacher':
to_login = db.session.query(Teacher).get(request.form['account'])
if to_login is None:
return "没有此用户"
if to_login.pwd == request.form['password']:
print(to_login.tno + "登录成功")
return redirect('/'+request.form['login-method']+'/'+request.form['account'])
else:
print(to_login.tno + "密码错误")
return "密码错误"
elif request.form['login-method'] == 'admin':
to_login = db.session.query(Admin).get(request.form['account'])
if to_login is None:
return "没有此用户"
if to_login.pwd == request.form['password']:
print(to_login.ano + "登录成功")
return redirect('/'+request.form['login-method']+'/'+request.form['account'])
else:
print(to_login.ano + "密码错误")
return "密码错误"
@app.route('/student/<sno>', methods=['GET', 'POST'])
def student(sno):
user_info = db.session.query(Student).get(sno)
user_course = db.session.query(SC,Course).join(SC,SC.cno == Course.cno).filter(SC.sno == sno).all()
course_list = db.session.query(Course).all()
credit_sum = sum([x[1].ccredit for x in user_course])
temp = [user_course[x][1] for x in range(len(user_course))]
selectable_course = [x for x in course_list if x not in [user_course[x][1] for x in range(len(user_course))]]
if request.method == 'GET':
return render_template('student.html',
username = user_info.sname,usergender = user_info.ssex,userage = user_info.sage,
usercourse = user_course,totalcredit = credit_sum,
selectablecourse = selectable_course)
if request.method == 'POST':
if request.form['type'] == 'changeinfo':
if user_info:
user_info.sname = request.form['name']
user_info.ssex = request.form['gender']
user_info.sage = request.form['age']
if request.form['password'] != "":
user_info.pwd = request.form['password']
db.session.commit()
return "个人信息修改完毕"
elif request.form['type'] == 'courseselection':
to_select = next(filter(lambda x: x.cno == request.form['course'],selectable_course))
to_submit = SC(cno = request.form['course'],sno = sno,tno = to_select.tno,grade = 0)
db.session.add(to_submit)
db.session.commit()
return "选课成功"
@app.route('/admin/<ano>', methods=['GET', 'POST'])
def admin(ano):
student_list = db.session.query(Student,db.func.coalesce(db.func.sum(Course.ccredit),0)).\
join(SC,Student.sno == SC.sno, isouter=True).join(Course,SC.cno == Course.cno, isouter=True).group_by(Student.sno).all()
teacher_list = db.session.query(Teacher).all()
teacher_position_list = db.session.query(Teacher.tposition,db.func.avg(Teacher.tsalary),db.func.count(Teacher.tno)).\
group_by(Teacher.tposition).all()
course_list = db.session.query(Course,db.func.max(SC.grade),db.func.min(SC.grade),db.func.avg(SC.grade)).\
join(SC,Course.cno == SC.cno).group_by(Course.cno).all()
sc_list = db.session.query(SC).group_by(SC.cno).all()
if request.method == 'GET':
return render_template("admin.html",studentlist=student_list,
teacherlist=teacher_list,
salary=teacher_position_list,
courselist=course_list,
sclist=sc_list)
if request.method == 'POST':
if request.form['type'] == 'changestudent':
student = db.session.query(Student).get(request.form['sno'])
student.sname = request.form['sname']
student.sage = request.form['sage']
student.pwd = request.form['pwd']
student.ssex = request.form['ssex']
db.session.commit()
return "修改学生信息成功"
elif request.form['type'] == 'addstudent':
db.session.add(Student(sno = request.form['sno'],
sname=request.form['sname'],
sage = request.form['sage'],
pwd = request.form['pwd'],
ssex=request.form['ssex']
))
db.session.commit()
return "添加学生成功"
elif request.form['type'] == 'deletestudent':
db.session.delete(Student.query.get(request.form['sno']))
db.session.commit()
return "学生已删除"
elif request.form['type'] == 'changeteacher':
teacher = db.session.query(Teacher).get(request.form['tno'])
teacher.tname = request.form['tname']
teacher.tposition = request.form['tposition']
teacher.pwd = request.form['pwd']
teacher.tsalary = request.form['tsalary']
db.session.commit()
return "修改教师信息成功"
elif request.form['type'] == 'addteacher':
db.session.add(Teacher(tno = request.form['tno'],
tname=request.form['tname'],
tposition = request.form['tposition'],
pwd = request.form['pwd'],
tsalary=request.form['tsalary']
))
db.session.commit()
return "添加教师成功"
elif request.form['type'] == 'deleteteacher':
db.session.delete(Teacher.query.get(request.form['tno']))
db.session.commit()
return "教师已删除"
elif request.form['type'] == 'changesc':
sc = db.session.query(SC).filter_by(sno=request.form['sno'],cno=request.form['cno']).first()
sc.grade = request.form['grade']
db.session.commit()
return "成绩修改成功"
elif request.form['type'] == 'addsc':
db.session.add(SC(cno = request.form['cno'],
sno=request.form['sno'],
grade = request.form['grade']
))
db.session.commit()
return "选课信息添加成功"
elif request.form['type'] == 'deletesc':
db.session.delete(Teacher.query().filter_by(sno=request.form['sno'],cno=request.form['cno']).first())
db.session.commit()
return "选课信息已删除"
@app.errorhandler(404)
def page_not_found(e):
return redirect('/login')
with app.app_context():
db.engine.connect()
if __name__ == '__main__':
app.run()

View File

@ -0,0 +1,268 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理员系统</title>
</head>
<body>
<h2>数据库实验-管理员系统</h2>
<br>
<h3>学生信息修改</h3>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>密码</th>
<th>总学分</th>
<th>提交修改</th>
<th>删除学生</th>
</tr>
</thead>
<tbody>
{% for i in studentlist %}
<tr>
<form action="" method="post">
<th>
{{ i[0].sno }}<input name="sno" type="hidden" value="{{ i[0].sno }}">
</th>
<th>
<input name="sname" type="text" value="{{ i[0].sname }}">
</th>
<th>
{% if i[0].ssex == '男' %}
<select name="ssex">
<option value="男" selected></option>
<option value="女"></option>
</select>
{% else %}
<select name="ssex">
<option value="男"></option>
<option value="女" selected></option>
</select>
{% endif %}
</th>
<th>
<input name="sage" type="text" value="{{ i[0].sage }}">
</th>
<th>
<input name="pwd" type="text" value="{{ i[0].pwd }}">
</th>
<th>
{{ i[1] }}
</th>
<th>
<button type="submit" name="type" value="changestudent">提交修改</button>
</th>
<th>
<button type="submit" name="type" value="deletestudent">删除学生</button>
</th>
</form>
</tr>
{% endfor %}
<tr>
<form action="" method="post">
<input name="type" type="hidden" value="addstudent">
<th>
<input name="sno" type="text">
</th>
<th>
<input name="sname" type="text">
</th>
<th>
<select name="ssex">
<option value="男"></option>
<option value="女"></option>
</select>
</th>
<th>
<input name="sage" type="text">
</th>
<th>
<input name="pwd" type="text">
</th>
<th>
<button type="submit">添加学生</button>
</th>
</form>
</tr>
</tbody>
</table>
<br>
<h3>教师信息修改</h3>
<table>
<thead>
<tr>
<th>教师号</th>
<th>姓名</th>
<th>职称</th>
<th>工资</th>
<th>密码</th>
<th>提交修改</th>
<th>删除教师</th>
</tr>
</thead>
<tbody>
{% for i in teacherlist %}
<tr>
<form action="" method="post">
<th>
{{ i.tno }}<input name="tno" type="hidden" value="{{ i.tno }}">
</th>
<th>
<input name="tname" type="text" value="{{ i.tname }}">
</th>
<th>
<input name="tposition" type="text" value="{{ i.tposition }}">
</th>
<th>
<input name="tsalary" type="text" value="{{ i.tsalary }}">
</th>
<th>
<input name="pwd" type="text" value="{{ i.pwd }}">
</th>
<th>
<button type="submit" name="type" value="changeteacher">提交修改</button>
</th>
<th>
<button type="submit" name="type" value="deleteteacher">删除教师</button>
</th>
</form>
</tr>
{% endfor %}
<tr>
<form action="" method="post">
<input name="type" type="hidden" value="addteacher">
<th>
<input name="tno" type="text">
</th>
<th>
<input name="tname" type="text">
</th>
<th>
<input name="tposition" type="text">
</th>
<th>
<input name="tsalary" type="text">
</th>
<th>
<input name="pwd" type="text">
</th>
<th>
<button type="submit">添加教师</button>
</th>
</form>
</tr>
</tbody>
</table>
<br>
<h3>教师信息统计</h3>
<table>
<thead>
<tr>
<th>职称</th>
<th>平均工资</th>
<th>人数</th>
</tr>
</thead>
<tbody>
{% for i in salary %}
<tr>
<th>
{{ i[0] }}
</th>
<th>
{{ i[1] }}
</th>
<th>
{{ i[2] }}
</th>
</tr>
{% endfor %}
</tbody>
</table>
<br>
<h3>课程信息统计</h3>
<table>
<thead>
<tr>
<th>课程号</th>
<th>课程名</th>
<th>最高成绩</th>
<th>最低成绩</th>
<th>平均成绩</th>
</tr>
</thead>
<tbody>
{% for i in courselist %}
<tr>
<th>{{i[0].cno}}</th>
<th>{{ i[0].cname }}</th>
<th>{{i[1]}}</th>
<th>{{i[2]}}</th>
<th>{{i[3]}}</th>
</tr>
{% endfor %}
</tbody>
</table>a
<br>
<h3>选课及成绩统计</h3>
<table>
<thead>
<tr>
<th>课程号</th>
<th>学生号</th>
<th>成绩</th>
<th>修改</th>
<th>删除</th>
</tr>
</thead>
<tbody>
{% for i in sclist %}
<tr>
<form action="" method="post">
<th>
<input name="cno" type="hidden" value="{{ i.cno }}">{{ i.cno }}
</th>
<th>
<input name="sno" type="hidden" value="{{ i.sno }}">{{ i.sno }}
</th>
<th>
<input name="grade" type="text" value="{{ i.grade }}">
</th>
<th>
<button type="submit" name="type" value="changesc">提交修改</button>
</th>
<th>
<button type="submit" name="type" value="deletesc">删除选课</button>
</th>
</form>
</tr>
{% endfor %}
<tr>
<form action="" method="post">
<input name="type" type="hidden" value="addsc">
<th>
<input name="cno" type="text">
</th>
<th>
<input name="sno" type="text">
</th>
<th>
<input name="grade" type="text">
</th>
<th>
<button type="submit">添加信息</button>
</th>
</form>
</tr>
</tbody>
</table>
</body>
</html>

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<title>数据库实验</title>
<meta charset="UTF-8">
</head>
<body>
<h2>数据库实验-学校管理系统</h2>
<form action="/login" method="post">
<label for="login-method">登录选项:</label>
<select id="login-method" name="login-method">
<option value="student">学生登录</option>
<option value="teacher">教师登录</option>
<option value="admin">管理员登录</option>
</select><br><br>
<label for="account">账号:</label>
<input type="text" id="account" name="account"><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>

View File

@ -0,0 +1,94 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
<title>学生系统</title>
<meta charset="UTF-8">
</head>
<body>
<h2>数据库实验-学生系统</h2>
<br>
<h3>个人信息修改</h3>
<form action="" method="post">
<input type="hidden" name="type" value="changeinfo">
<label for="name">姓名:</label>
<input type="text" id="name" name="name" value="{{ username }}"><br>
{% if usergender == '男' %}
<input type="radio" name="gender" value="男" checked>
{% else %}
<input type="radio" name="gender" value="男">
{% endif %}
{% if usergender == '女' %}
<input type="radio" name="gender" value="女" checked>
{% else %}
<input type="radio" name="gender" value="女">
{% endif %}
<label for="age" >年龄:</label>
<input type="number" id="age" name="age" value="{{ userage }}"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<input type="submit" value="提交修改">
</form>
<br>
<h3>选课信息</h3>
<table>
<thead>
<tr>
<th>课程号</th>
<th>课程名</th>
<th>科任老师编号</th>
<th>学分</th>
</tr>
</thead>
<tbody>
{% for i in usercourse %}
<tr>
<th>{{ i[1].cno }}</th>
<th>{{ i[1].cname }}</th>
<th>{{ i[1].tno }}</th>
<th>{{ i[1].ccredit }}</th>
</tr>
{% endfor %}
</tbody>
</table>
<div>合计学分:{{ totalcredit }}</div>
<br>
<h3>选课</h3>
<table>
<thead>
<tr>
<th>课程号</th>
<th>课程名</th>
<th>科任老师编号</th>
<th>学分</th>
<th>选择</th>
</tr>
</thead>
<tbody>
{% for i in selectablecourse %}
<tr>
<th>{{ i.cno }}</th>
<th>{{ i.cname }}</th>
<th>{{ i.tno }}</th>
<th>{{ i.ccredit }}</th>
<th>
<form action="" method="POST">
<input type="hidden" name="type" value="courseselection">
<input type="hidden" name="course" value="{{ i.cno }}">
<input type="submit" value="选课">
</form>
</th>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>