Compare commits
No commits in common. "5f61ce8c45f420ff1d8812975d3c0b47cb340a0d" and "17b3c46960f33ba7b383ac8b9f3b789c8d468e30" have entirely different histories.
5f61ce8c45
...
17b3c46960
|
|
@ -1 +0,0 @@
|
||||||
__pycache__
|
|
||||||
10
Dockerfile
10
Dockerfile
|
|
@ -1,10 +0,0 @@
|
||||||
FROM python:latest
|
|
||||||
|
|
||||||
WORKDIR /usr/src/app
|
|
||||||
|
|
||||||
COPY requirements.txt ./
|
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
CMD [ "python", "./app.py" ]
|
|
||||||
17
app.py
17
app.py
|
|
@ -1,21 +1,10 @@
|
||||||
from flask import Flask, render_template, jsonify
|
from flask import Flask
|
||||||
from rethinkdb import RethinkDB
|
|
||||||
from routes.conditions import create_conditions_routes
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
# Connect to RethinkDB
|
|
||||||
r = RethinkDB()
|
|
||||||
conn = r.connect('alice', 40002, db='finfree')
|
|
||||||
|
|
||||||
create_conditions_routes(app, r, conn)
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def hello_world():
|
||||||
cursor = r.table("conditions").run(conn)
|
return 'Hello World!'
|
||||||
conditions = list(cursor)
|
|
||||||
|
|
||||||
return render_template('index.html', data={"conditions": conditions})
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=8080)
|
app.run(host='0.0.0.0', port=8080)
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
flask
|
|
||||||
rethinkdb
|
|
||||||
|
|
@ -1,47 +0,0 @@
|
||||||
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 + '/<id>', methods=['GET'])
|
|
||||||
def get_condition(id):
|
|
||||||
condition = r.table(table_name).get(id).run(conn)
|
|
||||||
return jsonify(condition)
|
|
||||||
|
|
||||||
# Update operation
|
|
||||||
@app.route('/' + route + '/<id>', 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 + '/<id>', methods=['DELETE'])
|
|
||||||
def delete_condition(id):
|
|
||||||
r.table(table_name).get(id).delete().run(conn)
|
|
||||||
return jsonify({'message': 'condition deleted successfully'})
|
|
||||||
|
|
@ -1,88 +0,0 @@
|
||||||
body {
|
|
||||||
font-family: Arial, sans-serif;
|
|
||||||
background-color: #f0f0f0;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.container {
|
|
||||||
max-width: 800px;
|
|
||||||
margin: 20px auto;
|
|
||||||
background-color: #fff;
|
|
||||||
padding: 20px;
|
|
||||||
border-radius: 8px;
|
|
||||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1 {
|
|
||||||
margin-top: 0;
|
|
||||||
font-size: 24px;
|
|
||||||
}
|
|
||||||
|
|
||||||
table {
|
|
||||||
width: 100%;
|
|
||||||
border-collapse: collapse;
|
|
||||||
margin-top: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
table,
|
|
||||||
th,
|
|
||||||
td {
|
|
||||||
border: 1px solid #ddd;
|
|
||||||
}
|
|
||||||
|
|
||||||
th,
|
|
||||||
td {
|
|
||||||
padding: 12px;
|
|
||||||
text-align: left;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
background-color: #4CAF50;
|
|
||||||
color: white;
|
|
||||||
border: none;
|
|
||||||
padding: 10px 20px;
|
|
||||||
text-align: center;
|
|
||||||
text-decoration: none;
|
|
||||||
display: inline-block;
|
|
||||||
font-size: 16px;
|
|
||||||
border-radius: 5px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.modal {
|
|
||||||
display: none;
|
|
||||||
position: fixed;
|
|
||||||
z-index: 1;
|
|
||||||
left: 0;
|
|
||||||
top: 0;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
.close {
|
|
||||||
color: #aaa;
|
|
||||||
float: right;
|
|
||||||
font-size: 28px;
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
|
|
||||||
.close:hover,
|
|
||||||
.close:focus {
|
|
||||||
color: black;
|
|
||||||
text-decoration: none;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
@ -1,98 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Paper-Like Website</title>
|
|
||||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
|
|
||||||
|
|
||||||
<div class="container">
|
|
||||||
<h1>Conditions</h1>
|
|
||||||
<button onclick="openModal()">Add Item</button>
|
|
||||||
<table id="dataTable">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>ID</th>
|
|
||||||
<th>Name</th>
|
|
||||||
<th>Description</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<!-- Data will be inserted here dynamically -->
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div id="myModal" class="modal">
|
|
||||||
<div class="modal-content">
|
|
||||||
<span class="close" onclick="closeModal()">×</span>
|
|
||||||
<h2>Add Item</h2>
|
|
||||||
<form id="addItemForm" method="POST" action="/conditions">
|
|
||||||
<label for="itemName">Name:</label><br>
|
|
||||||
<input type="text" id="itemName" name="itemName" required><br>
|
|
||||||
<label for="itemDescription">Description:</label><br>
|
|
||||||
<textarea id="itemDescription" name="itemDescription" required></textarea><br><br>
|
|
||||||
<button type="submit">Submit</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
// Sample data to populate the table
|
|
||||||
const data = `{{ data.conditions | tojson }}`;
|
|
||||||
const sampleData = JSON.parse(data)
|
|
||||||
|
|
||||||
|
|
||||||
// Function to populate the table with data
|
|
||||||
function populateTable(data) {
|
|
||||||
const tableBody = document.querySelector('#dataTable tbody');
|
|
||||||
tableBody.innerHTML = '';
|
|
||||||
data.forEach(item => {
|
|
||||||
const row = `<tr>
|
|
||||||
<td title="${item.id}">${item.id.substr(0,6)}</td>
|
|
||||||
<td>${item.itemName}</td>
|
|
||||||
<td>${item.itemDescription}</td>
|
|
||||||
</tr>`;
|
|
||||||
tableBody.insertAdjacentHTML('beforeend', row);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open modal function
|
|
||||||
function openModal() {
|
|
||||||
document.getElementById('myModal').style.display = 'block';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close modal function
|
|
||||||
function closeModal() {
|
|
||||||
document.getElementById('myModal').style.display = 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle form submission
|
|
||||||
document.getElementById('addItemForm').addEventListener('submit', function (event) {
|
|
||||||
event.preventDefault();
|
|
||||||
const form = event.target.closest('form');
|
|
||||||
const formData = new FormData(form);
|
|
||||||
const json = Array.from(formData.entries()).reduce((acc, [k, v]) => ({...acc, [k]: v}), {});
|
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
xhr.open('POST', form.action, true);
|
|
||||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
|
||||||
xhr.onload = console.log
|
|
||||||
xhr.onerror = console.log
|
|
||||||
xhr.send(JSON.stringify(json));
|
|
||||||
|
|
||||||
closeModal();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Initially populate the table with sample data
|
|
||||||
populateTable(sampleData);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
||||||
Loading…
Reference in New Issue