diff --git a/Lesson_5/Task 4/.gitignore b/Lesson_5/Task 4/.gitignore new file mode 100644 index 0000000..9bca40a --- /dev/null +++ b/Lesson_5/Task 4/.gitignore @@ -0,0 +1 @@ +articles.txt diff --git a/Lesson_5/Task 4/templates/admin.html b/Lesson_5/Task 4/templates/admin.html new file mode 100644 index 0000000..0204ff3 --- /dev/null +++ b/Lesson_5/Task 4/templates/admin.html @@ -0,0 +1,13 @@ + + + + Editor + + +
+
+
+ +
+ + diff --git a/Lesson_5/Task 4/templates/editor.html b/Lesson_5/Task 4/templates/editor.html new file mode 100644 index 0000000..0add54c --- /dev/null +++ b/Lesson_5/Task 4/templates/editor.html @@ -0,0 +1,15 @@ + + + + Editor + + +
+
+
+
+
+ +
+ + diff --git a/Lesson_5/Task 4/templates/index.html b/Lesson_5/Task 4/templates/index.html new file mode 100644 index 0000000..6bd91c8 --- /dev/null +++ b/Lesson_5/Task 4/templates/index.html @@ -0,0 +1,13 @@ + + + + News + + +
+ {{contents|safe}} +
+ Add
+ Admin + + diff --git a/Lesson_5/Task 4/webserver.py b/Lesson_5/Task 4/webserver.py new file mode 100644 index 0000000..a1fd8c5 --- /dev/null +++ b/Lesson_5/Task 4/webserver.py @@ -0,0 +1,49 @@ +from flask import Flask, render_template, redirect, request +from flask_httpauth import HTTPBasicAuth +from werkzeug.security import generate_password_hash, check_password_hash +from datetime import datetime + +app = Flask("News") +auth = HTTPBasicAuth() + +users = { + "admin": generate_password_hash("example") +} + +@auth.verify_password +def verify_password(username, password): + if username in users and \ + check_password_hash(users.get(username), password): + return username + +@app.route('/') +def index(): + with open("articles.txt", "r") as file: + data = file.read() + return render_template("index.html", contents=data) + +@app.route('/editor') +def editor(): + return render_template("editor.html") + +@app.route('/process.html') +def process(): + with open("articles.txt", "a") as file: + file.write("

" + str(request.args.get('title')) + "

\n

" + str(datetime.now()) + "

\n

" + str(request.args.get('text')) + "


\n") + return redirect('/') + +@app.route('/admin') +@auth.login_required +def admin(): + with open("articles.txt", "r") as file: + data = file.read() + return render_template("admin.html", contents=data) + +@app.route('/process_admin.html') +@auth.login_required +def process_admin(): + with open("articles.txt", "w") as file: + file.write(str(request.args.get('data'))) + return redirect('/') + +app.run(port=8080)