Last active
September 11, 2015 16:43
-
-
Save justanr/36fef66ea9e6d7b39c1e to your computer and use it in GitHub Desktop.
Simple user registration in three styles
This file contains hidden or 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
@expose('/register/', method=['POST'] | |
def create_new_user(): | |
form = NewUserForm() | |
username, password, email = form.data.username, form.data.password, form.data.email | |
if User.query.filter_by(User.username == username).first(): | |
return {'success': False, 'reason': 'Username in use already'} | |
elif User.query.filter(User.email == email).first(): | |
return {'success': False, 'reason': 'Email in use already'} | |
else: | |
password_hash = hash_password(password) | |
user = User(username, password_hash, email) | |
db.session.add(user) | |
db.session.commit() | |
return {'success': True, 'reason': None} |
This file contains hidden or 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
@expose('/register/', method=['POST']) | |
def register_user(): | |
success, reason = form_to_new_user(NewUserForm()) | |
return {'success': success, 'reason': reason} | |
def form_to_new_user(form): | |
username, password, email = form.data.username, form.data.password, form.data.email | |
return register_user(username, password, email) | |
def register_user(username, password, email): | |
all_users = User.query | |
def on_create(username, password, email): | |
password_hash = hash_password(password) | |
user = User(username, password_hash, email) | |
db.session.add(user) | |
db.session.commit() | |
created, reason = create_new_user(username, password, email, all_users, callback=on_create) | |
return created, reason | |
def create_new_user(username, password, email, all_users, callback=None): | |
if is_username_taken(username, all_users): | |
return False, "Username in use already" | |
elif is_email_taken(email, all_users): | |
return False, "Email in use already" | |
else: | |
callback(username, password, email) if callback else None | |
return True, '' | |
def is_username_taken(username, all_users): | |
return any(username == user.username for user in all_users) | |
def is_email_taken(email, all_users): | |
return any(email == user.email for user in all_users) |
This file contains hidden or 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
@expose('/register/', method=['POST']) | |
def register_user(): | |
success, reason = form_to_new_user(NewUserForm(), app.repositories.get('user')) | |
return {'success': success, 'reason': reason} | |
def form_to_new_user(form, user_repository): | |
return create_new_user(user_repository, username, password, email) | |
def create_new_user(user_repository, username, password, email): | |
if is_username_taken(username, user_repository): | |
return False, "Username is in use" | |
elif is_email_taken(email, user_repository): | |
return False, "Email is in use" | |
else: | |
password_hash = hash_password(password) | |
user = user_repository.create(username, password_hash, email) | |
user_repository.save(user) | |
return user, None | |
def is_username_taken(username, user_repository): | |
return user_repository.first(username=username) | |
def is_email_taken(email, user_repository): | |
return user_repository.first(email=email) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment