Add Lesson 5 Task 4
This commit is contained in:
parent
54fc772c83
commit
35addea923
5 changed files with 91 additions and 0 deletions
1
Lesson_5/Task 4/.gitignore
vendored
Normal file
1
Lesson_5/Task 4/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
articles.txt
|
13
Lesson_5/Task 4/templates/admin.html
Normal file
13
Lesson_5/Task 4/templates/admin.html
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Editor</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="/process_admin.html">
|
||||||
|
<label for="data">Data: </label><br>
|
||||||
|
<textarea name="data" rows="5" cols="50">{{contents|safe}}</textarea><br>
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
15
Lesson_5/Task 4/templates/editor.html
Normal file
15
Lesson_5/Task 4/templates/editor.html
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Editor</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="/process.html">
|
||||||
|
<label for="title">Title: </label><br>
|
||||||
|
<input type="text" id="title" name="title"><br>
|
||||||
|
<label for="text">Name: </label><br>
|
||||||
|
<textarea name="text" rows="5" cols="50" ></textarea><br>
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
13
Lesson_5/Task 4/templates/index.html
Normal file
13
Lesson_5/Task 4/templates/index.html
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>News</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
{{contents|safe}}
|
||||||
|
</div>
|
||||||
|
<a href="/editor">Add</a><br>
|
||||||
|
<a href="/admin">Admin</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
49
Lesson_5/Task 4/webserver.py
Normal file
49
Lesson_5/Task 4/webserver.py
Normal file
|
@ -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("<h1>" + str(request.args.get('title')) + "</h1>\n<h4>" + str(datetime.now()) + "</h4>\n<h2>" + str(request.args.get('text')) + "</h2><hr>\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)
|
Reference in a new issue