generic tables from dataclasses

This commit is contained in:
null
2024-03-07 22:18:27 +01:00
parent a5c01038ca
commit f9743b834c
6 changed files with 105 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
from db import get_connection, getRethinkDB
from dataclasses import fields
from flask import jsonify, request, redirect, render_template
r = getRethinkDB()
def create_routes(cls, app):
print("creating routes for " + cls.__name__)
table_name = cls.get_table_name()
if table_name not in r.table_list().run(get_connection()):
print("creating table " + table_name)
r.table_create(table_name).run(get_connection())
@app.route(cls.get_route_prefix() + "/", methods=['GET'])
def get():
cursor = r.table(table_name).run(get_connection())
items = list(cursor)
return render_template(cls.get_route_prefix() + "/index.html", data={
"title": cls.__name__,
"headers": [field.name for field in fields(cls)],
"items": items,
"prefix": cls.get_route_prefix()
})