<s> from __future__ import absolute_import , print_function <EOL> from django import forms <EOL> from sentry . auth . view import AuthView , ConfigureView <EOL> from sentry . models import AuthIdentity <EOL> from . client import GitHubClient <EOL> from . constants import ERR_NO_ORG_ACCESS <EOL> from . constants import REQUIRE_VERIFIED_EMAIL <EOL> from . constants import ( <EOL> ERR_NO_SINGLE_VERIFIED_PRIMARY_EMAIL , ERR_NO_SINGLE_PRIMARY_EMAIL , <EOL> ERR_NO_VERIFIED_PRIMARY_EMAIL , ERR_NO_PRIMARY_EMAIL , <EOL> ) <EOL> def _get_name_from_email ( email ) : <EOL> """<STR_LIT>""" <EOL> name = email . rsplit ( '<STR_LIT:@>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> name = '<STR_LIT:U+0020>' . join ( [ n_part . capitalize ( ) for n_part in name . split ( '<STR_LIT:.>' ) ] ) <EOL> return name <EOL> class FetchUser ( AuthView ) : <EOL> def __init__ ( self , client_id , client_secret , org = None , * args , ** kwargs ) : <EOL> self . org = org <EOL> self . client = GitHubClient ( client_id , client_secret ) <EOL> super ( FetchUser , self ) . __init__ ( * args , ** kwargs ) <EOL> def handle ( self , request , helper ) : <EOL> access_token = helper . fetch_state ( '<STR_LIT:data>' ) [ '<STR_LIT>' ] <EOL> if self . org is not None : <EOL> if not self . client . is_org_member ( access_token , self . org [ '<STR_LIT:id>' ] ) : <EOL> return helper . error ( ERR_NO_ORG_ACCESS ) <EOL> user = self . client . get_user ( access_token ) <EOL> if not user . get ( '<STR_LIT:email>' ) : <EOL> emails = self . client . get_user_emails ( access_token ) <EOL> email = [ <EOL> e [ '<STR_LIT:email>' ] for e in emails <EOL> if ( ( not REQUIRE_VERIFIED_EMAIL ) | e [ '<STR_LIT>' ] ) <EOL> and e [ '<STR_LIT>' ] <EOL> ] <EOL> if len ( email ) == <NUM_LIT:0> : <EOL> if REQUIRE_VERIFIED_EMAIL : <EOL> msg = ERR_NO_VERIFIED_PRIMARY_EMAIL <EOL> else : <EOL> msg = ERR_NO_PRIMARY_EMAIL <EOL> return helper . error ( msg ) <EOL> elif len ( email ) > <NUM_LIT:1> : <EOL> if REQUIRE_VERIFIED_EMAIL : <EOL> msg = ERR_NO_SINGLE_VERIFIED_PRIMARY_EMAIL <EOL> else : <EOL> msg = ERR_NO_SINGLE_PRIMARY_EMAIL <EOL> return helper . error ( msg ) <EOL> else : <EOL> user [ '<STR_LIT:email>' ] = email [ <NUM_LIT:0> ] <EOL> if not user . get ( '<STR_LIT:name>' ) : <EOL> user [ '<STR_LIT:name>' ] = _get_name_from_email ( user [ '<STR_LIT:email>' ] ) <EOL> helper . bind_state ( '<STR_LIT:user>' , user ) <EOL> return helper . next_step ( ) <EOL> class ConfirmEmailForm ( forms . Form ) : <EOL> email = forms . EmailField ( label = '<STR_LIT>' ) <EOL> class ConfirmEmail ( AuthView ) : <EOL> def handle ( self , request , helper ) : <EOL> user = helper . fetch_state ( '<STR_LIT:user>' ) <EOL> try : <EOL> auth_identity = AuthIdentity . objects . select_related ( '<STR_LIT:user>' ) . get ( <EOL> auth_provider = helper . auth_provider , <EOL> ident = user [ '<STR_LIT:id>' ] , <EOL> ) <EOL> except AuthIdentity . DoesNotExist : <EOL> pass <EOL> else : <EOL> user [ '<STR_LIT:email>' ] = auth_identity . user . email <EOL> if user . get ( '<STR_LIT:email>' ) : <EOL> return helper . next_step ( ) <EOL> form = ConfirmEmailForm ( request . POST or None ) <EOL> if form . is_valid ( ) : <EOL> user [ '<STR_LIT:email>' ] = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> helper . bind_state ( '<STR_LIT:user>' , user ) <EOL> return helper . next_step ( ) <EOL> return self . respond ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> class SelectOrganizationForm ( forms . Form ) : <EOL> org = forms . ChoiceField ( label = '<STR_LIT>' ) <EOL> def __init__ ( self , org_list , * args , ** kwargs ) : <EOL> super ( SelectOrganizationForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . choices = [ <EOL> ( o [ '<STR_LIT:id>' ] , o [ '<STR_LIT>' ] ) for o in org_list <EOL> ] <EOL> self . fields [ '<STR_LIT>' ] . widget . choices = self . fields [ '<STR_LIT>' ] . choices <EOL> class SelectOrganization ( AuthView ) : <EOL> def __init__ ( self , client_id , client_secret , * args , ** kwargs ) : <EOL> self . client = GitHubClient ( client_id , client_secret ) <EOL> super ( SelectOrganization , self ) . __init__ ( * args , ** kwargs ) <EOL> def handle ( self , request , helper ) : <EOL> access_token = helper . fetch_state ( '<STR_LIT:data>' ) [ '<STR_LIT>' ] <EOL> org_list = self . client . get_org_list ( access_token ) <EOL> form = SelectOrganizationForm ( org_list , request . POST or None ) <EOL> if form . is_valid ( ) : <EOL> org_id = form . cleaned_data [ '<STR_LIT>' ] <EOL> org = [ o for o in org_list if org_id == str ( o [ '<STR_LIT:id>' ] ) ] [ <NUM_LIT:0> ] <EOL> helper . bind_state ( '<STR_LIT>' , org ) <EOL> return helper . next_step ( ) <EOL> return self . respond ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : org_list , <EOL> } ) <EOL> class GitHubConfigureView ( ConfigureView ) : <EOL> def dispatch ( self , request , organization , auth_provider ) : <EOL> return self . render ( '<STR_LIT>' ) </s>
