<s> """<STR_LIT>""" <EOL> import logging <EOL> from django import http <EOL> from django import shortcuts <EOL> from django import template <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . utils import simplejson <EOL> from django . utils . translation import ugettext as _ <EOL> from django_openstack import forms <EOL> from django_openstack import api <EOL> from django_openstack . dash . views . ports import DeletePort <EOL> from django_openstack . dash . views . ports import DetachPort <EOL> from django_openstack . dash . views . ports import TogglePort <EOL> import warnings <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> class CreateNetwork ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( required = True , label = "<STR_LIT>" ) <EOL> def handle ( self , request , data ) : <EOL> network_name = data [ '<STR_LIT:name>' ] <EOL> try : <EOL> LOG . info ( '<STR_LIT>' % network_name ) <EOL> send_data = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT:%s>' % network_name } } <EOL> api . quantum_create_network ( request , send_data ) <EOL> except Exception , e : <EOL> messages . error ( request , <EOL> '<STR_LIT>' % <EOL> ( network_name , e . message , ) ) <EOL> return shortcuts . redirect ( request . build_absolute_uri ( ) ) <EOL> else : <EOL> msg = '<STR_LIT>' % network_name <EOL> LOG . info ( msg ) <EOL> messages . success ( request , msg ) <EOL> return shortcuts . redirect ( '<STR_LIT>' , <EOL> tenant_id = request . user . tenant_id ) <EOL> class DeleteNetwork ( forms . SelfHandlingForm ) : <EOL> network = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> def handle ( self , request , data ) : <EOL> try : <EOL> LOG . info ( '<STR_LIT>' % data [ '<STR_LIT>' ] ) <EOL> api . quantum_delete_network ( request , data [ '<STR_LIT>' ] ) <EOL> except Exception , e : <EOL> messages . error ( request , <EOL> '<STR_LIT>' % <EOL> ( data [ '<STR_LIT>' ] , e . message , ) ) <EOL> else : <EOL> msg = '<STR_LIT>' % data [ '<STR_LIT>' ] <EOL> LOG . info ( msg ) <EOL> messages . success ( request , msg ) <EOL> return shortcuts . redirect ( request . build_absolute_uri ( ) ) <EOL> class RenameNetwork ( forms . SelfHandlingForm ) : <EOL> network = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> new_name = forms . CharField ( required = True ) <EOL> def handle ( self , request , data ) : <EOL> try : <EOL> LOG . info ( '<STR_LIT>' % <EOL> ( data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] ) ) <EOL> send_data = { '<STR_LIT>' : { '<STR_LIT:name>' : '<STR_LIT:%s>' % data [ '<STR_LIT>' ] } } <EOL> api . quantum_update_network ( request , data [ '<STR_LIT>' ] , send_data ) <EOL> except Exception , e : <EOL> messages . error ( request , <EOL> '<STR_LIT>' % <EOL> ( data [ '<STR_LIT>' ] , e . message , ) ) <EOL> else : <EOL> msg = '<STR_LIT>' % ( data [ '<STR_LIT>' ] , data [ '<STR_LIT>' ] ) <EOL> LOG . info ( msg ) <EOL> messages . success ( request , msg ) <EOL> return shortcuts . redirect ( request . build_absolute_uri ( ) ) <EOL> @ login_required <EOL> def index ( request , tenant_id ) : <EOL> delete_form , delete_handled = DeleteNetwork . maybe_handle ( request ) <EOL> networks = [ ] <EOL> instances = [ ] <EOL> try : <EOL> networks_list = api . quantum_list_networks ( request ) <EOL> details = [ ] <EOL> for network in networks_list [ '<STR_LIT>' ] : <EOL> net_stats = _calc_network_stats ( request , tenant_id , network [ '<STR_LIT:id>' ] ) <EOL> details = api . quantum_network_details ( request , network [ '<STR_LIT:id>' ] ) <EOL> networks . append ( { <EOL> '<STR_LIT:name>' : details [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT:id>' : network [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : net_stats [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : net_stats [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : net_stats [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : tenant_id <EOL> } ) <EOL> except Exception , e : <EOL> messages . error ( request , '<STR_LIT>' % e . message ) <EOL> return shortcuts . render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : networks , <EOL> '<STR_LIT>' : delete_form , <EOL> } , context_instance = template . RequestContext ( request ) ) <EOL> @ login_required <EOL> def create ( request , tenant_id ) : <EOL> network_form , handled = CreateNetwork . maybe_handle ( request ) <EOL> if handled : <EOL> return shortcuts . redirect ( '<STR_LIT>' , request . user . tenant_id ) <EOL> return shortcuts . render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : network_form <EOL> } , context_instance = template . RequestContext ( request ) ) <EOL> @ login_required <EOL> def detail ( request , tenant_id , network_id ) : <EOL> delete_port_form , delete_handled = DeletePort . maybe_handle ( request ) <EOL> detach_port_form , detach_handled = DetachPort . maybe_handle ( request ) <EOL> toggle_port_form , port_toggle_handled = TogglePort . maybe_handle ( request ) <EOL> network = { } <EOL> try : <EOL> network_details = api . quantum_network_details ( request , network_id ) <EOL> network [ '<STR_LIT:name>' ] = network_details [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] <EOL> network [ '<STR_LIT:id>' ] = network_id <EOL> network [ '<STR_LIT>' ] = _get_port_states ( request , tenant_id , network_id ) <EOL> except Exception , e : <EOL> messages . error ( request , '<STR_LIT>' % e . message ) <EOL> return shortcuts . render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : network , <EOL> '<STR_LIT>' : tenant_id , <EOL> '<STR_LIT>' : delete_port_form , <EOL> '<STR_LIT>' : detach_port_form , <EOL> '<STR_LIT>' : toggle_port_form <EOL> } , context_instance = template . RequestContext ( request ) ) <EOL> @ login_required <EOL> def rename ( request , tenant_id , network_id ) : <EOL> rename_form , handled = RenameNetwork . maybe_handle ( request ) <EOL> network_details = api . quantum_network_details ( request , network_id ) <EOL> if handled : <EOL> return shortcuts . redirect ( '<STR_LIT>' , request . user . tenant_id ) <EOL> return shortcuts . render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : network_details , <EOL> '<STR_LIT>' : rename_form <EOL> } , context_instance = template . RequestContext ( request ) ) <EOL> def _get_port_states ( request , tenant_id , network_id ) : <EOL> """<STR_LIT>""" <EOL> network_ports = [ ] <EOL> vifs = api . get_vif_ids ( request ) <EOL> ports = api . quantum_list_ports ( request , network_id ) <EOL> for port in ports [ '<STR_LIT>' ] : <EOL> port_details = api . quantum_port_details ( request , <EOL> network_id , port [ '<STR_LIT:id>' ] ) <EOL> port_attachment = api . quantum_port_attachment ( request , <EOL> network_id , port [ '<STR_LIT:id>' ] ) <EOL> connected_instance = None <EOL> if port_attachment [ '<STR_LIT>' ] : <EOL> for vif in vifs : <EOL> if str ( vif [ '<STR_LIT:id>' ] ) == str ( port_attachment [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) : <EOL> connected_instance = vif [ '<STR_LIT>' ] <EOL> break <EOL> network_ports . append ( { <EOL> '<STR_LIT:id>' : port_details [ '<STR_LIT:port>' ] [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:state>' : port_details [ '<STR_LIT:port>' ] [ '<STR_LIT:state>' ] , <EOL> '<STR_LIT>' : port_attachment [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : connected_instance <EOL> } ) <EOL> return network_ports <EOL> def _calc_network_stats ( request , tenant_id , network_id ) : <EOL> """<STR_LIT>""" <EOL> total = <NUM_LIT:0> <EOL> available = <NUM_LIT:0> <EOL> used = <NUM_LIT:0> <EOL> ports = api . quantum_list_ports ( request , network_id ) <EOL> for port in ports [ '<STR_LIT>' ] : <EOL> total += <NUM_LIT:1> <EOL> port_attachment = api . quantum_port_attachment ( request , <EOL> network_id , port [ '<STR_LIT:id>' ] ) <EOL> if port_attachment [ '<STR_LIT>' ] : <EOL> used += <NUM_LIT:1> <EOL> else : <EOL> available += <NUM_LIT:1> <EOL> return { '<STR_LIT>' : total , '<STR_LIT>' : used , '<STR_LIT>' : available } </s>
