refactor to generic classes
This commit is contained in:
@@ -1,76 +0,0 @@
|
||||
from flask import jsonify, request, redirect, render_template
|
||||
from db import get_connection, getRethinkDB
|
||||
|
||||
table_name = 'conditions'
|
||||
route = table_name
|
||||
r = getRethinkDB()
|
||||
|
||||
booleans = ["disabled", "fulfilled"]
|
||||
|
||||
def create_conditions_routes(app):
|
||||
# Create a table (if not exists)
|
||||
if table_name not in r.table_list().run(get_connection()):
|
||||
r.table_create(table_name).run(get_connection())
|
||||
|
||||
# Routes for CRUD operations
|
||||
|
||||
# Create operation
|
||||
@app.route('/' + route + '/create', methods=['GET'])
|
||||
def create_form():
|
||||
return render_template(route + '/create.html')
|
||||
|
||||
@app.route('/' + route, methods=['POST'])
|
||||
def create_condition():
|
||||
if request.headers['Content-Type'] == 'application/json':
|
||||
data = request.json
|
||||
else: # Assuming form data is in 'application/x-www-form-urlencoded' format
|
||||
data = request.form.to_dict()
|
||||
|
||||
# convert checkboxes to true/false
|
||||
for key in booleans:
|
||||
data[key] = key in request.form
|
||||
|
||||
result = r.table(table_name).insert(data).run(get_connection())
|
||||
return render_template(route + '/create.html')
|
||||
|
||||
# Read operation
|
||||
@app.route('/' + route, methods=['GET'])
|
||||
def get_conditions():
|
||||
cursor = r.table(table_name).run(get_connection())
|
||||
conditions = list(cursor)
|
||||
return jsonify(conditions)
|
||||
|
||||
@app.route('/' + route + '/<id>', methods=['GET'])
|
||||
def get_condition(id):
|
||||
cursor = r.table(table_name).get(id).run(get_connection())
|
||||
json = jsonify(cursor)
|
||||
if request.headers.get('Content-Type') == 'application/json':
|
||||
return json
|
||||
else:
|
||||
return render_template(route + '/update.html', data=cursor)
|
||||
|
||||
# Update operation
|
||||
@app.route('/' + route + '/<id>', methods=['POST'])
|
||||
def update_condition(id):
|
||||
if request.headers['Content-Type'] == 'application/json':
|
||||
data = request.json
|
||||
else:
|
||||
data = request.form.to_dict()
|
||||
|
||||
# convert checkboxes to true/false
|
||||
for key in booleans:
|
||||
data[key] = key in request.form
|
||||
|
||||
r.table(table_name).get(id).update(data).run(get_connection())
|
||||
return "<script>window.close();</script>"
|
||||
|
||||
# Delete operation
|
||||
@app.route('/' + route + '/delete/<id>', methods=['POST'])
|
||||
def delete_post(id):
|
||||
r.table(table_name).get(id).delete().run(get_connection())
|
||||
return jsonify({'message': 'condition deleted successfully'})
|
||||
|
||||
@app.route('/' + route + '/<id>', methods=['DELETE'])
|
||||
def delete_condition(id):
|
||||
r.table(table_name).get(id).delete().run(get_connection())
|
||||
return jsonify({'message': 'condition deleted successfully'})
|
||||
+13
-6
@@ -16,7 +16,14 @@ def create_routes(cls, app):
|
||||
print("creating table " + table_name)
|
||||
r.table_create(table_name).run(get_connection())
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/", methods=['GET'])
|
||||
get_endpoint = f"{cls.__name__}_get"
|
||||
get_create_endpoint = f"{cls.__name__}_get_create"
|
||||
post_create_endpoint = f"{cls.__name__}_post_create"
|
||||
get_update_endpoint = f"{cls.__name__}_get_update"
|
||||
post_update_endpoint = f"{cls.__name__}_post_update"
|
||||
post_delete_endpoint = f"{cls.__name__}_post_delete"
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/", methods=['GET'], endpoint=get_endpoint)
|
||||
def get():
|
||||
cursor = r.table(table_name).run(get_connection())
|
||||
fields = list(cursor)
|
||||
@@ -29,7 +36,7 @@ def create_routes(cls, app):
|
||||
})
|
||||
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['GET'])
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['GET'], endpoint=get_create_endpoint)
|
||||
def get_create():
|
||||
return render_template(cls.get_template_prefix() + "/create.html", data={
|
||||
"name": cls.__name__,
|
||||
@@ -38,7 +45,7 @@ def create_routes(cls, app):
|
||||
"fields": {field.name: field.type.__name__ for field in fields(cls)}
|
||||
})
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['POST'])
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['POST'], endpoint=post_create_endpoint)
|
||||
def post_create():
|
||||
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||
data = request.form.to_dict()
|
||||
@@ -51,7 +58,7 @@ def create_routes(cls, app):
|
||||
return "Resource created", 201
|
||||
return "Unsupported Content-Type", 415
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['GET'])
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['GET'], endpoint=get_update_endpoint)
|
||||
def get_update(id):
|
||||
cursor = r.table(table_name).get(id).run(get_connection())
|
||||
|
||||
@@ -65,7 +72,7 @@ def create_routes(cls, app):
|
||||
})
|
||||
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['POST'])
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['POST'], endpoint=post_update_endpoint)
|
||||
def post_update(id):
|
||||
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||
data = request.form.to_dict()
|
||||
@@ -78,7 +85,7 @@ def create_routes(cls, app):
|
||||
return "Unsupported Content-Type", 415
|
||||
|
||||
|
||||
@app.route(cls.get_route_prefix() + '/delete/<id>', methods=['POST'])
|
||||
@app.route(cls.get_route_prefix() + '/delete/<id>', methods=['POST'], endpoint=post_delete_endpoint)
|
||||
def post_delete(id):
|
||||
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||
r.table(table_name).get(id).delete().run(get_connection())
|
||||
|
||||
Reference in New Issue
Block a user