Пример #1
0
 def route(page):
     endpoint, *args = page.split("/")
     admin_user = current_user.is_admin
     if f"/{endpoint}" not in app.rbac["post_requests"]:
         return jsonify({"alert": "Invalid POST request."})
     if not admin_user and f"/{endpoint}" not in current_user.post_requests:
         return jsonify({"alert": "Error 403 Forbidden."})
     form_type = request.form.get("form_type")
     if request.json:
         kwargs = request.json
     elif form_type:
         form = form_classes[form_type](request.form)
         if not form.validate_on_submit():
             return jsonify({
                 "invalid_form": True,
                 **{
                     "errors": form.errors
                 }
             })
         kwargs = form_postprocessing(form, request.form)
     else:
         kwargs = request.form
     try:
         with db.session_scope():
             result = getattr(app, endpoint)(*args, **kwargs)
     except db.rbac_error:
         return {"alert": "Error 403 - Operation not allowed."}
     except Exception as exc:
         return {"alert": str(exc)}
     return jsonify(result)
Пример #2
0
def route(page):
    endpoint, *args = page.split("/")
    if f"/{endpoint}" not in app.rbac["endpoints"]["POST"]:
        return jsonify({"alert": "Invalid POST request."})
    if f"/{endpoint}" in app.rbac["groups"][current_user.group]["POST"]:
        return jsonify({"alert": "Error 403 Forbidden."})
    form_type = request.form.get("form_type")
    if endpoint in app.json_endpoints:
        result = getattr(app, endpoint)(*args, **request.json)
    elif form_type:
        form = form_classes[form_type](request.form)
        if not form.validate_on_submit():
            return jsonify({"invalid_form": True, **{"errors": form.errors}})
        result = getattr(app,
                         endpoint)(*args,
                                   **form_postprocessing(form, request.form))
    else:
        result = getattr(app, endpoint)(*args)
    try:
        Session.commit()
        return jsonify(result)
    except Exception as exc:
        raise exc
        Session.rollback()
        if app.settings["app"]["config_mode"] == "debug":
            raise
        return jsonify({"alert": handle_exception(str(exc))})
Пример #3
0
 def route(page):
     endpoint, *args = page.split("/")
     if f"/{endpoint}" not in app.rbac["endpoints"]["POST"]:
         return jsonify({"alert": "Invalid POST request."})
     if f"/{endpoint}" in app.rbac["groups"][
             current_user.group]["POST"]:
         return jsonify({"alert": "Error 403 Forbidden."})
     form_type = request.form.get("form_type")
     if endpoint in app.json_endpoints:
         result = getattr(app, endpoint)(*args, **request.json)
     elif form_type:
         form = form_classes[form_type](request.form)
         if not form.validate_on_submit():
             return jsonify({
                 "invalid_form": True,
                 **{
                     "errors": form.errors
                 }
             })
         result = getattr(app, endpoint)(*args, **form_postprocessing(
             form, request.form))
     else:
         result = getattr(app, endpoint)(*args, **request.form)
     try:
         db.session.commit()
         return jsonify(result)
     except Exception as exc:
         raise exc
         db.session.rollback()
         if app.settings["app"]["config_mode"] == "debug":
             raise
         match = search("UNIQUE constraint failed: (\w+).(\w+)",
                        str(exc))
         if match:
             result = (f"There already is a {match.group(1)} "
                       f"with the same {match.group(2)}.")
         else:
             result = str(exc)
         return jsonify({"alert": result})
Пример #4
0
def route(page):
    f, *args = page.split("/")
    if f not in app.valid_post_endpoints:
        return jsonify({"error": "Invalid POST request."})
    form_type = request.form.get("form_type")
    if f in ("table_filtering", "view_filtering", "multiselect_filtering"):
        result = getattr(app, f)(*args, request.form)
    elif form_type:
        form = form_classes[form_type](request.form)
        if not form.validate_on_submit():
            return jsonify({"invalid_form": True, **{"errors": form.errors}})
        result = getattr(app, f)(*args, **form_postprocessing(request.form))
    else:
        result = getattr(app, f)(*args)
    try:
        Session.commit()
        return jsonify(result)
    except Exception as exc:
        raise exc
        Session.rollback()
        if app.config_mode == "Debug":
            raise
        return jsonify({"error": handle_exception(str(exc))})
Пример #5
0
def route(page):
    f, *args = page.split("/")
    if f not in app.json_endpoints + app.form_endpoints:
        return jsonify({"alert": "Invalid POST request."})
    form_type = request.form.get("form_type")
    if f in app.json_endpoints:
        result = getattr(app, f)(*args, **request.json)
    elif form_type:
        form = form_classes[form_type](request.form)
        if not form.validate_on_submit():
            return jsonify({"invalid_form": True, **{"errors": form.errors}})
        result = getattr(app, f)(*args, **form_postprocessing(form, request.form))
    else:
        result = getattr(app, f)(*args)
    try:
        Session.commit()
        return jsonify(result)
    except Exception as exc:
        raise exc
        Session.rollback()
        if app.config["app"]["config_mode"] == "debug":
            raise
        return jsonify({"alert": handle_exception(str(exc))})
Пример #6
0
def route(page: str) -> Response:
    f, *args = page.split("/")
    if f not in controller.valid_post_endpoints:
        return jsonify({"error": "Invalid POST request."})
    form_type = request.form.get("form_type")
    if form_type:
        form = form_classes[form_type](request.form)
        if not form.validate_on_submit():
            return jsonify({"invalid_form": True, **{"errors": form.errors}})
        result = getattr(controller, f)(*args,
                                        **form_postprocessing(request.form))
    elif f == "filtering":
        result = getattr(controller, f)(*args, request.form)
    else:
        result = getattr(controller, f)(*args)
    try:
        Session.commit()
        return jsonify(result)
    except Exception as exc:
        Session.rollback()
        if controller.enms_config_mode == "Debug":
            raise
        return jsonify({"error": handle_exception(str(exc))})