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

20
app.py
View File

@ -13,17 +13,25 @@ r = RethinkDB()
conn = r.connect(rethinkUrl, rethinkPort, db='finfree') conn = r.connect(rethinkUrl, rethinkPort, db='finfree')
create_conditions_routes(app, r, conn) create_conditions_routes(app, r, conn)
conditionModel = {
"id": {
"type": "readonly",
"name": "ID"
},
"itemDescription": {
"type": "string",
"name": "Beschreibung"
},
"itemName": {
"type": "string",
"name": "Name"
}
}
@app.route('/') @app.route('/')
def index(): def index():
cursor = r.table("conditions").run(conn) cursor = r.table("conditions").run(conn)
conditions = list(cursor) conditions = list(cursor)
conditionModel = {
"id": "string",
"itemDescription": "string",
"itemName": "string"
}
return render_template('index.html', data={"conditions": { "data": conditions, "model": conditionModel }}) return render_template('index.html', data={"conditions": { "data": conditions, "model": conditionModel }})
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,4 +1,4 @@
from flask import jsonify, request from flask import jsonify, request, redirect, render_template
import rethinkdb as r import rethinkdb as r
table_name = 'conditions' table_name = 'conditions'
@ -12,6 +12,10 @@ def create_conditions_routes(app, r, conn):
# Routes for CRUD operations # Routes for CRUD operations
# Create operation # Create operation
@app.route('/' + route + '/create', methods=['GET'])
def create_form():
return render_template('conditions/create.html')
@app.route('/' + route, methods=['POST']) @app.route('/' + route, methods=['POST'])
def create_condition(): def create_condition():
if request.headers['Content-Type'] == 'application/json': 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 else: # Assuming form data is in 'application/x-www-form-urlencoded' format
data = request.form.to_dict() data = request.form.to_dict()
result = r.table(table_name).insert(data).run(conn) 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 # Read operation
@app.route('/' + route, methods=['GET']) @app.route('/' + route, methods=['GET'])
@ -30,17 +34,29 @@ def create_conditions_routes(app, r, conn):
@app.route('/' + route + '/<id>', methods=['GET']) @app.route('/' + route + '/<id>', methods=['GET'])
def get_condition(id): def get_condition(id):
condition = r.table(table_name).get(id).run(conn) cursor = r.table(table_name).get(id).run(conn)
return jsonify(condition) json = jsonify(cursor)
if request.headers.get('Content-Type') == 'application/json':
return json
else:
return render_template('conditions/update.html', data=cursor)
# Update operation # Update operation
@app.route('/' + route + '/<id>', methods=['PUT']) @app.route('/' + route + '/<id>', methods=['POST'])
def update_condition(id): 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) r.table(table_name).get(id).update(data).run(conn)
return jsonify({'message': 'condition updated successfully'}) return "<script>window.close();</script>"
# Delete operation # 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']) @app.route('/' + route + '/<id>', methods=['DELETE'])
def delete_condition(id): def delete_condition(id):
r.table(table_name).get(id).delete().run(conn) r.table(table_name).get(id).delete().run(conn)

View File

@ -26,6 +26,11 @@ table {
margin-top: 20px; margin-top: 20px;
} }
tbody tr:hover {
background-color: #eee;
cursor: pointer;
}
th, th,
td { td {
border: 1px solid #ddd; border: 1px solid #ddd;
@ -50,38 +55,64 @@ button {
cursor: pointer; cursor: pointer;
} }
.modal { button:hover {
display: none; background-color: #3c8d3e;
position: fixed; }
z-index: 1;
left: 0; fieldset {
top: 0; 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%; width: 100%;
height: 100%; height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.4);
padding-top: 60px;
} }
.modal-content { button.danger {
background-color: #fefefe; margin-top: 50px;
margin: 5% auto; width: 100%;
padding: 20px; background-color: #af4c4c;
border: 1px solid #888;
width: 80%;
border-radius: 8px;
} }
.close { button.danger:hover {
color: #aaa; background-color: #8d3c3c;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
} }

View File

@ -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()
}
}
}

View File

@ -0,0 +1,29 @@
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">
<style>
body {
background-color: white;
overflow: hidden;
}
</style>
</head>
<body>
<h2>Create a condition</h2>
<form method="POST" action="/conditions">
<fieldset>
<label for="itemDescription">Beschreibung</label>
<input type="text" name="itemDescription">
<label for="itemName">Name</label>
<input type="text" name="itemName">
</fieldset>
<fieldset>
<button value="cancel" formmethod="dialog" onclick="window.top.postMessage('close', '*')">Cancel</button>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>

View File

@ -0,0 +1,35 @@
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">
<style>
body {
background-color: white;
overflow: hidden;
}
</style>
</head>
<body>
<h2>Update a condition</h2>
<form method="POST" action="/conditions/{{ data.id }}">
<fieldset>
<label>ID</label>
<div>{{ data.id }}</div>
<label for="itemDescription">Beschreibung</label>
<input type="text" name="itemDescription" value="{{ data.itemDescription }}">
<label for="itemName">Name</label>
<input type="text" name="itemName" value="{{ data.itemName }}">
</fieldset>
<fieldset>
<button value="cancel" formmethod="dialog" onclick="window.top.postMessage('close', '*')">Cancel</button>
<button type="submit">Submit</button>
</fieldset>
</form>
<form method="POST" action="/conditions/delete/{{ data.id }}">
<button class="danger" type="submit">Delete</button>
</form>
</body>
</html>

View File

@ -12,28 +12,20 @@
<body> <body>
<div class="container"> <div class="container">
<h1>Conditions</h1> <h1>Conditions</h1>
<dialog id="conditionDialog"> <dialog>
<form method="POST" action="/conditions"> <iframe frameborder="0"></iframe>
<label for="itemName">Name:</label><br>
<input type="text" id="itemName" name="itemName"><br>
<label for="itemDescription">Description:</label><br>
<textarea id="itemDescription" name="itemDescription"></textarea><br><br>
<button value="cancel" formmethod="dialog">Cancel</button>
<button type="submit">Submit</button>
</form>
</dialog> </dialog>
<table id="conditionTable"> <table id="conditionTable">
<thead> <thead>
<tr> <tr>
{% for key, items in data.conditions.model.items() %} {% for key, value in data.conditions.model.items() %}
<th>{{ key }}</th> <th>{{ value.name }}</th>
{% endfor %} {% endfor %}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for item in data.conditions.data %} {% for item in data.conditions.data %}
<tr> <tr onclick="document.querySelector('dialog iframe').contentWindow.location='/conditions/{{ item.id }}'">
{% for key, value in item.items() %} {% for key, value in item.items() %}
<td>{{ value }}</td> <td>{{ value }}</td>
{% endfor %} {% endfor %}
@ -43,7 +35,7 @@
<tfoot> <tfoot>
<tr> <tr>
<td colspan="3" style="text-align: right;"> <!-- Adjust colspan as per your total columns --> <td colspan="3" style="text-align: right;"> <!-- Adjust colspan as per your total columns -->
<button onclick="openModal()">Add Item</button> <button onclick="document.querySelector('dialog iframe').contentWindow.location='/conditions/create'">Add Item</button>
</td> </td>
</tr> </tr>
</tfoot> </tfoot>