-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #34
- Loading branch information
Showing
11 changed files
with
287 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from flask import Blueprint, render_template, flash, redirect, url_for, request | ||
from flask.ext.login import LoginManager, login_user, logout_user | ||
from flask.ext.wtf import Form | ||
from wtforms import TextField, PasswordField | ||
from wtforms.validators import Required, EqualTo | ||
from models import db, User | ||
import bcrypt | ||
from sqlalchemy.orm.exc import NoResultFound | ||
|
||
lm = LoginManager() | ||
|
||
auth = Blueprint('auth', __name__) | ||
|
||
|
||
def auth_user(login, password): | ||
try: | ||
user = db.session.query(User).filter(User.name == login).one() | ||
except NoResultFound: | ||
return None | ||
db_hash = user.password | ||
hashed = bcrypt.hashpw(password.encode('utf-8'), db_hash.encode('ascii')) | ||
if db_hash != hashed: | ||
return None | ||
return user | ||
|
||
|
||
class SignupForm(Form): | ||
""" | ||
Form used in signup | ||
""" | ||
username = TextField(validators=[Required()]) | ||
password = PasswordField( | ||
validators=[Required(), | ||
EqualTo('confirm', message='Passwords must match')]) | ||
confirm = PasswordField(validators=[Required()]) | ||
|
||
|
||
@auth.route('/signup', methods=['GET', 'POST']) | ||
def signup(): | ||
""" | ||
Sign up a new user | ||
""" | ||
form = SignupForm() | ||
if form.validate_on_submit(): | ||
username = form.username.data | ||
password = form.password.data | ||
user = User(username, password) | ||
db.session.add(user) | ||
db.session.commit() | ||
flash('User successfully created') | ||
return redirect(url_for('bp.home')) | ||
return render_template('signup.html', title='Sign up', form=form) | ||
|
||
|
||
class LoginForm(Form): | ||
""" | ||
Form used in login | ||
""" | ||
username = TextField(validators=[Required()]) | ||
password = PasswordField(validators=[Required()]) | ||
|
||
|
||
@auth.route('/login', methods=['GET', 'POST']) | ||
def login(): | ||
""" | ||
Log a user in | ||
""" | ||
form = LoginForm() | ||
if form.validate_on_submit(): | ||
username = form.username.data | ||
password = form.password.data | ||
user = auth_user(username, password) | ||
if user is None: | ||
flash('Bad login or password') | ||
return redirect(url_for('.login')) | ||
login_user(user) | ||
flash('Logged in') | ||
return redirect(request.args.get('next') or url_for('bp.home')) | ||
return render_template('login.html', title='Log in', form=form) | ||
|
||
|
||
@auth.route('/logout') | ||
def logout(): | ||
""" | ||
Log a user out | ||
""" | ||
logout_user() | ||
return redirect(url_for('bp.home')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Add User | ||
Revision ID: 4e32faff090b | ||
Revises: 1c27bdf60a4e | ||
Create Date: 2014-10-30 13:51:13.288615 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '4e32faff090b' | ||
down_revision = '1c27bdf60a4e' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('user', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('name', sa.String(), nullable=False), | ||
sa.Column('password', sa.String(), nullable=False), | ||
sa.Column('role', sa.SmallInteger(), nullable=False), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('name') | ||
) | ||
### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('user') | ||
### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
bcrypt | ||
coverage | ||
flask | ||
flask-admin | ||
flask-assets | ||
flask-login | ||
flask-migrate | ||
flask-script | ||
flask-sqlalchemy | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% if form.errors %} | ||
<ul class="errors"> | ||
{% for field_name, field_errors in form.errors|dictsort if field_errors %} | ||
{% for error in field_errors %} | ||
<li>{{ form[field_name].label }}: {{ error }}</li> | ||
{% endfor %} | ||
{% endfor %} | ||
</ul> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% extends "base.html" %} | ||
{% block content %} | ||
<form method="post"> | ||
{{form.hidden_tag()}} | ||
<p> <label> Username {{form.username()}} </label> </p> | ||
<p> <label> Password {{form.password()}} </label> </p> | ||
<p><input type="submit" value="Log in"></p> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{% extends "base.html" %} | ||
{% block content %} | ||
{% include "form_errors.html" %} | ||
<form method="post"> | ||
{{form.hidden_tag()}} | ||
<p> <label> Username {{form.username()}} </label> </p> | ||
<p> <label> Password {{form.password()}} </label> </p> | ||
<p> <label> Confirm password {{form.confirm()}} </label> </p> | ||
<p><input type="submit" value="Log in"></p> | ||
</form> | ||
{% endblock %} |