<s> from django . core . urlresolvers import reverse <EOL> from django . template . defaultfilters import filesizeformat <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . views . decorators . debug import sensitive_variables <EOL> from horizon import exceptions <EOL> from horizon import forms <EOL> from horizon import messages <EOL> from horizon . utils import fields <EOL> from horizon . utils import validators <EOL> from openstack_dashboard import api <EOL> from openstack_dashboard . dashboards . project . images_and_snapshots import utils <EOL> def _image_choice_title ( img ) : <EOL> gb = filesizeformat ( img . bytes ) <EOL> return '<STR_LIT>' % ( img . display_name , gb ) <EOL> class RebuildInstanceForm ( forms . SelfHandlingForm ) : <EOL> instance_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> image = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = fields . SelectWidget ( attrs = { '<STR_LIT:class>' : '<STR_LIT>' } , <EOL> data_attrs = ( '<STR_LIT:size>' , '<STR_LIT>' ) , <EOL> transform = _image_choice_title ) ) <EOL> password = forms . RegexField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . PasswordInput ( render_value = False ) , <EOL> regex = validators . password_validator ( ) , <EOL> error_messages = { '<STR_LIT>' : validators . password_validator_msg ( ) } ) <EOL> confirm_password = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . PasswordInput ( render_value = False ) ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( RebuildInstanceForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> instance_id = kwargs . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' ) <EOL> self . fields [ '<STR_LIT>' ] . initial = instance_id <EOL> images = utils . get_available_images ( request , request . user . tenant_id ) <EOL> choices = [ ( image . id , image . name ) for image in images ] <EOL> if choices : <EOL> choices . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> else : <EOL> choices . insert ( <NUM_LIT:0> , ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ) <EOL> self . fields [ '<STR_LIT:image>' ] . choices = choices <EOL> def clean ( self ) : <EOL> cleaned_data = super ( RebuildInstanceForm , self ) . clean ( ) <EOL> if '<STR_LIT:password>' in cleaned_data : <EOL> passwd = cleaned_data . get ( '<STR_LIT:password>' ) <EOL> confirm = cleaned_data . get ( '<STR_LIT>' ) <EOL> if passwd is not None and confirm is not None : <EOL> if passwd != confirm : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> return cleaned_data <EOL> @ sensitive_variables ( '<STR_LIT:data>' , '<STR_LIT:password>' ) <EOL> def handle ( self , request , data ) : <EOL> instance = data . get ( '<STR_LIT>' ) <EOL> image = data . get ( '<STR_LIT:image>' ) <EOL> password = data . get ( '<STR_LIT:password>' ) or None <EOL> try : <EOL> api . nova . server_rebuild ( request , instance , image , password ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) % instance ) <EOL> except Exception : <EOL> redirect = reverse ( '<STR_LIT>' ) <EOL> exceptions . handle ( request , _ ( "<STR_LIT>" ) , <EOL> redirect = redirect ) <EOL> return True </s>
