Skip to content

Commit

Permalink
password hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Sep 3, 2014
1 parent 25e636a commit 86c839f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app.py
Expand Up @@ -3,9 +3,11 @@
url_for, request, session, flash
from functools import wraps
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.bcrypt import Bcrypt

# create the application object
# create the application object, pass it into Bcrypt for hashing
app = Flask(__name__)
bcrypt = Bcrypt(app)

# config
import os
Expand Down
9 changes: 9 additions & 0 deletions db_create_users.py
@@ -0,0 +1,9 @@
from app import db
from models import User

# insert data
db.session.add(User("michael", "michael@realpython.com", "i'll-never-tell"))
db.session.add(User("admin", "ad@min.com", "admin"))

# commit the changes
db.session.commit()
26 changes: 26 additions & 0 deletions migrations/versions/3e0014fa59ca_.py
@@ -0,0 +1,26 @@
"""empty message
Revision ID: 3e0014fa59ca
Revises: 4a5b39003781
Create Date: 2014-09-03 16:30:20.119025
"""

# revision identifiers, used by Alembic.
revision = '3e0014fa59ca'
down_revision = '4a5b39003781'

from alembic import op
import sqlalchemy as sa


def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'password')
### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('password', sa.VARCHAR(), autoincrement=False, nullable=False))
### end Alembic commands ###
26 changes: 26 additions & 0 deletions migrations/versions/c1b2d6a2aa6_.py
@@ -0,0 +1,26 @@
"""empty message
Revision ID: c1b2d6a2aa6
Revises: 3e0014fa59ca
Create Date: 2014-09-03 16:32:08.805429
"""

# revision identifiers, used by Alembic.
revision = 'c1b2d6a2aa6'
down_revision = '3e0014fa59ca'

from alembic import op
import sqlalchemy as sa


def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('users', sa.Column('password', sa.String(), nullable=True))
### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('users', 'password')
### end Alembic commands ###
9 changes: 4 additions & 5 deletions models.py
@@ -1,4 +1,4 @@
from app import db
from app import db, bcrypt

from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
Expand Down Expand Up @@ -28,14 +28,13 @@ class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
email = db.Column(db.String, nullable=False)
password = db.Column(db.String, nullable=False)
password = db.Column(db.String)
posts = relationship("BlogPost", backref="author")

def __init__(self, name, email, password, favorite_color):
def __init__(self, name, email, password):
self.name = name
self.email = email
self.password = password
self.favorite_color = favorite_color
self.password = bcrypt.generate_password_hash(password)

def __repr__(self):
return '<name {}'.format(self.name)

0 comments on commit 86c839f

Please sign in to comment.