Ejemplo n.º 1
0
def database():
    from dallinger.experiment_server.experiment_server import Experiment, session

    title = "Database View"
    exp = Experiment(session)
    model_type = request.args.get("model_type")
    if model_type:
        title = "Database View: {}s".format(model_type)
    datatables_options = prep_datatables_options(
        exp.table_data(**request.args.to_dict(flat=False)))
    columns = [
        c.get("name") or c["data"]
        for c in datatables_options.get("columns", []) if c.get("data")
    ]

    # Extend with custom actions
    actions = {
        "extend": "collection",
        "text": "Actions",
        "buttons": [],
    }
    buttons = actions["buttons"]

    exp_actions = exp.dashboard_database_actions()
    for action in exp_actions:
        buttons.append({
            "extend": "route_action",
            "text": action["title"],
            "route_name": action["name"],
        })

    is_sandbox = getattr(recruiters.from_config(get_config()), "is_sandbox",
                         None)
    if is_sandbox is True or is_sandbox is False:
        buttons.append("compensate")
    else:
        is_sandbox = None

    if len(buttons):
        datatables_options["buttons"].append(actions)

    return render_template(
        "dashboard_database.html",
        title=title,
        columns=columns,
        is_sandbox=is_sandbox,
        datatables_options=json.dumps(datatables_options,
                                      default=date_handler,
                                      indent=True),
    )
Ejemplo n.º 2
0
def database_action(route_name):
    from dallinger.experiment_server.experiment_server import Experiment, session

    data = request.json
    exp = Experiment(session)
    if route_name not in {a["name"] for a in exp.dashboard_database_actions()}:
        return error_response(
            error_text="Access to {} not allowed".format(route_name),
            status=403)
    route_func = getattr(exp, route_name, None)
    if route_func is None:
        return error_response(
            error_text="Method {} not found".format(route_name), status=404)
    result = route_func(data)
    session.commit()
    if result.get("message"):
        flash(result["message"], "success")
    return success_response(**result)