<s> from django . core . urlresolvers import reverse <EOL> from django import http <EOL> from django . template . defaultfilters import filesizeformat <EOL> from django . utils import html <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 gbpui import client <EOL> from gbpui import column_filters as gfilters <EOL> from gbpui import fields <EOL> PROTOCOLS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> DIRECTIONS = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] <EOL> POLICY_ACTION_TYPES = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] <EOL> PROTOCOL_MAP = { '<STR_LIT:http>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class BaseUpdateForm ( forms . SelfHandlingForm ) : <EOL> def clean ( self ) : <EOL> cleaned_data = super ( BaseUpdateForm , self ) . clean ( ) <EOL> updated_data = { d : cleaned_data [ d ] for d in cleaned_data <EOL> if d in self . changed_data } <EOL> return updated_data <EOL> class UpdatePolicyRuleSetForm ( BaseUpdateForm ) : <EOL> name = forms . CharField ( label = _ ( "<STR_LIT:Name>" ) ) <EOL> description = forms . CharField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> policy_rules = forms . MultipleChoiceField ( label = _ ( "<STR_LIT>" ) , ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdatePolicyRuleSetForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> rules = [ ] <EOL> try : <EOL> items = client . policyrule_list ( request , <EOL> tenant_id = request . user . tenant_id ) <EOL> rules = [ ( p . id , p . name ) for p in items ] <EOL> policy_rule_set = client . policy_rule_set_get ( <EOL> request , self . initial [ '<STR_LIT>' ] ) <EOL> if policy_rule_set : <EOL> self . fields [ '<STR_LIT:name>' ] . initial = policy_rule_set . name <EOL> self . fields [ <EOL> '<STR_LIT:description>' ] . initial = policy_rule_set . description <EOL> self . fields [ '<STR_LIT>' ] . initial = policy_rule_set . shared <EOL> existing = [ item for item in policy_rule_set . policy_rules ] <EOL> self . fields [ '<STR_LIT>' ] . initial = existing <EOL> except Exception : <EOL> exceptions . handle ( request , _ ( '<STR_LIT>' ) ) <EOL> self . fields [ '<STR_LIT>' ] . choices = rules <EOL> def handle ( self , request , context ) : <EOL> try : <EOL> policy_rule_set_id = self . initial [ '<STR_LIT>' ] <EOL> if context . get ( '<STR_LIT:name>' ) : <EOL> context [ '<STR_LIT:name>' ] = html . escape ( context [ '<STR_LIT:name>' ] ) <EOL> if context . get ( '<STR_LIT:description>' ) : <EOL> context [ '<STR_LIT:description>' ] = html . escape ( context [ '<STR_LIT:description>' ] ) <EOL> client . policy_rule_set_update ( request , <EOL> policy_rule_set_id , <EOL> ** context <EOL> ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> return http . HttpResponseRedirect ( url ) <EOL> except Exception : <EOL> redirect = reverse ( '<STR_LIT>' ) <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) , <EOL> redirect = redirect ) <EOL> class AddPolicyActionForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( label = _ ( "<STR_LIT:Name>" ) ) <EOL> description = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> action_type = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , <EOL> choices = POLICY_ACTION_TYPES , <EOL> widget = forms . Select ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' <EOL> } ) ) <EOL> action_value = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> choices = [ ] , <EOL> widget = forms . Select ( attrs = { <EOL> '<STR_LIT:class>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:source>' , <EOL> '<STR_LIT>' : <EOL> _ ( '<STR_LIT>' ) <EOL> } ) ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> initial = False , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( AddPolicyActionForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> try : <EOL> sc_specs = client . servicechainspec_list ( request , <EOL> tenant_id = request . user . tenant_id ) <EOL> self . fields [ '<STR_LIT>' ] . choices = [ ( spec . id , <EOL> ( spec . name if spec . name is not None else "<STR_LIT>" ) + "<STR_LIT::>" + spec . id ) <EOL> for spec in sc_specs ] <EOL> except Exception : <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) , redirect = url ) <EOL> def handle ( self , request , context ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> try : <EOL> if not context [ '<STR_LIT>' ] : <EOL> del context [ '<STR_LIT>' ] <EOL> if context . get ( '<STR_LIT:name>' ) : <EOL> context [ '<STR_LIT:name>' ] = html . escape ( context [ '<STR_LIT:name>' ] ) <EOL> if context . get ( '<STR_LIT:description>' ) : <EOL> context [ '<STR_LIT:description>' ] = html . escape ( context [ '<STR_LIT:description>' ] ) <EOL> action = client . policyaction_create ( request , ** context ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> return action <EOL> except Exception : <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) , redirect = url ) <EOL> class UpdatePolicyActionForm ( BaseUpdateForm ) : <EOL> name = forms . CharField ( label = _ ( "<STR_LIT:Name>" ) ) <EOL> description = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdatePolicyActionForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> try : <EOL> policyaction_id = self . initial [ '<STR_LIT>' ] <EOL> pa = client . policyaction_get ( request , policyaction_id ) <EOL> self . fields [ '<STR_LIT:name>' ] . initial = pa . name <EOL> self . fields [ '<STR_LIT:description>' ] . initial = pa . description <EOL> self . fields [ '<STR_LIT>' ] . initial = pa . shared <EOL> except Exception : <EOL> pass <EOL> def handle ( self , request , context ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> try : <EOL> policyaction_id = self . initial [ '<STR_LIT>' ] <EOL> if context . get ( '<STR_LIT:name>' ) : <EOL> context [ '<STR_LIT:name>' ] = html . escape ( context [ '<STR_LIT:name>' ] ) <EOL> if context . get ( '<STR_LIT:description>' ) : <EOL> context [ '<STR_LIT:description>' ] = html . escape ( context [ '<STR_LIT:description>' ] ) <EOL> client . policyaction_update ( request , policyaction_id , ** context ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> return http . HttpResponseRedirect ( url ) <EOL> except Exception : <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) , redirect = url ) <EOL> class AddPolicyClassifierForm ( forms . SelfHandlingForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT> , label = _ ( "<STR_LIT:Name>" ) , required = False ) <EOL> protocol = forms . CharField ( required = True ) <EOL> port_range = forms . CharField ( max_length = <NUM_LIT> , label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> widget = forms . TextInput ( attrs = { '<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>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : _ ( "<STR_LIT>" ) } ) ) <EOL> direction = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , <EOL> choices = [ ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) ] ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , <EOL> initial = False , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( AddPolicyClassifierForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . widget = fields . DropdownEditWidget ( <EOL> data_list = PROTOCOLS , name = '<STR_LIT:list>' ) <EOL> def handle ( self , request , context ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> try : <EOL> protocol = context . get ( '<STR_LIT>' ) . lower ( ) <EOL> if protocol in PROTOCOL_MAP : <EOL> context [ '<STR_LIT>' ] = PROTOCOL_MAP [ protocol ] <EOL> elif protocol == "<STR_LIT>" : <EOL> del context [ '<STR_LIT>' ] <EOL> if not context . get ( '<STR_LIT>' ) : <EOL> context [ '<STR_LIT>' ] = None <EOL> if context . get ( '<STR_LIT:name>' ) : <EOL> context [ '<STR_LIT:name>' ] = html . escape ( context [ '<STR_LIT:name>' ] ) <EOL> classifier = client . policyclassifier_create ( request , ** context ) <EOL> messages . success ( <EOL> request , _ ( '<STR_LIT>' ) ) <EOL> return classifier <EOL> except Exception : <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) , <EOL> redirect = url ) <EOL> class UpdatePolicyClassifierForm ( BaseUpdateForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT> , label = _ ( "<STR_LIT:Name>" ) , required = False ) <EOL> description = forms . CharField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> protocol = forms . CharField ( required = True ) <EOL> port_range = forms . CharField ( max_length = <NUM_LIT> , label = _ ( "<STR_LIT>" ) , <EOL> required = False ) <EOL> direction = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) , choices = DIRECTIONS ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdatePolicyClassifierForm , self ) . __init__ ( <EOL> request , * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . widget = fields . DropdownEditWidget ( <EOL> data_list = PROTOCOLS , name = '<STR_LIT:list>' ) <EOL> try : <EOL> policyclassifier_id = self . initial [ '<STR_LIT>' ] <EOL> classifier = client . policyclassifier_get ( <EOL> request , policyclassifier_id ) <EOL> classifier = gfilters . update_classifier_attributes ( classifier ) <EOL> for item in [ '<STR_LIT:name>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . fields [ item ] . initial = getattr ( classifier , item ) <EOL> protocol = getattr ( classifier , "<STR_LIT>" ) <EOL> if protocol is not None : <EOL> self . fields [ '<STR_LIT>' ] . initial = protocol <EOL> else : <EOL> self . fields [ '<STR_LIT>' ] . initial = "<STR_LIT>" <EOL> except Exception : <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) ) <EOL> def handle ( self , request , context ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> try : <EOL> policyclassifier_id = self . initial [ '<STR_LIT>' ] <EOL> protocol = context . get ( '<STR_LIT>' ) . lower ( ) <EOL> if protocol in PROTOCOL_MAP : <EOL> context [ '<STR_LIT>' ] = PROTOCOL_MAP [ protocol ] <EOL> elif protocol == "<STR_LIT>" : <EOL> context [ '<STR_LIT>' ] = None <EOL> if '<STR_LIT>' in context and context [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> context [ '<STR_LIT>' ] = None <EOL> if context . get ( '<STR_LIT:name>' ) : <EOL> context [ '<STR_LIT:name>' ] = html . escape ( context [ '<STR_LIT:name>' ] ) <EOL> if context . get ( '<STR_LIT:description>' ) : <EOL> context [ '<STR_LIT:description>' ] = html . escape ( context [ '<STR_LIT:description>' ] ) <EOL> client . policyclassifier_update ( self . request , <EOL> policyclassifier_id , ** context ) <EOL> messages . success ( <EOL> request , _ ( '<STR_LIT>' ) ) <EOL> return http . HttpResponseRedirect ( url ) <EOL> except Exception : <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) , <EOL> redirect = url ) <EOL> class UpdatePolicyRuleForm ( BaseUpdateForm ) : <EOL> name = forms . CharField ( max_length = <NUM_LIT> , label = _ ( "<STR_LIT:Name>" ) , required = False ) <EOL> description = forms . CharField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> policy_classifier_id = forms . ChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> policy_actions = forms . MultipleChoiceField ( label = _ ( "<STR_LIT>" ) ) <EOL> shared = forms . BooleanField ( label = _ ( "<STR_LIT>" ) , required = False ) <EOL> def __init__ ( self , request , * args , ** kwargs ) : <EOL> super ( UpdatePolicyRuleForm , self ) . __init__ ( request , * args , ** kwargs ) <EOL> try : <EOL> policyrule_id = self . initial [ '<STR_LIT>' ] <EOL> rule = client . policyrule_get ( request , policyrule_id ) <EOL> for item in [ '<STR_LIT:name>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . fields [ item ] . initial = getattr ( rule , item ) <EOL> actions = client . policyaction_list ( request , <EOL> tenant_id = request . user . tenant_id ) <EOL> action_list = [ a . id for a in actions ] <EOL> for action in actions : <EOL> action . set_id_as_name_if_empty ( ) <EOL> actions = sorted ( actions , key = lambda action : action . name ) <EOL> action_list = [ ( a . id , a . name ) for a in actions ] <EOL> self . fields [ '<STR_LIT>' ] . choices = action_list <EOL> classifiers = client . policyclassifier_list ( request , <EOL> tenant_id = request . user . tenant_id ) <EOL> classifier_list = [ ( c . id , c . name ) for c in classifiers ] <EOL> self . fields [ '<STR_LIT>' ] . choices = classifier_list <EOL> except Exception : <EOL> exceptions . handle ( <EOL> request , _ ( "<STR_LIT>" ) ) <EOL> def handle ( self , request , context ) : <EOL> url = reverse ( '<STR_LIT>' ) <EOL> try : <EOL> prid = self . initial [ '<STR_LIT>' ] <EOL> if context . get ( '<STR_LIT:name>' ) : <EOL> context [ '<STR_LIT:name>' ] = html . escape ( context [ '<STR_LIT:name>' ] ) <EOL> if context . get ( '<STR_LIT:description>' ) : <EOL> context [ '<STR_LIT:description>' ] = html . escape ( context [ '<STR_LIT:description>' ] ) <EOL> client . policyrule_update ( request , prid , ** context ) <EOL> messages . success ( request , _ ( '<STR_LIT>' ) ) <EOL> return http . HttpResponseRedirect ( url ) <EOL> except Exception as e : <EOL> msg = _ ( "<STR_LIT>" ) % ( str ( e ) ) <EOL> exceptions . handle ( request , msg , redirect = url ) </s>
