diff --git a/app/main.py b/app/main.py index e311bb0..bcbd3f8 100644 --- a/app/main.py +++ b/app/main.py @@ -5,7 +5,7 @@ # Copyright 2019 (C) Hui Lan # Written permission must be obtained from the author for commercial uses. ########################################################################### - +from datetime import datetime from flask import escape from Login import * from Article import * @@ -102,6 +102,78 @@ def mainpage(): 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__': ''' diff --git a/app/templates/admin_index.html b/app/templates/admin_index.html new file mode 100644 index 0000000..be88556 --- /dev/null +++ b/app/templates/admin_index.html @@ -0,0 +1,112 @@ + + + + + + + + + + +
+
重置选中用户密码
+
+
+ + +
+ + +
+
+ +
+ {% if tips %} + + {% endif %} + +
+
录入文章
+
+
+ + + + + + + + +
+ + +
+
+
+ +
+
文章列表
+
+ {% for text in text_list %} +
+
+
{{ text['source'] }}
+ Date:{{ text['date'] }} Level:{{ text['level'] }} +
+

{{ text['text'] }}

+
+ {% endfor %} + +
+
+ + + + + + + + diff --git a/app/templates/mainpage_get.html b/app/templates/mainpage_get.html index cbb51a6..4cc4417 100644 --- a/app/templates/mainpage_get.html +++ b/app/templates/mainpage_get.html @@ -23,9 +23,9 @@

English Pal - Learn English smartly!

{% if session['logged_in'] %} - {{session['username']}}

+ {{session['username']}} 管理

{% else %} -

登录 注册 使用说明

+

管理 登录 注册 使用说明

{{random_ads|safe}}

{% endif %} diff --git a/app/templates/userpage_get.html b/app/templates/userpage_get.html index 24bff60..863d2e4 100644 --- a/app/templates/userpage_get.html +++ b/app/templates/userpage_get.html @@ -23,6 +23,7 @@

English Pal for {{ username }} + 管理 退出 重设密码