<s> import collections <EOL> import logging <EOL> from django . conf import settings <EOL> from django . contrib . auth import authenticate <EOL> from django . contrib . auth import forms as django_auth_forms <EOL> from django import forms <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . decorators . debug import sensitive_variables <EOL> from openstack_auth import exceptions <EOL> from openstack_auth import utils <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Login ( django_auth_forms . AuthenticationForm ) : <EOL> """<STR_LIT>""" <EOL> region = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> username = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . TextInput ( attrs = { "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> password = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Login , self ) . __init__ ( * args , ** kwargs ) <EOL> fields_ordering = [ '<STR_LIT:username>' , '<STR_LIT:password>' , '<STR_LIT>' ] <EOL> if getattr ( settings , <EOL> '<STR_LIT>' , <EOL> False ) : <EOL> last_domain = self . request . COOKIES . get ( '<STR_LIT>' , None ) <EOL> self . fields [ '<STR_LIT>' ] = forms . CharField ( <EOL> initial = last_domain , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = True , <EOL> widget = forms . TextInput ( attrs = { "<STR_LIT>" : "<STR_LIT>" } ) ) <EOL> self . fields [ '<STR_LIT:username>' ] . widget = forms . widgets . TextInput ( ) <EOL> fields_ordering = [ '<STR_LIT>' , '<STR_LIT:username>' , '<STR_LIT:password>' , '<STR_LIT>' ] <EOL> self . fields [ '<STR_LIT>' ] . choices = self . get_region_choices ( ) <EOL> if len ( self . fields [ '<STR_LIT>' ] . choices ) == <NUM_LIT:1> : <EOL> self . fields [ '<STR_LIT>' ] . initial = self . fields [ '<STR_LIT>' ] . choices [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . widgets . HiddenInput ( ) <EOL> elif len ( self . fields [ '<STR_LIT>' ] . choices ) > <NUM_LIT:1> : <EOL> self . fields [ '<STR_LIT>' ] . initial = self . request . COOKIES . get ( <EOL> '<STR_LIT>' ) <EOL> if utils . is_websso_enabled ( ) : <EOL> initial = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> choices = getattr ( settings , '<STR_LIT>' , ( ) ) , <EOL> required = False , <EOL> initial = initial ) <EOL> fields_ordering . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> elif getattr ( settings , '<STR_LIT>' , False ) : <EOL> msg = ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> LOG . warning ( msg ) <EOL> self . fields = collections . OrderedDict ( <EOL> ( key , self . fields [ key ] ) for key in fields_ordering ) <EOL> @ staticmethod <EOL> def get_region_choices ( ) : <EOL> default_region = ( settings . OPENSTACK_KEYSTONE_URL , "<STR_LIT>" ) <EOL> regions = getattr ( settings , '<STR_LIT>' , [ ] ) <EOL> if not regions : <EOL> regions = [ default_region ] <EOL> return regions <EOL> @ sensitive_variables ( ) <EOL> def clean ( self ) : <EOL> default_domain = getattr ( settings , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> username = self . cleaned_data . get ( '<STR_LIT:username>' ) <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' ) <EOL> region = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> domain = self . cleaned_data . get ( '<STR_LIT>' , default_domain ) <EOL> if not ( username and password ) : <EOL> return self . cleaned_data <EOL> try : <EOL> self . user_cache = authenticate ( request = self . request , <EOL> username = username , <EOL> password = password , <EOL> user_domain_name = domain , <EOL> auth_url = region ) <EOL> msg = '<STR_LIT>' % { '<STR_LIT:username>' : username } <EOL> LOG . info ( msg ) <EOL> except exceptions . KeystoneAuthException as exc : <EOL> msg = '<STR_LIT>' % { '<STR_LIT:username>' : username } <EOL> LOG . warning ( msg ) <EOL> raise forms . ValidationError ( exc ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . check_for_test_cookie ( ) <EOL> return self . cleaned_data </s>
