-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
67 lines (53 loc) · 1.71 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask import Flask, render_template, request, redirect, url_for
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
app = Flask(__name__)
app.secret_key = 'mysecretkey'
login_manager = LoginManager()
login_manager.init_app(app)
users = {'user': {'password': 'password'},
'user2': {'password': 'password'}}
messages = []
class User(UserMixin):
def __init__(self, id_, password):
self.id = id_
self.password = password
def __repr__(self):
return f'<User: {self.id}>'
@login_manager.user_loader
def load_user(user_id):
if user_id in users:
return User(user_id, users[user_id]['password'])
return None
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username in users and password == users[username]['password']:
user = User(username, password)
login_user(user)
return redirect(url_for('home'))
else:
return 'Invalid username/password combination'
else:
return render_template('login.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/')
@login_required
def home():
return render_template('index.html', messages=messages)
@app.route('/post', methods=['POST'])
@login_required
def post():
messages.append(f"{current_user.id}: {request.form['message']}")
return redirect(url_for('home'))
@app.route('/about')
@login_required
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)