refactor to generic classes
This commit is contained in:
parent
ae707720bc
commit
1399cdacca
2
app.py
2
app.py
|
|
@ -3,11 +3,11 @@ from routes.conditions import create_conditions_routes
|
|||
from db import get_connection, getRethinkDB
|
||||
from routes.create import create_routes
|
||||
from models.Abc import Abc
|
||||
from models.Condition import conditions
|
||||
|
||||
app = Flask(__name__)
|
||||
r = getRethinkDB()
|
||||
|
||||
create_conditions_routes(app)
|
||||
create_routes(Abc, app)
|
||||
|
||||
@app.route('/')
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
from dataclasses import dataclass
|
||||
from .Strategy import Strategy
|
||||
|
||||
@dataclass
|
||||
class conditions(Strategy):
|
||||
id: str
|
||||
symbol: str
|
||||
condition: str
|
||||
value: float
|
||||
disabled: bool
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
from flask import jsonify, request, redirect, render_template
|
||||
from db import get_connection, getRethinkDB
|
||||
|
||||
table_name = 'conditions'
|
||||
route = table_name
|
||||
r = getRethinkDB()
|
||||
|
||||
booleans = ["disabled", "fulfilled"]
|
||||
|
||||
def create_conditions_routes(app):
|
||||
# Create a table (if not exists)
|
||||
if table_name not in r.table_list().run(get_connection()):
|
||||
r.table_create(table_name).run(get_connection())
|
||||
|
||||
# Routes for CRUD operations
|
||||
|
||||
# Create operation
|
||||
@app.route('/' + route + '/create', methods=['GET'])
|
||||
def create_form():
|
||||
return render_template(route + '/create.html')
|
||||
|
||||
@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()
|
||||
|
||||
# convert checkboxes to true/false
|
||||
for key in booleans:
|
||||
data[key] = key in request.form
|
||||
|
||||
result = r.table(table_name).insert(data).run(get_connection())
|
||||
return render_template(route + '/create.html')
|
||||
|
||||
# Read operation
|
||||
@app.route('/' + route, methods=['GET'])
|
||||
def get_conditions():
|
||||
cursor = r.table(table_name).run(get_connection())
|
||||
conditions = list(cursor)
|
||||
return jsonify(conditions)
|
||||
|
||||
@app.route('/' + route + '/<id>', methods=['GET'])
|
||||
def get_condition(id):
|
||||
cursor = r.table(table_name).get(id).run(get_connection())
|
||||
json = jsonify(cursor)
|
||||
if request.headers.get('Content-Type') == 'application/json':
|
||||
return json
|
||||
else:
|
||||
return render_template(route + '/update.html', data=cursor)
|
||||
|
||||
# Update operation
|
||||
@app.route('/' + route + '/<id>', methods=['POST'])
|
||||
def update_condition(id):
|
||||
if request.headers['Content-Type'] == 'application/json':
|
||||
data = request.json
|
||||
else:
|
||||
data = request.form.to_dict()
|
||||
|
||||
# convert checkboxes to true/false
|
||||
for key in booleans:
|
||||
data[key] = key in request.form
|
||||
|
||||
r.table(table_name).get(id).update(data).run(get_connection())
|
||||
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(get_connection())
|
||||
return jsonify({'message': 'condition deleted successfully'})
|
||||
|
||||
@app.route('/' + route + '/<id>', methods=['DELETE'])
|
||||
def delete_condition(id):
|
||||
r.table(table_name).get(id).delete().run(get_connection())
|
||||
return jsonify({'message': 'condition deleted successfully'})
|
||||
|
|
@ -16,7 +16,14 @@ def create_routes(cls, app):
|
|||
print("creating table " + table_name)
|
||||
r.table_create(table_name).run(get_connection())
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/", methods=['GET'])
|
||||
get_endpoint = f"{cls.__name__}_get"
|
||||
get_create_endpoint = f"{cls.__name__}_get_create"
|
||||
post_create_endpoint = f"{cls.__name__}_post_create"
|
||||
get_update_endpoint = f"{cls.__name__}_get_update"
|
||||
post_update_endpoint = f"{cls.__name__}_post_update"
|
||||
post_delete_endpoint = f"{cls.__name__}_post_delete"
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/", methods=['GET'], endpoint=get_endpoint)
|
||||
def get():
|
||||
cursor = r.table(table_name).run(get_connection())
|
||||
fields = list(cursor)
|
||||
|
|
@ -29,7 +36,7 @@ def create_routes(cls, app):
|
|||
})
|
||||
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['GET'])
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['GET'], endpoint=get_create_endpoint)
|
||||
def get_create():
|
||||
return render_template(cls.get_template_prefix() + "/create.html", data={
|
||||
"name": cls.__name__,
|
||||
|
|
@ -38,7 +45,7 @@ def create_routes(cls, app):
|
|||
"fields": {field.name: field.type.__name__ for field in fields(cls)}
|
||||
})
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['POST'])
|
||||
@app.route(cls.get_route_prefix() + "/create", methods=['POST'], endpoint=post_create_endpoint)
|
||||
def post_create():
|
||||
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||
data = request.form.to_dict()
|
||||
|
|
@ -51,7 +58,7 @@ def create_routes(cls, app):
|
|||
return "Resource created", 201
|
||||
return "Unsupported Content-Type", 415
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['GET'])
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['GET'], endpoint=get_update_endpoint)
|
||||
def get_update(id):
|
||||
cursor = r.table(table_name).get(id).run(get_connection())
|
||||
|
||||
|
|
@ -65,7 +72,7 @@ def create_routes(cls, app):
|
|||
})
|
||||
|
||||
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['POST'])
|
||||
@app.route(cls.get_route_prefix() + "/update/<id>", methods=['POST'], endpoint=post_update_endpoint)
|
||||
def post_update(id):
|
||||
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||
data = request.form.to_dict()
|
||||
|
|
@ -78,7 +85,7 @@ def create_routes(cls, app):
|
|||
return "Unsupported Content-Type", 415
|
||||
|
||||
|
||||
@app.route(cls.get_route_prefix() + '/delete/<id>', methods=['POST'])
|
||||
@app.route(cls.get_route_prefix() + '/delete/<id>', methods=['POST'], endpoint=post_delete_endpoint)
|
||||
def post_delete(id):
|
||||
if request.headers['Content-Type'] == 'application/x-www-form-urlencoded':
|
||||
r.table(table_name).get(id).delete().run(get_connection())
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ h2 {
|
|||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin-top: 20px;
|
||||
padding-top: 20px;
|
||||
background-color: #fff;
|
||||
}
|
||||
|
||||
tbody tr:hover {
|
||||
|
|
|
|||
|
|
@ -1,42 +0,0 @@
|
|||
<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?submitted">
|
||||
<fieldset>
|
||||
<label for="symbol">Symbol</label>
|
||||
<input type="text" name="symbol">
|
||||
|
||||
<label for="condition">Condition</label>
|
||||
<select name="condition">
|
||||
<option value="gt">Greater than</option>
|
||||
<option value="lt">Lower than</option>
|
||||
</select>
|
||||
|
||||
<label for="value">Value</label>
|
||||
<input type="number" step="any" name="value">
|
||||
|
||||
<label for="disabled">Disabled</label>
|
||||
<input type="checkbox" name="disabled">
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<button value="cancel" formmethod="dialog" onclick="window.top.postMessage('close', '*')">Cancel</button>
|
||||
<button type="submit" >Submit</button>
|
||||
</fieldset>
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
<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 }}?submitted">
|
||||
<fieldset>
|
||||
<label>ID</label>
|
||||
<div>{{ data.id }}</div>
|
||||
|
||||
<label for="symbol">Symbol</label>
|
||||
<input type="text" name="symbol" value="{{ data.symbol }}">
|
||||
|
||||
<label for="condition">Condition</label>
|
||||
<select name="condition">
|
||||
<option value="gt" {% if data.condition=="gt" %}selected{% endif %}>Greater than</option>
|
||||
<option value="lt" {% if data.condition=="lt" %}selected{% endif %}>Lower than</option>
|
||||
</select>
|
||||
|
||||
<label for="value">Value</label>
|
||||
<input type="number" step="any" name="value" value="{{ data.value }}">
|
||||
|
||||
<label for="disabled">Disabled</label>
|
||||
<input type="checkbox" name="disabled" {{ "checked" if data.disabled }}>
|
||||
|
||||
<label for="fulfilled">Fulfilled</label>
|
||||
<input type="checkbox" name="fulfilled" {{ "checked" if data.fulfilled }}>
|
||||
</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 }}?submitted">
|
||||
<button class="danger" type="submit">Delete</button>
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
@ -12,48 +12,7 @@
|
|||
|
||||
<body>
|
||||
<div class="container">
|
||||
<dialog>
|
||||
<iframe frameborder="0"></iframe>
|
||||
</dialog>
|
||||
<table id="conditionTable">
|
||||
<thead>
|
||||
<tr>
|
||||
<td colspan="5"><h2>Conditions</h2></td>
|
||||
</tr>
|
||||
{% if data.conditions %}
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Symbol</th>
|
||||
<th>Condition</th>
|
||||
<th>Value</th>
|
||||
<th>Disabled</th>
|
||||
<th>Fulfilled</th>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<th class="nodata">No Data</th>
|
||||
</tr>
|
||||
{% endif %}
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for item in data.conditions %}
|
||||
<tr onclick="onOpenDialog('/conditions/{{ item.id }}')">
|
||||
|
||||
{% set order = ["id", "symbol", "condition", "value", "disabled", "fulfilled"] %}
|
||||
{% for key in order %}
|
||||
<td>{{ item[key] }}</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<td colspan="6" style="text-align: right;"> <!-- Adjust colspan as per your total columns -->
|
||||
<button onclick="onOpenDialog('/conditions/create')">Add Item</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
<a href="/strategies/abc">Abc</a>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
|
||||
<body>
|
||||
<div class="container">
|
||||
<a href="/">Home</a>
|
||||
<dialog>
|
||||
<iframe frameborder="0"></iframe>
|
||||
</dialog>
|
||||
|
|
|
|||
Loading…
Reference in New Issue