generic tables from dataclasses
This commit is contained in:
@@ -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()
|
||||
})
|
||||
Reference in New Issue
Block a user