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
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"))
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"))
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()
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"))
def delete(self, id): widget = WidgetModel.find_by_id(id) db.session.delete(widget) db.session.commit() return widget.json()
def get(self, id): sort = request.args.get("sort", "id") widget = WidgetModel.find_by_id(id) return widget.json()
def get_edit_widget(id): widget = WidgetModel.find_by_id(id) return render_template("widget_edit.html", widget=widget)