提交作业

yugaoxiang 2022-12-31 18:25:13 +08:00
parent f0afd5f40f
commit 0209548896
4 changed files with 188 additions and 3 deletions

View File

@ -5,7 +5,7 @@
# Copyright 2019 (C) Hui Lan <hui.lan@cantab.net> # Copyright 2019 (C) Hui Lan <hui.lan@cantab.net>
# Written permission must be obtained from the author for commercial uses. # Written permission must be obtained from the author for commercial uses.
########################################################################### ###########################################################################
from datetime import datetime
from flask import escape from flask import escape
from Login import * from Login import *
from Article import * from Article import *
@ -102,6 +102,78 @@ def mainpage():
d_len=d_len, lst=lst, yml=Yaml.yml) d_len=d_len, lst=lst, yml=Yaml.yml)
def insert_article(content, source='manual_input', level=5, question=''):
sql = f"INSERT into article (text,source, date, level, question) VALUES " \
f"('{content}','{source}','{datetime.now().strftime('%Y-%m-%d')}', '{level}', '{question}');"
print(sql)
rq = RecordQuery('./static/wordfreqapp.db')
rq.instructions(sql)
rq.do()
def get_articles():
sql = f"SELECT * from article order by -article_id;"
rq = RecordQuery('./static/wordfreqapp.db')
rq.instructions(sql)
rq.do()
result = rq.get_results()
return result
def get_users():
sql = f"SELECT * from user;"
rq = RecordQuery('./static/wordfreqapp.db')
rq.instructions(sql)
rq.do()
result = rq.get_results()
return result
def update_user_password(username, password='123456'):
password = md5(username + password)
sql = f"UPDATE user SET password='{password}' where name='{username}';"
rq = RecordQuery('./static/wordfreqapp.db')
rq.instructions(sql)
rq.do()
@app.route("/admin", methods=['GET', 'POST'])
def admin():
'''
'''
# 未登录,跳转到未登录界面
if not session.get('logged_in'):
return render_template('not_login.html')
# 获取session里的用户名
username = session.get('username')
context = {
# 'user': request.user,
'text_list': get_articles(),
'user_list': get_users(),
'username': username
}
if request.method == 'GET':
return render_template('admin_index.html', **context)
else:
data = request.form
content = data.get('content')
source = data.get('source', '')
question = data.get('question', '')
username = data.get('username')
if content:
insert_article(
content=content,
source=source,
question=question
)
context['text_list'] = get_articles()
if username:
update_user_password(username)
return render_template('admin_index.html', **context)
if __name__ == '__main__': if __name__ == '__main__':
''' '''

View File

@ -0,0 +1,112 @@
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
</head>
<body class="container" style="width: 800px; margin: auto; margin-top:24px;">
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">你好 {{ username }}</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/logout">退出登录</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="card" style="margin-top:24px;">
<h5>重置选中用户密码</h5>
<form action="" method="post" class="container mb-3">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">用户</label>
<select
id="username"
name="username"
class="form-select"
aria-label="Default select example">
<option selected>选择用户</option>
{% for user in user_list %}
<option value="{{ user['name'] }}">{{ user['name'] }}</option>
{% endfor %}
</select>
</div>
<input type="submit" value="重置密码为123456" class="btn btn-primary">
</form>
</div>
<div class="card" style="margin-top:24px;">
{% if tips %}
<div class="alert alert-success" role="alert">
{{ tips }}
</div>
{% endif %}
<div class="card-content">
<h5>录入文章</h5>
<form action="" method="post" class="container mb-3">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">文章内容</label>
<textarea
id="content"
name="content"
class="form-control"
placeholder="请输入文章内容"
></textarea>
<label for="exampleInputEmail1" class="form-label">文章来源</label>
<textarea
id="source"
name="source"
class="form-control"
placeholder="请输入来源"
></textarea>
<label for="exampleInputEmail1" class="form-label">文章问题</label>
<textarea
id="question"
name="question"
class="form-control"
placeholder="请输入问题"
></textarea>
</div>
<input type="submit" value="保存" class="btn btn-primary">
</form>
</div>
</div>
<div class="card" style="margin-top:24px;">
<h5>文章列表</h5>
<div class="list-group">
{% for text in text_list %}
<div class="list-group-item list-group-item-action" aria-current="true">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ text['source'] }}</h5>
<small>Date:{{ text['date'] }} Level:{{ text['level'] }}</small>
</div>
<p class="mb-1">{{ text['text'] }}</p>
</div>
{% endfor %}
</div>
</div>
</body>
</html>

View File

@ -23,9 +23,9 @@
<div class="container-fluid"> <div class="container-fluid">
<p><b><font size="+3" color="red">English Pal - Learn English smartly!</font></b></p> <p><b><font size="+3" color="red">English Pal - Learn English smartly!</font></b></p>
{% if session['logged_in'] %} {% if session['logged_in'] %}
<a href="/{{session['username']}}">{{session['username']}}</a></p> <a href="/{{session['username']}}">{{session['username']}}</a> <a href="/admin">管理</a></p>
{% else %} {% else %}
<p><a href="/login">登录</a> <a href="/signup">注册</a> <a href="/static/usr/instructions.html">使用说明</a></p > <p><a href="/admin">管理</a> <a href="/login">登录</a> <a href="/signup">注册</a> <a href="/static/usr/instructions.html">使用说明</a></p >
<p><b>{{random_ads|safe}}</b></p> <p><b>{{random_ads|safe}}</b></p>
{% endif %} {% endif %}
<div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{number_of_essays}} </span></div> <div class="alert alert-success" role="alert">共有文章 <span class="badge bg-success"> {{number_of_essays}} </span></div>

View File

@ -23,6 +23,7 @@
<body> <body>
<div class="container-fluid"> <div class="container-fluid">
<p><b>English Pal for <font id="username" color="red">{{ username }}</font></b> <p><b>English Pal for <font id="username" color="red">{{ username }}</font></b>
<a class="btn btn-secondary" href="/admin" role="button" onclick="stopRead()">管理</a>
<a class="btn btn-secondary" href="/logout" role="button" onclick="stopRead()">退出</a> <a class="btn btn-secondary" href="/logout" role="button" onclick="stopRead()">退出</a>
<a class="btn btn-secondary" href="/reset" role="button" onclick="stopRead()">重设密码</a> <a class="btn btn-secondary" href="/reset" role="button" onclick="stopRead()">重设密码</a>
</p> </p>