def opt_out(request): ''' The view function for opt-out. the request.POST should contain {UserOpts.id:"checked"} for all the UserOpts to be opted out. ''' error_msg = [] profile = UserProfile.get_or_create_profile(request.user) keys = request.POST.keys() if (request.method == "POST"): fv_args = [] for key in keys: try: # check all the keys that are integer values and opt them out. int(key) opt = UserOpts.objects.get(id=key) except: continue ofs = OptsFlowSpace.objects.filter(opt=opt) new_fv_args = opt_fses_outof_exp(ofs) fv_args = fv_args + new_fv_args opt.delete() if len(error_msg) == 0: # Now make sure the priorities are consequative nice_opts = UserOpts.objects.filter(user=request.user, nice=True).order_by('priority') next_priority = profile.max_priority_level - Priority.Strict_Priority_Offset - 1 for opt in nice_opts: if opt.priority != next_priority: opt.priority = next_priority opt.save() new_args = update_match_struct_priority_and_get_fv_args( opt) fv_args = fv_args + new_args next_priority = next_priority - 1 strict_opts = UserOpts.objects.filter( user=request.user, nice=False).order_by('priority') next_priority = profile.max_priority_level - 1 for opt in strict_opts: if opt.priority != next_priority: opt.priority = next_priority opt.save() new_args = update_match_struct_priority_and_get_fv_args( opt) fv_args = fv_args + new_args next_priority = next_priority - 1 try: fv = FVServerProxy.objects.all()[0] try: if len(fv_args) > 0: fv.proxy.api.changeFlowSpace(fv_args) except Exception, e: import traceback traceback.print_exc() transaction.rollback() error_msg.append( "change flowspace in opt_out view failed: %s" % str(e)) except Exception, e: import traceback traceback.print_exc() transaction.rollback() error_msg.append("Flowvisor not set: %s" % str(e))
def opt_out(request): ''' The view function for opt-out. the request.POST should contain {UserOpts.id:"checked"} for all the UserOpts to be opted out. ''' error_msg = [] profile = UserProfile.get_or_create_profile(request.user) keys = request.POST.keys() if (request.method == "POST"): fv_args = [] for key in keys: try: # check all the keys that are integer values and opt them out. int(key) opt = UserOpts.objects.get(id=key) except: continue ofs = OptsFlowSpace.objects.filter(opt=opt) new_fv_args = opt_fses_outof_exp(ofs) fv_args = fv_args + new_fv_args opt.delete() if len(error_msg) == 0: # Now make sure the priorities are consequative nice_opts = UserOpts.objects.filter(user = request.user,nice=True).order_by('priority') next_priority = profile.max_priority_level - Priority.Strict_Priority_Offset - 1 for opt in nice_opts: if opt.priority != next_priority: opt.priority = next_priority opt.save() new_args = update_match_struct_priority_and_get_fv_args(opt) fv_args = fv_args + new_args next_priority = next_priority - 1 strict_opts = UserOpts.objects.filter(user = request.user,nice=False).order_by('priority') next_priority = profile.max_priority_level - 1 for opt in strict_opts: if opt.priority != next_priority: opt.priority = next_priority opt.save() new_args = update_match_struct_priority_and_get_fv_args(opt) fv_args = fv_args + new_args next_priority = next_priority -1 try: fv = FVServerProxy.objects.all()[0] try: if len(fv_args) > 0: fv.proxy.api.changeFlowSpace(fv_args) except Exception,e: import traceback traceback.print_exc() transaction.rollback() error_msg.append("change flowspace in opt_out view failed: %s"%str(e)) except Exception,e: import traceback traceback.print_exc() transaction.rollback() error_msg.append("Flowvisor not set: %s"%str(e))
def change_priority(request): ''' The view function to change priorities of previous opt-ins. request.POST should have a dictionary of "p_" followed by UserOpts database id as key and the new priority as value ''' fv_args = [] error_msg = [] max_priority = request.user.get_profile().max_priority_level profile = UserProfile.get_or_create_profile(request.user) keys = request.POST.keys() if (request.method == "POST"): for key in keys: if key.startswith("p_"): try: pid = int(key[2:len(key)]) new_priority = int(request.POST[key]) except: error_msg.append( "Found invalid pair in request.POST: %s:%s" % (key, request.POST[key])) break if (new_priority > max_priority): error_msg.append("Priority %s, is larger than your maximum(%s)"\ % (new_priority, max_priority)) break u = UserOpts.objects.get(pk=pid) if (u.priority != new_priority): u.priority = new_priority if (u.priority >= max_priority - Priority.Strict_Priority_Offset): u.nice = False else: u.nice = True u.save() new_args = update_match_struct_priority_and_get_fv_args(u) fv_args = fv_args + new_args if len(error_msg) == 0: try: fv = FVServerProxy.objects.all()[0] try: if len(fv_args) > 0: fv.proxy.api.changeFlowSpace(fv_args) return simple.direct_to_template( request, template= 'openflow/optin_manager/opts/change_priority_successful.html', extra_context={}, ) except Exception, e: import traceback traceback.print_exc() transaction.rollback() error_msg.append("update flowspace priorities failed: %s" % str(e)) except Exception, e: import traceback traceback.print_exc() transaction.rollback() error_msg.append("Flowvisor not set: %s" % str(e)) else: #error msg has some content transaction.rollback()
def change_priority(request): ''' The view function to change priorities of previous opt-ins. request.POST should have a dictionary of "p_" followed by UserOpts database id as key and the new priority as value ''' fv_args = [] error_msg = [] max_priority = request.user.get_profile().max_priority_level profile = UserProfile.get_or_create_profile(request.user) keys = request.POST.keys() if (request.method == "POST"): for key in keys: if key.startswith("p_"): try: pid = int(key[2:len(key)]) new_priority = int(request.POST[key]) except: error_msg.append("Found invalid pair in request.POST: %s:%s"%(key,request.POST[key])) break if (new_priority > max_priority): error_msg.append("Priority %s, is larger than your maximum(%s)"\ % (new_priority, max_priority)) break u = UserOpts.objects.get(pk=pid) if (u.priority != new_priority): u.priority = new_priority if (u.priority >= max_priority - Priority.Strict_Priority_Offset): u.nice = False else: u.nice = True u.save() new_args = update_match_struct_priority_and_get_fv_args(u) fv_args = fv_args + new_args if len(error_msg) == 0: try: fv = FVServerProxy.objects.all()[0] try: if len(fv_args) > 0: fv.proxy.api.changeFlowSpace(fv_args) return simple.direct_to_template(request, template = 'openflow/optin_manager/opts/change_priority_successful.html', extra_context = {}, ) except Exception,e: import traceback traceback.print_exc() transaction.rollback() error_msg.append("update flowspace priorities failed: %s"%str(e)) except Exception,e: import traceback traceback.print_exc() transaction.rollback() error_msg.append("Flowvisor not set: %s"%str(e)) else: #error msg has some content transaction.rollback()