<s> """<STR_LIT>""" <EOL> import logging <EOL> from django import template <EOL> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . shortcuts import redirect , render_to_response <EOL> from django . utils . text import normalize_newlines <EOL> from django . utils . translation import ugettext as _ <EOL> from django_openstack import api <EOL> from django_openstack import forms <EOL> from openstackx . api import exceptions as api_exceptions <EOL> from glance . common import exception as glance_exception <EOL> from novaclient import exceptions as novaclient_exceptions <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> class UpdateImageForm ( forms . SelfHandlingForm ) : <EOL> image_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> name = forms . CharField ( max_length = "<STR_LIT>" , label = "<STR_LIT:Name>" ) <EOL> kernel = forms . CharField ( max_length = "<STR_LIT>" , label = "<STR_LIT>" , <EOL> required = False ) <EOL> ramdisk = forms . CharField ( max_length = "<STR_LIT>" , label = "<STR_LIT>" , <EOL> required = False ) <EOL> architecture = forms . CharField ( label = "<STR_LIT>" , required = False ) <EOL> container_format = forms . CharField ( label = "<STR_LIT>" , <EOL> required = False ) <EOL> disk_format = forms . CharField ( label = "<STR_LIT>" ) <EOL> def handle ( self , request , data ) : <EOL> image_id = data [ '<STR_LIT>' ] <EOL> tenant_id = request . user . tenant_id <EOL> error_retrieving = _ ( '<STR_LIT>' % image_id ) <EOL> error_updating = _ ( '<STR_LIT>' % image_id ) <EOL> try : <EOL> image = api . image_get_meta ( request , image_id ) <EOL> except glance_exception . ClientConnectionError , e : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) ) <EOL> messages . error ( request , error_retrieving ) <EOL> except glance_exception . Error , e : <EOL> LOG . exception ( error_retrieving ) <EOL> messages . error ( request , error_retrieving ) <EOL> if image . owner == request . user . tenant_id : <EOL> try : <EOL> meta = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT:name>' : data [ '<STR_LIT:name>' ] , <EOL> } <EOL> meta [ '<STR_LIT>' ] = { } <EOL> if data [ '<STR_LIT>' ] : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> if data [ '<STR_LIT>' ] : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> if data [ '<STR_LIT>' ] : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> api . image_update ( request , image_id , meta ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> except glance_exception . ClientConnectionError , e : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) ) <EOL> messages . error ( request , error_retrieving ) <EOL> except glance_exception . Error , e : <EOL> LOG . exception ( error_updating ) <EOL> messages . error ( request , error_updating ) <EOL> except : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) ) <EOL> messages . error ( request , error_updating ) <EOL> return redirect ( '<STR_LIT>' , tenant_id , image_id ) <EOL> else : <EOL> messages . info ( request , _ ( '<STR_LIT>' ) ) <EOL> return redirect ( '<STR_LIT>' , tenant_id , image_id ) <EOL> class LaunchForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT> , label = "<STR_LIT>" ) <EOL> image_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> tenant_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> user_data = forms . CharField ( widget = forms . Textarea , <EOL> label = "<STR_LIT>" , <EOL> required = False ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( LaunchForm , self ) . __init__ ( * args , ** kwargs ) <EOL> flavorlist = kwargs . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( <EOL> choices = flavorlist , <EOL> label = "<STR_LIT>" , <EOL> help_text = "<STR_LIT>" ) <EOL> keynamelist = kwargs . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( choices = keynamelist , <EOL> label = "<STR_LIT>" , <EOL> required = False , <EOL> help_text = "<STR_LIT>" ) <EOL> securitygrouplist = kwargs . get ( '<STR_LIT>' , { } ) . get ( <EOL> '<STR_LIT>' , [ ] ) <EOL> self . fields [ '<STR_LIT>' ] = forms . MultipleChoiceField ( <EOL> choices = securitygrouplist , <EOL> label = '<STR_LIT>' , <EOL> required = True , <EOL> initial = [ '<STR_LIT:default>' ] , <EOL> widget = forms . SelectMultiple ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" } ) , <EOL> help_text = "<STR_LIT>" ) <EOL> field_list = ( <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> for field in field_list [ : : - <NUM_LIT:1> ] : <EOL> self . fields . insert ( <NUM_LIT:0> , field , self . fields . pop ( field ) ) <EOL> def handle ( self , request , data ) : <EOL> image_id = data [ '<STR_LIT>' ] <EOL> tenant_id = data [ '<STR_LIT>' ] <EOL> try : <EOL> image = api . image_get_meta ( request , image_id ) <EOL> flavor = api . flavor_get ( request , data [ '<STR_LIT>' ] ) <EOL> api . server_create ( request , <EOL> data [ '<STR_LIT:name>' ] , <EOL> image , <EOL> flavor , <EOL> data . get ( '<STR_LIT>' ) , <EOL> normalize_newlines ( data . get ( '<STR_LIT>' ) ) , <EOL> data . get ( '<STR_LIT>' ) ) <EOL> msg = '<STR_LIT>' <EOL> LOG . info ( msg ) <EOL> messages . success ( request , msg ) <EOL> return redirect ( '<STR_LIT>' , tenant_id ) <EOL> except api_exceptions . ApiException , e : <EOL> LOG . exception ( '<STR_LIT>' <EOL> % image_id ) <EOL> messages . error ( request , <EOL> '<STR_LIT>' % e . message ) <EOL> class DeleteImage ( forms . SelfHandlingForm ) : <EOL> image_id = forms . CharField ( required = True ) <EOL> def handle ( self , request , data ) : <EOL> image_id = data [ '<STR_LIT>' ] <EOL> tenant_id = request . user . tenant_id <EOL> try : <EOL> image = api . image_get_meta ( request , image_id ) <EOL> if image . owner == request . user . tenant_id : <EOL> api . image_delete ( request , image_id ) <EOL> else : <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> return redirect ( '<STR_LIT>' , tenant_id , image_id ) <EOL> except glance_exception . ClientConnectionError , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> messages . error ( request , "<STR_LIT>" <EOL> % e . message ) <EOL> except glance_exception . Error , e : <EOL> LOG . exception ( '<STR_LIT>' % image_id ) <EOL> messages . error ( request , "<STR_LIT>" <EOL> % ( image_id , e . message ) ) <EOL> return redirect ( request . build_absolute_uri ( ) ) <EOL> @ login_required <EOL> def index ( request , tenant_id ) : <EOL> for f in ( DeleteImage , ) : <EOL> unused , handled = f . maybe_handle ( request ) <EOL> if handled : <EOL> return handled <EOL> delete_form = DeleteImage ( ) <EOL> tenant = { } <EOL> try : <EOL> tenant = api . token_get_tenant ( request , request . user . tenant_id ) <EOL> except api_exceptions . ApiException , e : <EOL> messages . error ( request , "<STR_LIT>" % e . message ) <EOL> all_images = [ ] <EOL> try : <EOL> all_images = api . image_list_detailed ( request ) <EOL> if not all_images : <EOL> messages . info ( request , "<STR_LIT>" ) <EOL> except glance_exception . ClientConnectionError , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> messages . error ( request , "<STR_LIT>" % str ( e ) ) <EOL> except glance_exception . Error , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> messages . error ( request , "<STR_LIT>" % str ( e ) ) <EOL> except api_exceptions . ApiException , e : <EOL> msg = "<STR_LIT>" % str ( e ) <EOL> LOG . exception ( msg ) <EOL> messages . error ( request , msg ) <EOL> images = [ im for im in all_images <EOL> if im [ '<STR_LIT>' ] not in [ '<STR_LIT>' , '<STR_LIT>' ] ] <EOL> return render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : delete_form , <EOL> '<STR_LIT>' : tenant , <EOL> '<STR_LIT>' : images , <EOL> } , context_instance = template . RequestContext ( request ) ) <EOL> @ login_required <EOL> def launch ( request , tenant_id , image_id ) : <EOL> def flavorlist ( ) : <EOL> try : <EOL> fl = api . flavor_list ( request ) <EOL> sel = [ ( f . id , '<STR_LIT>' % <EOL> ( f . name , f . vcpus , f . disk , f . ram ) ) for f in fl ] <EOL> return sorted ( sel ) <EOL> except api_exceptions . ApiException : <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> return [ ( <NUM_LIT:1> , '<STR_LIT>' ) ] <EOL> def keynamelist ( ) : <EOL> try : <EOL> fl = api . keypair_list ( request ) <EOL> sel = [ ( f . name , f . name ) for f in fl ] <EOL> return sel <EOL> except api_exceptions . ApiException : <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> return [ ] <EOL> def securitygrouplist ( ) : <EOL> try : <EOL> fl = api . security_group_list ( request ) <EOL> sel = [ ( f . name , f . name ) for f in fl ] <EOL> return sel <EOL> except novaclient_exceptions . ClientException , e : <EOL> LOG . exception ( '<STR_LIT>' ) <EOL> return [ ] <EOL> image = api . image_get_meta ( request , image_id ) <EOL> tenant = api . token_get_tenant ( request , request . user . tenant_id ) <EOL> quotas = api . tenant_quota_get ( request , request . user . tenant_id ) <EOL> try : <EOL> quotas . ram = int ( quotas . ram ) / <NUM_LIT:100> <EOL> except Exception , e : <EOL> messages . error ( request , '<STR_LIT>' % <EOL> ( image_id , e . message ) ) <EOL> return redirect ( '<STR_LIT>' , tenant_id ) <EOL> form , handled = LaunchForm . maybe_handle ( <EOL> request , initial = { '<STR_LIT>' : flavorlist ( ) , <EOL> '<STR_LIT>' : keynamelist ( ) , <EOL> '<STR_LIT>' : securitygrouplist ( ) , <EOL> '<STR_LIT>' : image_id , <EOL> '<STR_LIT>' : tenant_id } ) <EOL> if handled : <EOL> return handled <EOL> return render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : tenant , <EOL> '<STR_LIT:image>' : image , <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : quotas , <EOL> } , context_instance = template . RequestContext ( request ) ) <EOL> @ login_required <EOL> def update ( request , tenant_id , image_id ) : <EOL> try : <EOL> image = api . image_get_meta ( request , image_id ) <EOL> except glance_exception . ClientConnectionError , e : <EOL> LOG . exception ( "<STR_LIT>" ) <EOL> messages . error ( request , "<STR_LIT>" <EOL> % e . message ) <EOL> except glance_exception . Error , e : <EOL> LOG . exception ( '<STR_LIT>' % image_id ) <EOL> messages . error ( request , "<STR_LIT>" <EOL> % ( image_id , e . message ) ) <EOL> form , handled = UpdateImageForm ( ) . maybe_handle ( request , initial = { <EOL> '<STR_LIT>' : image_id , <EOL> '<STR_LIT:name>' : image . get ( '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : image [ '<STR_LIT>' ] . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : image [ '<STR_LIT>' ] . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : image [ '<STR_LIT>' ] . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : image . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : image . get ( '<STR_LIT>' , '<STR_LIT>' ) , } ) <EOL> if handled : <EOL> return handled <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } , context_instance = template . RequestContext ( request ) ) </s>
