generic update / delete

This commit is contained in:
null
2024-03-08 11:33:21 +01:00
parent 1c22fb7b34
commit 5323204789
4 changed files with 84 additions and 4 deletions
+36 -2
View File
@@ -28,6 +28,7 @@ def create_routes(cls, app):
"order": cls.get_order()
})
@app.route(cls.get_route_prefix() + "/create", methods=['GET'])
def get_create():
return render_template(cls.get_route_prefix() + "/create.html", data={
@@ -47,6 +48,39 @@ def create_routes(cls, app):
data[key] = key in request.form
r.table(table_name).insert(data).run(get_connection())
return 201
return "Resource created", 201
return "Unsupported Content-Type", 415
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['GET'])
def get_update(id):
cursor = r.table(table_name).get(id).run(get_connection())
return render_template(cls.get_route_prefix() + "/update.html", data={
"name": cls.__name__,
"field": cursor,
"fields": {field.name: field.type.__name__ for field in fields(cls)},
"prefix": cls.get_route_prefix(),
"order": cls.get_order(),
"computed": cls.get_computed_properties()
})
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['POST'])
def post_update(id):
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
data = request.form.to_dict()
for key in get_bool_attribute_names(cls):
data[key] = key in request.form
r.table(table_name).get(id).update(data).run(get_connection())
return "Resource " + id + " updated", 204
return "Unsupported Content-Type", 415
@app.route(cls.get_route_prefix() + '/delete/<id>', methods=['POST'])
def post_delete(id):
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
r.table(table_name).get(id).delete().run(get_connection())
return "Resource " + id + " deleted", 204
return "Unsupported Content-Type", 415