Beispiel #1
0
 def post(self):
     data = request.get_json()
     widget = WidgetModel(name=data["name"],
                          color=data["color"],
                          weight=data.get("weight", None))
     db.session.add(widget)
     db.session.commit()
     return widget.json(), 201
Beispiel #2
0
def delete_widget(id):
    widget = WidgetModel.find_by_id(id)
    if widget:
        db.session.delete(widget)
        db.session.commit()
    else:
        print(f"Request to {request.url} found no match -- ignored")
    return redirect(url_for("get_widgets"))
Beispiel #3
0
def post_widgets():
    name = request.form.get("name")
    color = request.form.get("color")
    weight = request.form.get("weight")
    new_widget = WidgetModel(name=name, color=color, weight=weight)
    db.session.add(new_widget)
    db.session.commit()
    return redirect(url_for("get_widgets"))
Beispiel #4
0
 def put(self, id):
     data = request.get_json()
     widget = WidgetModel.find_by_id(id)
     widget.name = data["name"]
     widget.color = data["color"]
     widget.weight = data.get("weight", None)
     db.session.add(widget)
     db.session.commit()
     return widget.json()
Beispiel #5
0
def post_edit_widget(id):
    widget = WidgetModel.find_by_id(id)
    if widget:
        widget.name = request.form.get("name")
        widget.color = request.form.get("color")
        weight = request.form.get("weight", "")
        widget.weight = weight if len(weight) > 0 else None
        db.session.commit()
    else:
        print(f"POST to {request.url} found no matching entry")
    return redirect(url_for("get_widgets"))
Beispiel #6
0
 def delete(self, id):
     widget = WidgetModel.find_by_id(id)
     db.session.delete(widget)
     db.session.commit()
     return widget.json()
Beispiel #7
0
 def get(self, id):
     sort = request.args.get("sort", "id")
     widget = WidgetModel.find_by_id(id)
     return widget.json()
Beispiel #8
0
def get_edit_widget(id):
    widget = WidgetModel.find_by_id(id)
    return render_template("widget_edit.html", widget=widget)