<s> import logging <EOL> from django . contrib import messages <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , HttpResponseBadRequest , HttpResponse <EOL> from django . shortcuts import render <EOL> from webui . exceptions import UnauthorizedException , NotFoundException <EOL> from webui . forms import RuleRequestForm , RuleForm <EOL> from webui . models import TestPlan , Rule <EOL> from webui . views import signout <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ login_required <EOL> def rule_details ( request , testplan_id , rule_id ) : <EOL> try : <EOL> testplan = TestPlan ( auth_token = request . user . password ) . get ( testplan_id ) <EOL> rule = Rule ( testplan_id , auth_token = request . user . password ) . get ( rule_id ) <EOL> except UnauthorizedException : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return signout ( request ) <EOL> except NotFoundException : <EOL> logger . warning ( '<STR_LIT>' , rule_id , testplan_id ) <EOL> return render ( request , '<STR_LIT>' ) <EOL> except Exception as inst : <EOL> logger . error ( '<STR_LIT>' , exc_info = True ) <EOL> messages . error ( request , inst . message if inst . message else '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> initial_data = _rule_to_post_data ( rule ) <EOL> form = RuleRequestForm ( request . POST or None , initial = initial_data ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> Rule ( testplan_id , auth_token = request . user . password ) . update ( rule [ '<STR_LIT:id>' ] , form . cleaned_data ) <EOL> messages . success ( request , "<STR_LIT>" ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( str ( testplan_id ) , str ( rule_id ) ) ) ) <EOL> except UnauthorizedException : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return signout ( request ) <EOL> except Exception as inst : <EOL> logger . error ( '<STR_LIT>' , exc_info = True ) <EOL> messages . error ( request , inst . message if inst . message else '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( str ( testplan_id ) , str ( rule_id ) ) ) ) <EOL> data = { '<STR_LIT>' : testplan , '<STR_LIT>' : rule , '<STR_LIT>' : form } <EOL> return render ( request , '<STR_LIT>' , data ) <EOL> @ login_required <EOL> def rule_new ( request , testplan_id ) : <EOL> try : <EOL> testplan = TestPlan ( auth_token = request . user . password ) . get ( testplan_id ) <EOL> except UnauthorizedException : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return signout ( request ) <EOL> except NotFoundException : <EOL> return render ( request , '<STR_LIT>' ) <EOL> except Exception as inst : <EOL> logger . error ( '<STR_LIT>' , exc_info = True ) <EOL> messages . error ( request , inst . message if inst . message else '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> form = RuleForm ( request . POST or None ) <EOL> if form . is_valid ( ) : <EOL> try : <EOL> rule_id = Rule ( testplan_id , auth_token = request . user . password ) . create ( form . cleaned_data ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( str ( testplan_id ) , str ( rule_id ) ) ) ) <EOL> except UnauthorizedException : <EOL> logger . warning ( '<STR_LIT>' ) <EOL> return signout ( request ) <EOL> except Exception as inst : <EOL> messages . error ( request , inst . message if inst . message else '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( str ( testplan_id ) , ) ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : form , '<STR_LIT>' : testplan } ) <EOL> @ login_required <EOL> def rule_update ( request , testplan_id , rule_id ) : <EOL> if not request . POST : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( str ( testplan_id ) , str ( rule_id ) ) ) ) <EOL> name = request . POST . get ( '<STR_LIT:name>' ) <EOL> pk = request . POST . get ( '<STR_LIT>' ) <EOL> value = request . POST . get ( '<STR_LIT:value>' ) <EOL> if not name or not pk : <EOL> response = '<STR_LIT>' <EOL> return HttpResponseBadRequest ( response ) <EOL> if name == '<STR_LIT>' : <EOL> value = False if value == '<STR_LIT:0>' else True <EOL> try : <EOL> Rule ( testplan_id , auth_token = request . user . password ) . update ( pk , { name : value } ) <EOL> except Exception as inst : <EOL> logger . error ( '<STR_LIT>' , exc_info = True ) <EOL> return HttpResponseBadRequest ( content = '<STR_LIT>' . format ( inst . message ) ) <EOL> return HttpResponse ( ) <EOL> def _rule_to_post_data ( rule ) : <EOL> post_data = { } <EOL> post_data [ '<STR_LIT>' ] = rule [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in rule : <EOL> filter = rule [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in filter : <EOL> post_data [ '<STR_LIT>' ] = filter [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in filter : <EOL> post_data [ '<STR_LIT>' ] = filter [ '<STR_LIT>' ] <EOL> if '<STR_LIT:url>' in filter : <EOL> post_data [ '<STR_LIT>' ] = filter [ '<STR_LIT:url>' ] <EOL> if '<STR_LIT>' in filter : <EOL> post_data [ '<STR_LIT>' ] = filter [ '<STR_LIT>' ] <EOL> if '<STR_LIT:action>' in rule : <EOL> action = rule [ '<STR_LIT:action>' ] <EOL> if '<STR_LIT:type>' in action : <EOL> post_data [ '<STR_LIT>' ] = action [ '<STR_LIT:type>' ] <EOL> if '<STR_LIT>' in action : <EOL> post_data [ '<STR_LIT>' ] = action [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in action : <EOL> post_data [ '<STR_LIT>' ] = action [ '<STR_LIT>' ] <EOL> if '<STR_LIT:url>' in action : <EOL> post_data [ '<STR_LIT>' ] = action [ '<STR_LIT:url>' ] <EOL> if '<STR_LIT>' in action : <EOL> post_data [ '<STR_LIT>' ] = action [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in action : <EOL> post_data [ '<STR_LIT>' ] = action [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in action : <EOL> post_data [ '<STR_LIT>' ] = action [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in action : <EOL> action [ '<STR_LIT>' ] = action . pop ( '<STR_LIT>' ) <EOL> return post_data </s>
