Skip to content

Instantly share code, notes, and snippets.

@mycahp
Created September 10, 2014 02:52
Show Gist options
  • Save mycahp/0c1c23fc2ade7c4fd6fb to your computer and use it in GitHub Desktop.
Save mycahp/0c1c23fc2ade7c4fd6fb to your computer and use it in GitHub Desktop.
from werkzeug.security import generate_password_hash, check_password_hash
from sql import UsersTable, session
from sqlalchemy import func
class NewUser(object):
'''Our user class, which holds all of the methods that will deal with
account related (not character) things '''
def __init__(self, username, password):
# Initialize our variables
self.username = username
# Hash our password using set_password method
self.password = password
self.set_password(password)
def set_password(self, password):
# Uses wekzeug.security to hash our password.
self.pw_hash = generate_password_hash(password)
# User creation method used in our creation form (currently on index)
def create_user(self, email):
self.email = email
# Look to see if the username has already been created.
# To-do: Also check the user database for duplicate e-mail address
userSearch = session.query(UsersTable).filter_by(username=self.username).first()
# If a match is found (if the counted rows is more than 1) return match = 1
# Control structure for this is in account_create route
if userSearch is True:
match = True
return match
else:
# If no match is found, we use add_user to create the user in the database
add_user = UsersTable(self.username, self.pw_hash, self.email)
# Session is from sql.py
session.add(add_user)
session.commit()
# Return false so that our control structure knows what to tell the user.
match = False
return match
class RegisteredUser(object):
def __init__(self, username, password):
self.username = username
self.password = password
self.find_stored_password(self.username)
def find_stored_password(self, username):
userSearch = session.query(UsersTable).with_entities(UsersTable.password).filter_by(username=self.username)
self.stored_password = userSearch
def check_password(self, stored_password, password):
return check_password_hash(self.stored_password, self.password)
def check_login(self):
userSearch = session.query(UsersTable).filter_by(username=self.username).first()
if userSearch:
password_check = self.check_password(self.stored_password, self.password)
if password_check is True:
login = 1
return login
else:
login = 0
return login
else:
login = 0 # No matching usernmaes
return login
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment