<s> from django import forms <EOL> from django . utils . html import strip_tags <EOL> from django . utils . text import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from ralph . lib . transitions . models import ( <EOL> Action , <EOL> Transition , <EOL> TRANSITION_ORIGINAL_STATUS , <EOL> TransitionModel <EOL> ) <EOL> def wrap_action_name ( action ) : <EOL> verbose_name = action . verbose_name <EOL> help_text = action . help_text <EOL> if help_text : <EOL> verbose_name = verbose_name + """<STR_LIT>""" . format ( <EOL> txt = strip_tags ( str ( help_text ) ) , <EOL> ) <EOL> return mark_safe ( verbose_name ) <EOL> class TransitionForm ( forms . ModelForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise ValueError <EOL> if not isinstance ( self . _transition_model_instance , TransitionModel ) : <EOL> raise TypeError <EOL> super ( ) . __init__ ( * args , ** kwargs ) <EOL> self . model = self . _transition_model_instance . content_type . model_class ( ) <EOL> field_name = self . _transition_model_instance . field_name <EOL> choices = tuple ( self . model . _meta . get_field ( field_name ) . choices ) <EOL> self . fields [ '<STR_LIT:source>' ] = forms . MultipleChoiceField ( choices = choices ) <EOL> self . fields [ '<STR_LIT:target>' ] = forms . ChoiceField ( <EOL> choices = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) + choices + ( <EOL> TRANSITION_ORIGINAL_STATUS , <EOL> ) <EOL> ) <EOL> actions_choices = [ <EOL> ( i . id , wrap_action_name ( getattr ( self . model , i . name ) ) ) <EOL> for i in Action . objects . filter ( <EOL> content_type = self . _transition_model_instance . content_type <EOL> ) <EOL> ] <EOL> self . fields [ '<STR_LIT>' ] = forms . MultipleChoiceField ( <EOL> choices = actions_choices , widget = forms . CheckboxSelectMultiple ( ) <EOL> ) <EOL> self . fields [ '<STR_LIT>' ] . required = False <EOL> def clean ( self ) : <EOL> cleaned_data = super ( ) . clean ( ) <EOL> actions = Action . objects . in_bulk ( cleaned_data [ '<STR_LIT>' ] ) <EOL> attachment_counter = <NUM_LIT:0> <EOL> one_action = False <EOL> one_action_name = '<STR_LIT>' <EOL> actions_items = actions . items ( ) <EOL> for k , v in actions_items : <EOL> action = getattr ( self . model , v . name ) <EOL> if getattr ( action , '<STR_LIT>' , False ) : <EOL> attachment_counter += <NUM_LIT:1> <EOL> if getattr ( action , '<STR_LIT>' , False ) : <EOL> one_action = True <EOL> one_action_name = getattr ( action , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if attachment_counter > <NUM_LIT:1> : <EOL> msg = _ ( <EOL> '<STR_LIT>' <EOL> ) <EOL> self . add_error ( '<STR_LIT>' , msg ) <EOL> if one_action and len ( actions_items ) > <NUM_LIT:1> : <EOL> msg = _ ( <EOL> ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> ) % { '<STR_LIT:name>' : one_action_name } <EOL> self . add_error ( '<STR_LIT>' , msg ) <EOL> return cleaned_data <EOL> class Meta : <EOL> model = Transition <EOL> fields = [ '<STR_LIT:name>' , '<STR_LIT:source>' , '<STR_LIT:target>' , '<STR_LIT>' ] </s>
