From 1399cdacca2a27d6d491102f529f81d9d7b3209f Mon Sep 17 00:00:00 2001 From: null Date: Sat, 9 Mar 2024 20:13:30 +0100 Subject: [PATCH] refactor to generic classes --- app.py | 2 +- models/Condition.py | 11 +++++ routes/conditions.py | 76 -------------------------------- routes/create.py | 19 +++++--- static/css/styles.css | 3 +- templates/conditions/create.html | 42 ------------------ templates/conditions/update.html | 50 --------------------- templates/index.html | 43 +----------------- templates/strategies/index.html | 1 + 9 files changed, 29 insertions(+), 218 deletions(-) create mode 100644 models/Condition.py delete mode 100644 routes/conditions.py delete mode 100644 templates/conditions/create.html delete mode 100644 templates/conditions/update.html diff --git a/app.py b/app.py index 539b90d..a436cb6 100644 --- a/app.py +++ b/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('/') diff --git a/models/Condition.py b/models/Condition.py new file mode 100644 index 0000000..9ad39cc --- /dev/null +++ b/models/Condition.py @@ -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 \ No newline at end of file diff --git a/routes/conditions.py b/routes/conditions.py deleted file mode 100644 index 4c6b293..0000000 --- a/routes/conditions.py +++ /dev/null @@ -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 + '/', 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 + '/', 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 "" - - # Delete operation - @app.route('/' + route + '/delete/', 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 + '/', methods=['DELETE']) - def delete_condition(id): - r.table(table_name).get(id).delete().run(get_connection()) - return jsonify({'message': 'condition deleted successfully'}) diff --git a/routes/create.py b/routes/create.py index 4534dfa..37134bd 100644 --- a/routes/create.py +++ b/routes/create.py @@ -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/", methods=['GET']) + @app.route(cls.get_route_prefix() + "/update/", 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/", methods=['POST']) + @app.route(cls.get_route_prefix() + "/update/", 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/', methods=['POST']) + @app.route(cls.get_route_prefix() + '/delete/', 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()) diff --git a/static/css/styles.css b/static/css/styles.css index a3ecadc..cf5af37 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -27,7 +27,8 @@ h2 { table { width: 100%; border-collapse: collapse; - margin-top: 20px; + padding-top: 20px; + background-color: #fff; } tbody tr:hover { diff --git a/templates/conditions/create.html b/templates/conditions/create.html deleted file mode 100644 index ba7c86e..0000000 --- a/templates/conditions/create.html +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - -

Create a condition

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

Update a condition

-
-
- -
{{ data.id }}
- - - - - - - - - - - - - - - -
-
- - -
-
-
- -
- - - \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 33425f3..9c4ec05 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,48 +12,7 @@
- - - - - - - - - {% if data.conditions %} - - - - - - - - - {% else %} - - - - {% endif %} - - - {% for item in data.conditions %} - - - {% set order = ["id", "symbol", "condition", "value", "disabled", "fulfilled"] %} - {% for key in order %} - - {% endfor %} - - {% endfor %} - - - - - - -

Conditions

IDSymbolConditionValueDisabledFulfilled
No Data
{{ item[key] }}
- -
+ Abc
diff --git a/templates/strategies/index.html b/templates/strategies/index.html index 29f7bdf..7b091c4 100644 --- a/templates/strategies/index.html +++ b/templates/strategies/index.html @@ -12,6 +12,7 @@
+ Home