From 35addea923c939e34c6ce91ca22d223438d9e6cf Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Mon, 2 Jan 2023 19:15:05 +0200 Subject: [PATCH] Add Lesson 5 Task 4 --- Lesson_5/Task 4/.gitignore | 1 + Lesson_5/Task 4/templates/admin.html | 13 +++++++ Lesson_5/Task 4/templates/editor.html | 15 ++++++++ Lesson_5/Task 4/templates/index.html | 13 +++++++ Lesson_5/Task 4/webserver.py | 49 +++++++++++++++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 Lesson_5/Task 4/.gitignore create mode 100644 Lesson_5/Task 4/templates/admin.html create mode 100644 Lesson_5/Task 4/templates/editor.html create mode 100644 Lesson_5/Task 4/templates/index.html create mode 100644 Lesson_5/Task 4/webserver.py 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)