diff --git a/app.py b/app.py index 09ff66c..ed19235 100644 --- a/app.py +++ b/app.py @@ -13,17 +13,25 @@ r = RethinkDB() conn = r.connect(rethinkUrl, rethinkPort, db='finfree') create_conditions_routes(app, r, conn) +conditionModel = { + "id": { + "type": "readonly", + "name": "ID" + }, + "itemDescription": { + "type": "string", + "name": "Beschreibung" + }, + "itemName": { + "type": "string", + "name": "Name" + } +} @app.route('/') def index(): cursor = r.table("conditions").run(conn) conditions = list(cursor) - conditionModel = { - "id": "string", - "itemDescription": "string", - "itemName": "string" - } - return render_template('index.html', data={"conditions": { "data": conditions, "model": conditionModel }}) if __name__ == '__main__': diff --git a/routes/conditions.py b/routes/conditions.py index 4460195..eeac343 100644 --- a/routes/conditions.py +++ b/routes/conditions.py @@ -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 + '/', 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 + '/', methods=['PUT']) + @app.route('/' + route + '/', 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 "" # Delete operation + @app.route('/' + route + '/delete/', methods=['POST']) + def delete_post(id): + r.table(table_name).get(id).delete().run(conn) + return jsonify({'message': 'condition deleted successfully'}) + @app.route('/' + route + '/', methods=['DELETE']) def delete_condition(id): r.table(table_name).get(id).delete().run(conn) diff --git a/static/css/styles.css b/static/css/styles.css index 875f641..720eca8 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -26,6 +26,11 @@ table { margin-top: 20px; } +tbody tr:hover { + background-color: #eee; + cursor: pointer; +} + th, td { border: 1px solid #ddd; @@ -50,38 +55,64 @@ button { cursor: pointer; } -.modal { - display: none; - position: fixed; - z-index: 1; - left: 0; - top: 0; +button:hover { + background-color: #3c8d3e; +} + +fieldset { + margin: 0; + padding: 0; + border: none; +} +/* Dialog box */ +dialog { + width: 400px; + height: 420px; + padding: 20px; + border: 1px solid #ccc; + border-radius: 5px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); + overflow: hidden; +} + + /* Labels */ +label { + display: block; + font-weight: bold; + margin-bottom: 5px; + +} + +/* Text inputs */ +input, form div { + padding: 8px; + margin-bottom: 10px; + border: 1px solid #ccc; + border-radius: 3px; + display: block; + font-size: 16; +} + +input { + width: 100%; +} + +form fieldset:last-child { + margin-top: 20px; + text-align: right; +} + +iframe { width: 100%; height: 100%; - overflow: auto; - background-color: rgba(0, 0, 0, 0.4); - padding-top: 60px; } -.modal-content { - background-color: #fefefe; - margin: 5% auto; - padding: 20px; - border: 1px solid #888; - width: 80%; - border-radius: 8px; +button.danger { + margin-top: 50px; + width: 100%; + background-color: #af4c4c; } -.close { - color: #aaa; - float: right; - font-size: 28px; - font-weight: bold; -} - -.close:hover, -.close:focus { - color: black; - text-decoration: none; - cursor: pointer; +button.danger:hover { + background-color: #8d3c3c; } \ No newline at end of file diff --git a/static/js/main.js b/static/js/main.js index e69de29..cb61b64 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -0,0 +1,19 @@ +document.addEventListener('DOMContentLoaded', documentReady, false); +window.onmessage = (event) => { + if (event.data == "close") { + document.querySelector("dialog[open]").close(); + } +} + +function documentReady() { + const iframe = document.querySelector("iframe"); + iframe.onload=(event) => { + const openDialog = document.querySelector("dialog[open]"); + if (openDialog) { + openDialog.close(); + window.location.reload(); + } else { + document.querySelector("dialog").showModal() + } + } +} \ No newline at end of file diff --git a/templates/conditions/create.html b/templates/conditions/create.html new file mode 100644 index 0000000..9884834 --- /dev/null +++ b/templates/conditions/create.html @@ -0,0 +1,29 @@ + + + + + + + + +

Create a condition

+
+
+ + + + + +
+
+ + +
+
+ + \ No newline at end of file diff --git a/templates/conditions/update.html b/templates/conditions/update.html new file mode 100644 index 0000000..ebc2597 --- /dev/null +++ b/templates/conditions/update.html @@ -0,0 +1,35 @@ + + + + + + + + +

Update a condition

+
+
+ +
{{ data.id }}
+ + + + + + +
+
+ + +
+
+
+ +
+ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index df3e713..7c10ba5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,28 +12,20 @@

Conditions

- -
-
-
-
-

- - - -
+ + - {% for key, items in data.conditions.model.items() %} - + {% for key, value in data.conditions.model.items() %} + {% endfor %} {% for item in data.conditions.data %} - + {% for key, value in item.items() %} {% endfor %} @@ -43,7 +35,7 @@
{{ key }}{{ value.name }}
{{ value }}
- +