diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/app.py b/app.py index b98cf3d..49d3812 100644 --- a/app.py +++ b/app.py @@ -1,10 +1,21 @@ -from flask import Flask, render_template +from flask import Flask, render_template, jsonify +from rethinkdb import RethinkDB +from routes.conditions import create_conditions_routes app = Flask(__name__) +# Connect to RethinkDB +r = RethinkDB() +conn = r.connect('alice', 40002, db='finfree') + +create_conditions_routes(app, r, conn) + @app.route('/') def index(): - return render_template('index.html') + cursor = r.table("conditions").run(conn) + conditions = list(cursor) + + return render_template('index.html', data={"conditions": conditions}) if __name__ == '__main__': app.run(host='0.0.0.0', port=8080) diff --git a/requirements.txt b/requirements.txt index 7e10602..216013b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ flask +rethinkdb \ No newline at end of file diff --git a/routes/__init__.py b/routes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/routes/conditions.py b/routes/conditions.py new file mode 100644 index 0000000..4460195 --- /dev/null +++ b/routes/conditions.py @@ -0,0 +1,47 @@ +from flask import jsonify, request +import rethinkdb as r + +table_name = 'conditions' +route = table_name + +def create_conditions_routes(app, r, conn): + # Create a table (if not exists) + if table_name not in r.table_list().run(conn): + r.table_create(table_name).run(conn) + + # Routes for CRUD operations + + # Create operation + @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() + result = r.table(table_name).insert(data).run(conn) + return jsonify({'id': result['generated_keys'][0]}), 201 + + # Read operation + @app.route('/' + route, methods=['GET']) + def get_conditions(): + cursor = r.table(table_name).run(conn) + conditions = list(cursor) + return jsonify(conditions) + + @app.route('/' + route + '/', methods=['GET']) + def get_condition(id): + condition = r.table(table_name).get(id).run(conn) + return jsonify(condition) + + # Update operation + @app.route('/' + route + '/', methods=['PUT']) + def update_condition(id): + data = request.get_json() + r.table(table_name).get(id).update(data).run(conn) + return jsonify({'message': 'condition updated successfully'}) + + # Delete operation + @app.route('/' + route + '/', methods=['DELETE']) + def delete_condition(id): + r.table(table_name).get(id).delete().run(conn) + return jsonify({'message': 'condition deleted successfully'}) diff --git a/templates/index.html b/templates/index.html index d21c5bf..79f2200 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,6 +10,7 @@ +

Conditions

@@ -31,7 +32,7 @@