<s> import logging <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from horizon import forms <EOL> from horizon import messages <EOL> from openstack_dashboard import api <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class AddDHCPAgent ( forms . SelfHandlingForm ) : <EOL> network_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> network_name = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . TextInput ( <EOL> attrs = { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> agent = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( AddDHCPAgent , self ) . __init__ ( request , * args , ** kwargs ) <EOL> initial = kwargs . get ( '<STR_LIT>' , { } ) <EOL> self . fields [ '<STR_LIT>' ] . choices = self . _populate_agent_choices ( request , <EOL> initial ) <EOL> def _populate_agent_choices ( self , request , initial ) : <EOL> network_id = initial . get ( '<STR_LIT>' ) <EOL> agents = initial . get ( '<STR_LIT>' ) <EOL> try : <EOL> exist_agents = [ agent . id for agent in <EOL> api . neutron . list_dhcp_agent_hosting_networks ( <EOL> request , network_id ) ] <EOL> agent_list = [ ( agent . id , agent . host ) for agent in agents <EOL> if agent . id not in exist_agents ] <EOL> if agent_list : <EOL> agent_list . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> else : <EOL> agent_list . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> return agent_list <EOL> except Exception : <EOL> redirect = reverse ( '<STR_LIT>' , <EOL> args = ( network_id , ) ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> exceptions . handle ( request , msg , redirect = redirect ) <EOL> def handle ( self , request , data ) : <EOL> agent_name = data [ '<STR_LIT>' ] <EOL> for choice in self . fields [ '<STR_LIT>' ] . choices : <EOL> if choice [ <NUM_LIT:0> ] == data [ '<STR_LIT>' ] : <EOL> agent_name = choice [ <NUM_LIT:1> ] <EOL> try : <EOL> api . neutron . add_network_to_dhcp_agent ( request , data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] ) <EOL> msg = _ ( '<STR_LIT>' ) % agent_name <EOL> messages . success ( request , msg ) <EOL> return True <EOL> except Exception : <EOL> redirect = reverse ( '<STR_LIT>' , <EOL> args = ( data [ '<STR_LIT>' ] , ) ) <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % { '<STR_LIT>' : agent_name , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] } <EOL> exceptions . handle ( request , msg , redirect = redirect ) </s>
