Update login.py

This commit is contained in:
Logan Hunt 2018-03-27 09:28:16 -06:00 committed by GitHub
parent 1d0418d9eb
commit a6567c0ecb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,40 +1,34 @@
from flask import Flask <link rel="stylesheet" href="/static/style.css" type="text/css">
from flask import Flask, flash, redirect, render_template, request, session, abort {% block body %}
import os {% if session['logged_in'] %}
from sqlalchemy.orm import sessionmaker <p>You're logged in already!</p>
from tabledef import * {% else %}
engine = create_engine('sqlite:///tutorial.db', echo=True)
app = Flask(__name__) <form action="/login" method="POST">
<div class="login">
@app.route('/') <div class="login-screen">
def home(): <div class="app-title">
if not session.get('logged_in'): <h1>Login</h1>
return render_template('login.html') </div>
else:
return "Hello Boss! <a href='/logout'>Logout</a>" <div class="login-form">
<div class="control-group">
@app.route('/login', methods=['POST']) <input type="text" class="login-field" value="" placeholder="username" name="username">
def do_admin_login(): <label class="login-field-icon fui-user" for="login-name"></label>
</div>
POST_USERNAME = str(request.form['username'])
POST_PASSWORD = str(request.form['password']) <div class="control-group">
<input type="password" class="login-field" value="" placeholder="password" name="password">
Session = sessionmaker(bind=engine) <label class="login-field-icon fui-lock" for="login-pass"></label>
s = Session() </div>
query = s.query(User).filter(User.username.in_([POST_USERNAME]), User.password.in_([POST_PASSWORD]) )
result = query.first() <input type="submit" value="Log in" class="btn btn-primary btn-large btn-block" >
if result: <br>
session['logged_in'] = True </div>
else: </div>
flash('wrong password!') </div>
return home() </form>
@app.route("/logout") {% endif %}
def logout(): {% endblock %}
session['logged_in'] = False
return home()
if __name__ == "__main__":
app.secret_key = os.urandom(12)
app.run(debug=True,host='0.0.0.0', port=4000)