コード例 #1
0
ファイル: views.py プロジェクト: abutcher/loki
def config_status_save(request, bot_id):
    result = ""
    if request.method == "POST":
        master = Master.objects.get(id=bot_id)
        data = request.POST.copy()
        # get a status or create a newone
        if "configid" in data and data["configid"]:
            status = Status.objects.get(id=data["configid"])
            del data["configid"]
        else:
            config = Config.objects.get(id=data["config_type_id"])
            status = Status(master=master, type=config)
            status.save()
            del data["config_type_id"]

        params_2_add = []
        # add and upate params
        status_params = status.params.all()
        for p, v in data.items():
            v = type_sniffer(v)
            param_type = ConfigParam.objects.get(id=p)
            s = status.params.filter(type=param_type)
            if s:
                s = s[0]
                s.val = pickle.dumps(v)
                s.default = v == param_type.loads_default()
                s.save()
            else:
                param = StatusParam(
                    status=status, type=param_type, val=pickle.dumps(v), default=(v == param_type.loads_default())
                )
                params_2_add.append(param)

        if params_2_add:
            status.params = params_2_add
        status.save()
        result = status.id

    return HttpResponse(result)
コード例 #2
0
ファイル: views.py プロジェクト: oppianmatt/django-loki
def config_status_save(request, bot_id):
    result = ''
    if request.method == 'POST':
        master = Master.objects.get(id=bot_id)
        data = request.POST.copy()
        # get a status or create a newone
        if 'configid' in data and data['configid']:
            status = Status.objects.get(id=data['configid'])
            del data['configid']
        else:
            config = Config.objects.get(id=data['config_type_id'])
            status = Status(master=master, type=config)
            status.save()
            del data['config_type_id']

        params_2_add = []
        # update existing params
        for p in status.params.all():
            #TODO: update existing params
            #      only to creating a new one
            #      so just passing for now
            # how: check if default, if changed, save it
            #      then delete the key from the dict
            #      so it's not reprocessed
            #      and add the param to the params 2 add
            pass
        # add new params
        for p, v in data.items():
            param_type = ConfigParam.objects.get(id=p)
            if v != param_type.default:
                param = StatusParam(status=status, type=param_type, val=v)
                params_2_add.append(param)
        status.params = params_2_add
        status.save()
        result = status.id

    return HttpResponse(result)