<s> import hashlib <EOL> import os <EOL> import sqlalchemy as sa <EOL> from . form import email_allowed <EOL> from . . models import db , User , OauthUser <EOL> def local_signup ( username , email , password ) : <EOL> if not email_allowed ( email ) : <EOL> return <EOL> try : <EOL> return db . session . query ( User ) . filter ( ( User . username == username ) | ( User . email == email ) ) . one ( ) <EOL> except sa . orm . exc . NoResultFound : <EOL> pass <EOL> user = User ( username = username , email = email , password = password ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> return user <EOL> def oauth_signup ( info , provider ) : <EOL> if not info [ "<STR_LIT:email>" ] or not email_allowed ( info [ "<STR_LIT:email>" ] ) : <EOL> return <EOL> oauth_user = db . session . query ( OauthUser ) . filter ( OauthUser . provider_id == str ( info [ "<STR_LIT:id>" ] ) ) . filter ( OauthUser . provider == provider ) . first ( ) <EOL> if oauth_user : <EOL> return oauth_user . user <EOL> user = db . session . query ( User ) . filter ( User . email == info [ "<STR_LIT:email>" ] ) . first ( ) <EOL> if not user : <EOL> random_pwd = hashlib . sha1 ( os . urandom ( <NUM_LIT:64> ) ) . hexdigest ( ) <EOL> user = User ( username = "<STR_LIT>" . format ( provider , info [ "<STR_LIT:username>" ] ) , <EOL> nickname = info [ "<STR_LIT:username>" ] , <EOL> email = info [ "<STR_LIT:email>" ] , <EOL> password = random_pwd ) <EOL> oauth_user = OauthUser ( <EOL> user = user , <EOL> provider_info = info , <EOL> provider_id = str ( info [ "<STR_LIT:id>" ] ) , <EOL> provider_email = info [ "<STR_LIT:email>" ] , <EOL> provider = provider ) <EOL> db . session . add ( oauth_user ) <EOL> db . session . commit ( ) <EOL> return user <EOL> else : <EOL> oauth_user = OauthUser ( <EOL> user = user , <EOL> provider_info = info , <EOL> provider_id = str ( info [ "<STR_LIT:id>" ] ) , <EOL> provider_email = info [ "<STR_LIT:email>" ] , <EOL> provider = provider ) <EOL> db . session . add ( oauth_user ) <EOL> db . session . commit ( ) <EOL> return user </s>
