Exemple #1
0
def index(request):
    if request.method == 'POST':
        p_id = request.POST.get('id', False)
        if p_id:
            name = request.POST.get('name', "")
            try:
                user = Policy.objects.get(name=name)
            except ObjectDoesNotExist:
                user = False
            if user.name == name:
                return HttpResponseRedirect(p_id)
            else:
                return render(
                    request, 'index.html', {
                        'msg':
                        'A user with that ID was not found. Please try again.',
                        'form': form.PolicyForm()
                    })

        else:
            name = request.POST.get('id_name', False)
            view = request.POST.get('viewers', False)
            displen = request.POST.get('viewlen', False)
            delete = request.POST.get('deleted', False)
            datause = request.POST.get('datause', False)
            tracking = request.POST.get('track', False)
            gps = request.POST.get('gps', False)
            if name and view and displen and delete and datause and tracking and gps:
                policy_str = "("
                if view == "ADS":
                    policy_str += "(ADS or PVB or ME) and "
                elif view == "PVB":
                    policy_str += "(PVB or ME) and "
                else:
                    policy_str += "(ME) and "

                if displen == "720":
                    policy_str += "(720 or 168 or 24 or 1) and "
                elif displen == "168":
                    policy_str += "(168 or 24 or 1) and "
                elif displen == "24":
                    policy_str += "(24 or 1) and "
                else:
                    policy_str += "(1) and "

                if datause == "TARGET":
                    policy_str += "(TARGET or STATS or PARTNER or SITE) and "
                elif datause == "PARTNER":
                    policy_str += "(STATS or PARTNER or SITE) and "
                elif datause == "STATS":
                    policy_str += "(STATS or SITE) and "
                else:
                    policy_str += "(SITE) and "

                if delete == "KEEP":
                    policy_str += "(DELETED OR KEEP) and "
                else:
                    policy_str += "(KEEP) and "

                if tracking == "TRACK":
                    policy_str += "(TRACK or NOTRACK) and "
                else:
                    policy_str += "(NOTRACK) and "

                if gps == "LOCATE":
                    policy_str += "(LOCATE or NOLOCATE)"
                else:
                    policy_str += "(NOLOCATE)"

                policy_str += ")"
                print "-------------------------------------------------------"
                print "The user policy is:"
                print policy_str

                p = Policy(name=name, policy=policy_str)
                p.save()
                new_user = Policy.objects.get(name=name)
                p_id = new_user.id
                return HttpResponseRedirect(str(p_id))
            else:
                return render(
                    request, 'index.html', {
                        'msg':
                        'There was a mistake in your form entries. Please try again.',
                        'form': form.PolicyForm()
                    })

    return render(request, 'index.html', {'form': form.PolicyForm()})