Exemplo n.º 1
0
def update_deployments_form(request, scenario_id):
    if not request.user.is_authenticated(
    ) or not scenario_id or scenario_id < 0:
        return

    xml_file = request.POST['xml']
    json_str = rest_validate(xml_file)
    validation_result = json.loads(json_str)

    valid = True if (validation_result['result'] == 0) else False

    if not valid:
        return HttpResponse(json_str, content_type="application/json")

    model_scenario = ScenarioModel.objects.get(id=scenario_id)
    if model_scenario is None:
        return HttpResponse(json.dumps({'valid': False}),
                            content_type="application/json")

    if request.user != model_scenario.user:
        raise PermissionDenied

    try:
        temp_scenario = Scenario(xml_file)
    except ParseError:
        return HttpResponse(json.dumps({'valid': False}),
                            content_type="application/json")

    extra_data = load_deployments_data(temp_scenario)

    html = render_to_string("ts_om_edit/deployments_list.html", extra_data)

    return HttpResponse(html)
Exemplo n.º 2
0
def update_deployments_form(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    xml_file = request.POST['xml']
    json_str = rest_validate(xml_file)
    validation_result = json.loads(json_str)

    valid = True if (validation_result['result'] == 0) else False

    if not valid:
        return HttpResponse(json_str, content_type="application/json")

    model_scenario = ScenarioModel.objects.get(id=scenario_id)
    if model_scenario is None:
        return HttpResponse(json.dumps({'valid': False}), content_type="application/json")

    if request.user != model_scenario.user:
        raise PermissionDenied

    try:
        temp_scenario = Scenario(xml_file)
    except ParseError:
        return HttpResponse(json.dumps({'valid': False}), content_type="application/json")

    extra_data = load_deployments_data(temp_scenario)

    html = render_to_string("ts_om_edit/deployments_list.html", extra_data)

    return HttpResponse(html)
Exemplo n.º 3
0
def update_interventions_form(request, scenario_id):
    if not request.user.is_authenticated():
        raise PermissionDenied

    xml_file = request.POST['xml']
    json_str = rest_validate(xml_file)
    validation_result = json.loads(json_str)

    valid = True if (validation_result['result'] == 0) else False

    if not valid:
        return HttpResponse(json_str, content_type="application/json")

    model_scenario = get_object_or_404(ScenarioModel, id=scenario_id)

    if request.user != model_scenario.user:
        raise PermissionDenied

    temp_scenario = Scenario(xml_file)

    extra_data = load_interventions_data(temp_scenario)

    html = render_to_string("ts_om_edit/interventions/interventions_list.html", extra_data)

    return HttpResponse(html)
Exemplo n.º 4
0
def update_form(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    xml_file = request.POST["xml"]
    json_str = rest_validate(xml_file)
    validation_result = json.loads(json_str)

    valid = True if (validation_result["result"] == 0) else False

    if not valid:
        return HttpResponse(json_str, content_type="application/json")

    model_scenario = ScenarioModel.objects.get(id=scenario_id)
    if model_scenario is None:
        return HttpResponse(json.dumps({"valid": False}), content_type="application/json")

    if request.user != model_scenario.user:
        raise PermissionDenied

    try:
        temp_scenario = Scenario(xml_file)
    except ParseError:
        return HttpResponse(json.dumps({"valid": False}), content_type="application/json")

    return {"valid": valid, "scenario": temp_scenario}
Exemplo n.º 5
0
    def form_valid(self, form, **kwargs):
        validation_result = json.loads(rest_validate(self.scenario.xml))
        valid = True if (validation_result["result"] == 0) else False

        if not valid:
            self.kwargs["validation_error"] = "Error: Invalid openmalaria xml."

            return super(ScenarioBaseFormView, self).form_invalid(form)

        self.model_scenario.xml = self.scenario.xml

        if not self.request.is_ajax() or json.loads(self.request.POST["save"]):
            self.model_scenario.save()

        if not self.request.is_ajax():
            return super(ScenarioBaseFormView, self).form_valid(form)
        else:
            data = {"xml": kwargs["kwargs"]["xml"]}

            return self.render_to_json_response(data)
Exemplo n.º 6
0
def submit_scenarios(request):
    scenarios_data = {"ok": False, "scenarios": []}

    if not request.user.is_authenticated or not "scenario_ids" in request.POST:
        return HttpResponse(json.dumps(scenarios_data),
                            content_type="application/json")

    scenario_ids = json.loads(request.POST["scenario_ids"])

    if scenario_ids is None or len(scenario_ids) <= 0:
        return HttpResponse(json.dumps(scenarios_data),
                            content_type="application/json")

    for scenario_id in scenario_ids:
        scenarios_data["scenarios"].append({"id": scenario_id, "ok": False})

        scenario = Scenario.objects.get(user=request.user, id=int(scenario_id))

        if not scenario or scenario.new_simulation is not None:
            continue

        json_str = rest_validate(scenario.xml)
        validation_result = json.loads(json_str)

        valid = True if (validation_result['result'] == 0) else False

        if not valid:
            continue

        simulation = submit(scenario)

        if simulation:
            # scenario.simulation = simulation
            # scenario.save()
            scenarios_data["ok"] = True

    # scenarios_data["ok"] = True

    return HttpResponse(json.dumps(scenarios_data),
                        content_type="application/json")
Exemplo n.º 7
0
def save_scenario(request, scenario_id):
    if not request.user.is_authenticated() or not scenario_id or scenario_id < 0:
        return

    xml_file = request.read()
    json_str = rest_validate(xml_file)
    validation_result = json.loads(json_str)

    valid = True if (validation_result['result'] == 0) else False

    if not valid:
        return HttpResponse(json_str, content_type="application/json")

    scenario = Scenario.objects.get(user=request.user, id=int(scenario_id))

    if not scenario:
        return HttpResponse({'saved': False}, content_type="application/json")

    scenario.xml = xml_file
    scenario.save()

    return HttpResponse(json.dumps({'saved': True}), content_type="application/json")
Exemplo n.º 8
0
def submit_scenarios(request):
    scenarios_data = {"ok": False, "scenarios": []}

    if not request.user.is_authenticated() or not "scenario_ids" in request.POST:
        return HttpResponse(json.dumps(scenarios_data), content_type="application/json")

    scenario_ids = json.loads(request.POST["scenario_ids"])

    if scenario_ids is None or len(scenario_ids) <= 0:
        return HttpResponse(json.dumps(scenarios_data), content_type="application/json")

    for scenario_id in scenario_ids:
        scenarios_data["scenarios"].append({"id": scenario_id, "ok": False})

        scenario = Scenario.objects.get(user=request.user, id=int(scenario_id))

        if not scenario or scenario.simulation is not None:
            continue

        json_str = rest_validate(scenario.xml)
        validation_result = json.loads(json_str)

        valid = True if (validation_result['result'] == 0) else False

        if not valid:
            continue

        simulation = submit(request.user.username, scenario.xml)

        if simulation:
            scenario.simulation = simulation
            scenario.save()
            scenarios_data["scenarios"][-1]["ok"] = True

    scenarios_data["ok"] = True

    return HttpResponse(json.dumps(scenarios_data), content_type="application/json")
Exemplo n.º 9
0
    def form_valid(self, form, **kwargs):
        validation_result = json.loads(rest_validate(self.scenario.xml))
        valid = True if (validation_result['result'] == 0) else False

        if not valid:
            self.kwargs['validation_error'] = 'Error: Invalid openmalaria xml.'
            set_notification(self.request, validation_result, DANGER)
            return super(ScenarioBaseFormView, self).form_invalid(form)

        self.model_scenario.xml = self.scenario.xml

        if not self.request.is_ajax() or json.loads(self.request.POST["save"]):
            if not self.model_scenario.new_simulation:
                self.model_scenario.save()
            else:
                # Don't save scenario if it has been submitted already
                set_notification(self.request, "Not saved", INFO)

        if not self.request.is_ajax():
            return super(ScenarioBaseFormView, self).form_valid(form)
        else:
            data = {'xml': kwargs['kwargs']["xml"]}

            return self.render_to_json_response(data)