<s> import uuid <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , HttpResponseNotAllowed , HttpResponse <EOL> from django . views . generic import simple <EOL> from vt_manager . controller . drivers . VTDriver import VTDriver <EOL> from pypelib . RuleTable import RuleTable <EOL> from vt_manager . controller . policies . RuleTableManager import RuleTableManager <EOL> from pypelib . Rule import Rule <EOL> from pypelib . utils . Exceptions import * <EOL> @ login_required <EOL> def rule_table_view ( request , TableName = None ) : <EOL> if ( not request . user . is_superuser ) : <EOL> return simple . direct_to_template ( request , <EOL> template = '<STR_LIT>' , <EOL> extra_context = { '<STR_LIT:user>' : request . user } , <EOL> ) <EOL> else : <EOL> try : <EOL> ruleTable = RuleTableManager . getInstance ( RuleTableManager . getDefaultName ( ) ) <EOL> return simple . direct_to_template ( <EOL> request , <EOL> template = '<STR_LIT>' , <EOL> extra_context = { '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : ruleTable } <EOL> ) <EOL> except ZeroPolicyObjectsReturned : <EOL> return HttpResponseRedirect ( "<STR_LIT>" ) <EOL> except MultiplePolicyObjectsReturned : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( RuleTableManager . getDefaultName ( ) , ) ) ) <EOL> def policy_create ( request , table ) : <EOL> errors = [ ] <EOL> rules = [ ] <EOL> mapps = RuleTableManager . getActionMappings ( ) <EOL> priorityList = RuleTableManager . getPriorityList ( ) <EOL> condMapps = RuleTableManager . getConditionMappings ( ) <EOL> return simple . direct_to_template ( request , <EOL> template = '<STR_LIT>' , <EOL> extra_context = { '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : table , <EOL> '<STR_LIT>' : mapps , <EOL> '<STR_LIT>' : priorityList , <EOL> '<STR_LIT>' : RuleTableManager . GetResolverMappings ( table ) , <EOL> '<STR_LIT>' : condMapps , <EOL> '<STR_LIT>' : RuleTableManager . getActionMappings ( ) } , <EOL> ) <EOL> def policy_edit ( request , table ) : <EOL> if "<STR_LIT>" in request . META : <EOL> if "<STR_LIT>" in request . META [ '<STR_LIT>' ] or "<STR_LIT>" in request . META [ '<STR_LIT>' ] : <EOL> ruleTableSet = RuleTableManager . getAllInstances ( RuleTableManager . getDefaultName ( ) ) <EOL> return simple . direct_to_template ( request , <EOL> template = '<STR_LIT>' , <EOL> extra_context = { '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : ruleTableSet } <EOL> ) <EOL> return HttpResponseRedirect ( "<STR_LIT:/>" ) <EOL> '''<STR_LIT>''' <EOL> def policy_delete ( request , table_uuid ) : <EOL> if request . method == "<STR_LIT:POST>" : <EOL> RuleTableManager . deleteInstance ( table_uuid ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" ) <EOL> def rule_create ( request , table_name = None ) : <EOL> errors = list ( ) <EOL> formMode = request . POST . get ( "<STR_LIT>" ) <EOL> tableName = request . POST . get ( "<STR_LIT>" ) <EOL> PreviousPriority = request . POST . get ( "<STR_LIT>" ) <EOL> editing = request . POST . get ( "<STR_LIT>" ) <EOL> ruleid = request . POST . get ( "<STR_LIT>" ) <EOL> ruleCondition = request . POST . get ( "<STR_LIT>" ) <EOL> ruleDesc = request . POST . get ( "<STR_LIT:description>" ) <EOL> ruleError = request . POST . get ( "<STR_LIT>" ) <EOL> ruleType = request . POST . get ( "<STR_LIT:type>" ) <EOL> ruleAction = request . POST . get ( "<STR_LIT:action>" ) <EOL> ruleValue = request . POST . get ( "<STR_LIT:value>" ) <EOL> rulePriority = request . POST . get ( "<STR_LIT>" ) <EOL> ruleEnable = request . POST . get ( "<STR_LIT>" ) <EOL> previousTable = request . POST . get ( "<STR_LIT>" ) <EOL> expertRule = request . POST . get ( "<STR_LIT>" ) <EOL> newConditions = request . POST . get ( "<STR_LIT>" ) <EOL> saved = request . POST . get ( "<STR_LIT>" ) <EOL> if rulePriority == '<STR_LIT>' or rulePriority == '<STR_LIT>' : <EOL> priority = None <EOL> else : <EOL> priority = int ( rulePriority ) <EOL> if formMode == "<STR_LIT>" : <EOL> if ruleError == "<STR_LIT>" : <EOL> errors . append ( "<STR_LIT>" ) <EOL> if ruleCondition == "<STR_LIT>" : <EOL> errors . append ( "<STR_LIT>" ) <EOL> try : <EOL> str ( ruleDesc ) <EOL> except : <EOL> errors . append ( "<STR_LIT>" ) <EOL> try : <EOL> str ( ruleError ) <EOL> except : <EOL> errors . append ( "<STR_LIT>" ) <EOL> try : <EOL> str ( ruleCondition ) <EOL> except : <EOL> errors . append ( "<STR_LIT>" ) <EOL> if request . POST . get ( "<STR_LIT>" ) == '<STR_LIT>' : <EOL> enable = True <EOL> else : <EOL> enable = False <EOL> if ruleType == "<STR_LIT>" : <EOL> ruleType = "<STR_LIT>" <EOL> if saved == None : <EOL> saved = False <EOL> if formMode == "<STR_LIT>" : <EOL> if ruleAction != "<STR_LIT:None>" : <EOL> strings = "<STR_LIT>" + ruleCondition + "<STR_LIT>" + ruleValue + "<STR_LIT:U+0020>" + ruleType + "<STR_LIT>" + ruleAction + "<STR_LIT>" + ruleError + "<STR_LIT>" + ruleDesc <EOL> else : <EOL> strings = "<STR_LIT>" + ruleCondition + "<STR_LIT>" + ruleValue + "<STR_LIT:U+0020>" + ruleType + "<STR_LIT>" + ruleError + "<STR_LIT>" + ruleDesc <EOL> else : <EOL> strings = expertRule <EOL> try : <EOL> str ( expertRule ) <EOL> except : <EOL> errors . append ( "<STR_LIT>" ) <EOL> try : <EOL> if errors : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if editing == '<STR_LIT:1>' : <EOL> if previousTable == tableName : <EOL> try : <EOL> RuleTableManager . editRule ( strings , enable , priority , PreviousPriority , tableName ) <EOL> except Exception as e : <EOL> raise e <EOL> else : <EOL> RuleTableManager . AddRule ( strings , enable , priority , tableName = tableName ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" ) <EOL> except Exception as e : <EOL> errors . append ( e ) <EOL> errors . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> priority = RuleTableManager . getPriorityList ( tableName ) <EOL> priority = RuleTableManager . getPriorityList ( tableName ) <EOL> try : <EOL> int ( rulePriority ) <EOL> if int ( rulePriority ) in priority : <EOL> priority . pop ( priority . index ( int ( rulePriority ) ) ) <EOL> except : <EOL> rulePriority = "<STR_LIT>" <EOL> if ruleValue == "<STR_LIT>" : <EOL> value2 = [ "<STR_LIT>" ] <EOL> else : <EOL> value2 = [ "<STR_LIT>" ] <EOL> if ruleType == "<STR_LIT>" : <EOL> type2 = [ "<STR_LIT>" ] <EOL> else : <EOL> ruleType = "<STR_LIT>" <EOL> type2 = [ "<STR_LIT>" ] <EOL> context = { '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : tableName , <EOL> '<STR_LIT>' : PreviousPriority , <EOL> '<STR_LIT>' : ruleEnable , <EOL> '<STR_LIT>' : '<STR_LIT:True>' , <EOL> '<STR_LIT>' : ruleValue , <EOL> '<STR_LIT>' : value2 , <EOL> '<STR_LIT>' : ruleType , <EOL> '<STR_LIT>' : type2 , <EOL> '<STR_LIT>' : ruleError , <EOL> '<STR_LIT:description>' : ruleDesc , <EOL> '<STR_LIT>' : "<STR_LIT:U+0020>" + ruleCondition + "<STR_LIT:U+0020>" , <EOL> '<STR_LIT>' : tableName , <EOL> '<STR_LIT>' : request . POST . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:action>' : ruleAction , <EOL> '<STR_LIT>' : rulePriority , <EOL> '<STR_LIT>' : priority , <EOL> '<STR_LIT>' : RuleTableManager . GetResolverMappings ( tableName ) , <EOL> '<STR_LIT>' : RuleTableManager . getConditionMappings ( ) , <EOL> '<STR_LIT>' : RuleTableManager . getActionMappings ( ) , <EOL> '<STR_LIT>' : errors , <EOL> '<STR_LIT>' : ruleid , } <EOL> return simple . direct_to_template ( request , <EOL> template = '<STR_LIT>' , <EOL> extra_context = context ) <EOL> def enable_disable ( request , rule_uuid , table_name ) : <EOL> RuleTableManager . EnableOrDisableRule ( rule_uuid , table_name ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" ) <EOL> def condition_create ( request , TableName ) : <EOL> return simple . direct_to_template ( request , <EOL> template = '<STR_LIT>' , <EOL> extra_context = { '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : TableName , <EOL> '<STR_LIT>' : RuleTableManager . getConditionMappings ( ) } , <EOL> ) <EOL> def rule_delete ( request , table_name ) : <EOL> rule_uuid = request . POST . get ( "<STR_LIT>" ) <EOL> RuleTableManager . deleteRule ( rule_uuid , table_name ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" ) <EOL> def update_ruleTable_policy ( request ) : <EOL> policy = request . POST . get ( "<STR_LIT>" ) <EOL> tableName = request . POST . get ( "<STR_LIT>" ) <EOL> RuleTableManager . UpdateRuleTablePolicy ( policy , tableName ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" ) <EOL> def rule_edit ( request , table_name , rule_uuid , context = None ) : <EOL> load = request . POST . get ( '<STR_LIT>' ) <EOL> if not load == '<STR_LIT:True>' : <EOL> rule = RuleTableManager . getRuleOrIndexOrIsEnabled ( rule_uuid , '<STR_LIT>' , table_name ) <EOL> rulevalues = RuleTableManager . getValue ( rule ) <EOL> ruletypes = RuleTableManager . getType ( rule ) <EOL> edit = True <EOL> actionList = RuleTableManager . SetActionList ( rule , RuleTableManager . getActionMappings ( ) ) <EOL> priorityList = RuleTableManager . SetPriorityList ( rule , table_name ) <EOL> error = str ( rule . getErrorMsg ( ) ) <EOL> description = str ( rule . getDescription ( ) ) <EOL> return simple . direct_to_template ( request , <EOL> template = '<STR_LIT>' , <EOL> extra_context = { '<STR_LIT:user>' : request . user , <EOL> '<STR_LIT>' : edit , <EOL> '<STR_LIT>' : rule , <EOL> '<STR_LIT>' : RuleTableManager . getRuleOrIndexOrIsEnabled ( rule_uuid , '<STR_LIT>' , table_name ) , <EOL> '<STR_LIT>' : RuleTableManager . getRuleOrIndexOrIsEnabled ( rule_uuid , '<STR_LIT>' , table_name ) , <EOL> '<STR_LIT>' : rulevalues [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : rulevalues [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : ruletypes [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : ruletypes [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : rule_uuid , <EOL> '<STR_LIT>' : table_name , <EOL> '<STR_LIT>' : error , <EOL> '<STR_LIT:description>' : description , <EOL> '<STR_LIT>' : rule . getConditionDump ( ) , <EOL> '<STR_LIT:action>' : actionList [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : priorityList [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : priorityList [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : RuleTableManager . GetResolverMappings ( ) , <EOL> '<STR_LIT>' : RuleTableManager . getConditionMappings ( ) , <EOL> '<STR_LIT>' : RuleTableManager . getActionMappings ( ) , <EOL> '<STR_LIT>' : table_name } , <EOL> ) <EOL> else : <EOL> return rule_create ( request , table_name ) </s>
