-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
62 lines (52 loc) · 1.87 KB
/
run.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
from flask import Flask, render_template, redirect, url_for, request, flash , send_from_directory
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
from config import Config
from models import db, User, Achievement
app = Flask(__name__ , template_folder=render_template)
app.config.from_object(Config)
db.init_app(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/')
def index():
return send_from_directory('html_files', 'index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username, password=password).first()
if user:
login_user(user)
return redirect(url_for('dashboard'))
else:
flash('Invalid credentials')
return render_template('login.html')
@app.route('/dashboard')
@login_required
def dashboard():
return render_template('dashboard.html', username=current_user.username)
@app.route('/achievements')
@login_required
def achievements():
user_achievements = Achievement.query.filter_by(user_id=current_user.id).all()
return render_template('achievements.html', achievements=user_achievements)
@app.route('/leaderboard')
@login_required
def leaderboard():
users = User.query.all()
return render_template('leaderboard.html', users=users)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
if __name__ == '__main__':
with app.app_context():
db.create_all() # Create database tables
app.run(debug=True , port=400)