all crud ops

This commit is contained in:
null
2024-02-27 17:54:36 +01:00
parent 741979aee5
commit 99b78c9bd1
7 changed files with 185 additions and 55 deletions
+23 -7
View File
@@ -1,4 +1,4 @@
from flask import jsonify, request
from flask import jsonify, request, redirect, render_template
import rethinkdb as r
table_name = 'conditions'
@@ -12,6 +12,10 @@ def create_conditions_routes(app, r, conn):
# Routes for CRUD operations
# Create operation
@app.route('/' + route + '/create', methods=['GET'])
def create_form():
return render_template('conditions/create.html')
@app.route('/' + route, methods=['POST'])
def create_condition():
if request.headers['Content-Type'] == 'application/json':
@@ -19,7 +23,7 @@ def create_conditions_routes(app, r, conn):
else: # Assuming form data is in 'application/x-www-form-urlencoded' format
data = request.form.to_dict()
result = r.table(table_name).insert(data).run(conn)
return jsonify({'id': result['generated_keys'][0]}), 201
return redirect(request.referrer or url_for('index'))
# Read operation
@app.route('/' + route, methods=['GET'])
@@ -30,17 +34,29 @@ def create_conditions_routes(app, r, conn):
@app.route('/' + route + '/<id>', methods=['GET'])
def get_condition(id):
condition = r.table(table_name).get(id).run(conn)
return jsonify(condition)
cursor = r.table(table_name).get(id).run(conn)
json = jsonify(cursor)
if request.headers.get('Content-Type') == 'application/json':
return json
else:
return render_template('conditions/update.html', data=cursor)
# Update operation
@app.route('/' + route + '/<id>', methods=['PUT'])
@app.route('/' + route + '/<id>', methods=['POST'])
def update_condition(id):
data = request.get_json()
if request.headers['Content-Type'] == 'application/json':
data = request.json
else:
data = request.form.to_dict()
r.table(table_name).get(id).update(data).run(conn)
return jsonify({'message': 'condition updated successfully'})
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(conn)
return jsonify({'message': 'condition deleted successfully'})
@app.route('/' + route + '/<id>', methods=['DELETE'])
def delete_condition(id):
r.table(table_name).get(id).delete().run(conn)