import string
# changes a view such that it sends specified POST vars to the context
def view_post_vars_to_context(view):
def view_with_pv2c(request, post_vars_to_context = {}, extra_context={}, *args, **kwargs):
if request.method == "POST":
for k, v in request.POST.items():
if k in post_vars_to_context.keys():
if set(v) <= set(string.letters + string.digits + "-_"): # restrict to alphanumeric, plus _ and -, since POST isn't otherwised cleansed
context_var_name = post_vars_to_context[k]
extra_context[context_var_name] = v
return view(request, extra_context=extra_context, *args, **kwargs)
return view_with_pv2c
Comments