<s> import json <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> class UpdateMetadataForm ( forms . SelfHandlingForm ) : <EOL> def handle ( self , request , data ) : <EOL> id = self . initial [ '<STR_LIT:id>' ] <EOL> old_metadata = self . initial [ '<STR_LIT>' ] <EOL> try : <EOL> new_metadata = json . loads ( self . data [ '<STR_LIT>' ] ) <EOL> metadata = dict ( <EOL> ( item [ '<STR_LIT:key>' ] , str ( item [ '<STR_LIT:value>' ] ) ) <EOL> for item in new_metadata <EOL> ) <EOL> api . nova . flavor_extra_set ( request , id , metadata ) <EOL> remove_keys = [ key for key in old_metadata if key not in metadata ] <EOL> api . nova . flavor_extra_delete ( request , id , remove_keys ) <EOL> message = _ ( '<STR_LIT>' ) <EOL> messages . success ( request , message ) <EOL> except Exception : <EOL> exceptions . handle ( request , <EOL> _ ( '<STR_LIT>' ) ) <EOL> return False <EOL> return True </s>
