|
|
|
from flask import render_template, flash, redirect, url_for, request
|
|
|
|
from flask_login import login_user, logout_user, current_user, login_required
|
|
|
|
from app import app, db
|
|
|
|
from app.models import Player, Game
|
|
|
|
from app.forms import LoginForm, RegistrationForm
|
|
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
@app.route('/index', methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def index():
|
|
|
|
message="Hello, World"
|
|
|
|
return render_template("index.html", title='Home', message=message)
|
|
|
|
|
|
|
|
@app.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
form = LoginForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
player = Player.query.filter_by(name=form.username.data).first()
|
|
|
|
if player is None or not player.check_password(form.password.data):
|
|
|
|
flash('Invalid username or password')
|
|
|
|
return redirect(url_for('login'))
|
|
|
|
login_user(player, remember=form.remember_me.data)
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
return render_template('login.html', title='Sign In', form=form)
|
|
|
|
|
|
|
|
@app.route('/logout')
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
|
|
|
|
@app.route('/register', methods=['GET', 'POST'])
|
|
|
|
def register():
|
|
|
|
if current_user.is_authenticated:
|
|
|
|
return redirect(url_for('index'))
|
|
|
|
form = RegistrationForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
player = Player(name=form.username.data)
|
|
|
|
player.set_password(form.password.data)
|
|
|
|
db.session.add(player)
|
|
|
|
db.session.commit()
|
|
|
|
flash('Congratulations, you are now a registered user!')
|
|
|
|
return redirect(url_for('login'))
|
|
|
|
return render_template('register.html', title='Register', form=form)
|