Ejemplo n.º 1
0
def whitelist_save(request):
    """Save or update whitelist"""
    whitelist_id = int(request.POST.get("data[whitelist_id]", "0"))
    phoneuser_id = int(request.POST.get("data[phoneuser_id]", "0"))
    label = request.POST.get("data[label]", "")
    phonenumber = request.POST.get("data[phonenumber]", "")
    duration = int(request.POST.get("data[duration]", "0"))
    frequency = int(request.POST.get("data[frequency]", "0"))
    real_mobile = int(request.POST.get("data[real_mobile]", "0"))

    # la maschera consente di inserire i minuti
    duration = duration * 60
    action = "Creazione"
    try:
        if whitelist_id:
            whitelist = Whitelist.objects.get(pk=whitelist_id)
            action = "Modifica"
        else:
            whitelist = Whitelist()
            if frequency == 0:
                whitelist.enabled = True

        whitelist.phoneuser_id = phoneuser_id
        whitelist.label = label
        whitelist.phonenumber = phonenumber
        whitelist.duration = duration
        whitelist.frequency = frequency
        whitelist.real_mobile = real_mobile

        whitelist.save()

        # log azione
        audit = Audit()
        audit.log(user=request.user,
            what="%s autorizzazione: %s" % (action, whitelist.phoneuser))
        return whitelist_items(request, phoneuser_id)
    except Exception as e:
        return HttpResponse(status=400, content=json.dumps({'err_msg': format(e)}), content_type='application/json')