Skip to content
Snippets Groups Projects
Commit b6590097 authored by Sabine Schröder's avatar Sabine Schröder
Browse files

#3: found solution for loading controlled vocabulary only once at start time...

#3: found solution for loading controlled vocabulary only once at start time (with raw database connection); have to put it into place (so that pytests are also running); for now the workaround is still active
parent 934e8e26
No related branches found
No related tags found
No related merge requests found
Pipeline #40288 passed
......@@ -6,7 +6,7 @@ from typing import List
from fastapi import FastAPI, Depends, HTTPException, APIRouter
from sqlalchemy.orm import Session
from toardb.utils.database import ToarDbSession
from toardb.utils.database import ToarDbSession, engine
from toardb.variables import variables
from toardb.contacts import contacts
......@@ -14,6 +14,8 @@ from toardb.stationmeta import stationmeta
from toardb.timeseries import timeseries
from toardb.data import data
from collections import namedtuple
app = FastAPI()
......@@ -31,3 +33,43 @@ app.include_router(stationmeta.router)
app.include_router(timeseries.router)
app.include_router(data.router)
# get the controlled vocabulary from table
def __get_enum_dict(fake_cur, table_name):
fake_cur.execute("select * from "+table_name)
res = fake_cur.fetchall()
Enumdict=namedtuple("Dict",["value","string","display_str"])
enum_dict = []
for entry in res:
enum_dict.append(Enumdict(*entry))
return enum_dict
@app.on_event("startup")
# will be executed before application *starts*
# ==> again: at this point no database connection available!
# (and also all tables are unknown)
# how can I do pytests with this?!?!
# ==> https://fastapi.tiangolo.com/advanced/testing-events/
# I did not try yet, if test database is created before
# startup_event or after (for the letter, the hint in the link
# will not work)
async def startup_event():
fake_conn = engine.raw_connection()
fake_cur = fake_conn.cursor()
RC_vocabulary = __get_enum_dict(fake_cur, "rc_vocabulary")
RS_vocabulary = __get_enum_dict(fake_cur, "rs_vocabulary")
OK_vocabulary = __get_enum_dict(fake_cur, "ok_vocabulary")
DA_vocabulary = __get_enum_dict(fake_cur, "da_vocabulary")
SF_vocabulary = __get_enum_dict(fake_cur, "sf_vocabulary")
AT_vocabulary = __get_enum_dict(fake_cur, "at_vocabulary")
DS_vocabulary = __get_enum_dict(fake_cur, "ds_vocabulary")
MM_vocabulary = __get_enum_dict(fake_cur, "mm_vocabulary")
CZ_vocabulary = __get_enum_dict(fake_cur, "cz_vocabulary")
CV_vocabulary = __get_enum_dict(fake_cur, "cv_vocabulary")
ST_vocabulary = __get_enum_dict(fake_cur, "st_vocabulary")
TA_vocabulary = __get_enum_dict(fake_cur, "ta_vocabulary")
TC_vocabulary = __get_enum_dict(fake_cur, "tc_vocabulary")
TR_vocabulary = __get_enum_dict(fake_cur, "tr_vocabulary")
DL_vocabulary = __get_enum_dict(fake_cur, "dl_vocabulary")
RT_vocabulary = __get_enum_dict(fake_cur, "rt_vocabulary")
DF_vocabulary = __get_enum_dict(fake_cur, "df_vocabulary")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment