<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . core import validators <EOL> from django . forms import ValidationError <EOL> from django . forms . widgets import HiddenInput <EOL> from django . template import defaultfilters <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> from openstack_dashboard import policy <EOL> IMAGE_BACKEND_SETTINGS = getattr ( settings , '<STR_LIT>' , { } ) <EOL> IMAGE_FORMAT_CHOICES = IMAGE_BACKEND_SETTINGS . get ( '<STR_LIT>' , [ ] ) <EOL> class ImageURLField ( forms . URLField ) : <EOL> default_validators = [ validators . URLValidator ( schemes = [ "<STR_LIT:http>" , "<STR_LIT>" ] ) ] <EOL> def create_image_metadata ( data ) : <EOL> """<STR_LIT>""" <EOL> disk_format = data [ '<STR_LIT>' ] <EOL> if disk_format in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) : <EOL> container_format = disk_format <EOL> elif disk_format == '<STR_LIT>' : <EOL> disk_format = '<STR_LIT>' <EOL> container_format = '<STR_LIT>' <EOL> else : <EOL> container_format = '<STR_LIT>' <EOL> meta = { '<STR_LIT>' : data . get ( '<STR_LIT>' , data . get ( '<STR_LIT>' , False ) ) , <EOL> '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : disk_format , <EOL> '<STR_LIT>' : container_format , <EOL> '<STR_LIT>' : ( data [ '<STR_LIT>' ] or <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : ( data [ '<STR_LIT>' ] or <NUM_LIT:0> ) , <EOL> '<STR_LIT:name>' : data [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : { } } <EOL> if '<STR_LIT:description>' in data : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT:description>' ] = data [ '<STR_LIT:description>' ] <EOL> if data . get ( '<STR_LIT>' ) : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> if data . get ( '<STR_LIT>' ) : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> if data . get ( '<STR_LIT>' ) : <EOL> meta [ '<STR_LIT>' ] [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> return meta <EOL> class CreateImageForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT:255> , label = _ ( "<STR_LIT:Name>" ) ) <EOL> description = forms . CharField ( <EOL> max_length = <NUM_LIT:255> , <EOL> widget = forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT:4> } ) , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> source_type = forms . ChoiceField ( <EOL> label = _ ( '<STR_LIT>' ) , <EOL> required = False , <EOL> choices = [ ( '<STR_LIT:url>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:file>' , _ ( '<STR_LIT>' ) ) ] , <EOL> widget = forms . Select ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' } ) ) <EOL> image_url_attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> image_url = ImageURLField ( label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> widget = forms . TextInput ( attrs = image_url_attrs ) , <EOL> required = False ) <EOL> image_attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None <EOL> } <EOL> image_file = forms . FileField ( label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" ) , <EOL> widget = forms . FileInput ( attrs = image_attrs ) , <EOL> required = False ) <EOL> kernel = forms . ChoiceField ( <EOL> label = _ ( '<STR_LIT>' ) , <EOL> required = False , <EOL> widget = forms . SelectWidget ( <EOL> transform = lambda x : "<STR_LIT>" % ( <EOL> x . name , defaultfilters . filesizeformat ( x . size ) ) ) ) <EOL> ramdisk = forms . ChoiceField ( <EOL> label = _ ( '<STR_LIT>' ) , <EOL> required = False , <EOL> widget = forms . SelectWidget ( <EOL> transform = lambda x : "<STR_LIT>" % ( <EOL> x . name , defaultfilters . filesizeformat ( x . size ) ) ) ) <EOL> disk_format = forms . ChoiceField ( label = _ ( '<STR_LIT>' ) , <EOL> choices = [ ] , <EOL> widget = forms . Select ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> architecture = forms . CharField ( <EOL> max_length = <NUM_LIT:255> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> required = False ) <EOL> minimum_disk = forms . IntegerField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> min_value = <NUM_LIT:0> , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> required = False ) <EOL> minimum_ram = forms . IntegerField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> min_value = <NUM_LIT:0> , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> required = False ) <EOL> is_copying = forms . BooleanField ( <EOL> label = _ ( "<STR_LIT>" ) , initial = True , required = False , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> widget = forms . CheckboxInput ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : '<STR_LIT:source>' } ) ) <EOL> is_public = forms . BooleanField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> required = False ) <EOL> protected = forms . BooleanField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( '<STR_LIT>' ) , <EOL> required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( CreateImageForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> if ( not settings . HORIZON_IMAGES_ALLOW_UPLOAD or <EOL> not policy . check ( ( ( "<STR_LIT:image>" , "<STR_LIT>" ) , ) , request ) ) : <EOL> self . _hide_file_source_type ( ) <EOL> if not policy . check ( ( ( "<STR_LIT:image>" , "<STR_LIT>" ) , ) , request ) : <EOL> self . _hide_url_source_type ( ) <EOL> if not policy . check ( ( ( "<STR_LIT:image>" , "<STR_LIT>" ) , ) , request ) : <EOL> self . _hide_is_public ( ) <EOL> self . fields [ '<STR_LIT>' ] . choices = IMAGE_FORMAT_CHOICES <EOL> try : <EOL> kernel_images = api . glance . image_list_detailed ( <EOL> request , filters = { '<STR_LIT>' : '<STR_LIT>' } ) [ <NUM_LIT:0> ] <EOL> except Exception : <EOL> kernel_images = [ ] <EOL> msg = _ ( '<STR_LIT>' ) <EOL> messages . error ( request , msg ) <EOL> if kernel_images : <EOL> choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] <EOL> for image in kernel_images : <EOL> choices . append ( ( image . id , image ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> try : <EOL> ramdisk_images = api . glance . image_list_detailed ( <EOL> request , filters = { '<STR_LIT>' : '<STR_LIT>' } ) [ <NUM_LIT:0> ] <EOL> except Exception : <EOL> ramdisk_images = [ ] <EOL> msg = _ ( '<STR_LIT>' ) <EOL> messages . error ( request , msg ) <EOL> if ramdisk_images : <EOL> choices = [ ( '<STR_LIT>' , _ ( "<STR_LIT>" ) ) ] <EOL> for image in ramdisk_images : <EOL> choices . append ( ( image . id , image ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = choices <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> def _hide_file_source_type ( self ) : <EOL> self . fields [ '<STR_LIT>' ] . widget = HiddenInput ( ) <EOL> source_type = self . fields [ '<STR_LIT>' ] <EOL> source_type . choices = [ choice for choice in source_type . choices <EOL> if choice [ <NUM_LIT:0> ] != '<STR_LIT:file>' ] <EOL> if len ( source_type . choices ) == <NUM_LIT:1> : <EOL> source_type . widget = HiddenInput ( ) <EOL> def _hide_url_source_type ( self ) : <EOL> self . fields [ '<STR_LIT>' ] . widget = HiddenInput ( ) <EOL> source_type = self . fields [ '<STR_LIT>' ] <EOL> source_type . choices = [ choice for choice in source_type . choices <EOL> if choice [ <NUM_LIT:0> ] != '<STR_LIT:url>' ] <EOL> if len ( source_type . choices ) == <NUM_LIT:1> : <EOL> source_type . widget = HiddenInput ( ) <EOL> def _hide_is_public ( self ) : <EOL> self . fields [ '<STR_LIT>' ] . widget = HiddenInput ( ) <EOL> self . fields [ '<STR_LIT>' ] . initial = False <EOL> def clean ( self ) : <EOL> data = super ( CreateImageForm , self ) . clean ( ) <EOL> image_file = data . get ( '<STR_LIT>' , None ) <EOL> image_url = data . get ( '<STR_LIT>' , None ) <EOL> if not image_url and not image_file : <EOL> raise ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> elif image_url and image_file : <EOL> raise ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> return data <EOL> def handle ( self , request , data ) : <EOL> meta = create_image_metadata ( data ) <EOL> if ( settings . HORIZON_IMAGES_ALLOW_UPLOAD and <EOL> policy . check ( ( ( "<STR_LIT:image>" , "<STR_LIT>" ) , ) , request ) and <EOL> data . get ( '<STR_LIT>' , None ) ) : <EOL> meta [ '<STR_LIT:data>' ] = self . files [ '<STR_LIT>' ] <EOL> elif data [ '<STR_LIT>' ] : <EOL> meta [ '<STR_LIT>' ] = data [ '<STR_LIT>' ] <EOL> else : <EOL> meta [ '<STR_LIT:location>' ] = data [ '<STR_LIT>' ] <EOL> try : <EOL> image = api . glance . image_create ( request , ** meta ) <EOL> messages . info ( request , <EOL> _ ( '<STR_LIT>' ) % <EOL> meta [ '<STR_LIT:name>' ] ) <EOL> return image <EOL> except Exception as e : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> if hasattr ( e , '<STR_LIT:code>' ) and e . code == <NUM_LIT> : <EOL> if "<STR_LIT>" in e . details : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % meta [ '<STR_LIT>' ] <EOL> elif "<STR_LIT>" in e . details : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> elif "<STR_LIT>" in e . details : <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exceptions . handle ( request , msg ) <EOL> return False <EOL> class UpdateImageForm ( forms . SelfHandlingForm ) : <EOL> image_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> name = forms . CharField ( max_length = <NUM_LIT:255> , label = _ ( "<STR_LIT:Name>" ) ) <EOL> description = forms . CharField ( <EOL> max_length = <NUM_LIT:255> , <EOL> widget = forms . Textarea ( attrs = { '<STR_LIT>' : <NUM_LIT:4> } ) , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> kernel = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ) <EOL> ramdisk = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ) <EOL> architecture = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT>' : '<STR_LIT>' } ) , <EOL> ) <EOL> disk_format = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> ) <EOL> minimum_disk = forms . IntegerField ( label = _ ( "<STR_LIT>" ) , <EOL> min_value = <NUM_LIT:0> , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> required = False ) <EOL> minimum_ram = forms . IntegerField ( label = _ ( "<STR_LIT>" ) , <EOL> min_value = <NUM_LIT:0> , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> required = False ) <EOL> public = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> protected = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdateImageForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . choices = [ ( value , name ) for value , <EOL> name in IMAGE_FORMAT_CHOICES <EOL> if value ] <EOL> if not policy . check ( ( ( "<STR_LIT:image>" , "<STR_LIT>" ) , ) , request ) : <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . CheckboxInput ( <EOL> attrs = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . fields [ '<STR_LIT>' ] . help_text = _ ( <EOL> '<STR_LIT>' ) <EOL> def handle ( self , request , data ) : <EOL> image_id = data [ '<STR_LIT>' ] <EOL> error_updating = _ ( '<STR_LIT>' ) <EOL> meta = create_image_metadata ( data ) <EOL> meta [ '<STR_LIT>' ] = False <EOL> try : <EOL> image = api . glance . image_update ( request , image_id , ** meta ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> return image <EOL> except Exception : <EOL> exceptions . handle ( request , error_updating % image_id ) </s>
