Skip to content
Snippets Groups Projects
Commit 228e01c1 authored by Clemens Berteld's avatar Clemens Berteld
Browse files

API works and is finished (?)

parent a2b89b7a
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,7 @@ app.config['TESTING'] = False ...@@ -13,6 +13,7 @@ app.config['TESTING'] = False
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
columns = sql.SQL(' * ')
wheres = sql.SQL('') wheres = sql.SQL('')
values = '' # Gets used in cursor.execute()s' second parameter for safety reasons values = '' # Gets used in cursor.execute()s' second parameter for safety reasons
...@@ -22,6 +23,16 @@ def index(): ...@@ -22,6 +23,16 @@ def index():
for n in [int(station_id)]: for n in [int(station_id)]:
values = (*values, n) # adding n to existing tuple values = (*values, n) # adding n to existing tuple
if 'years' in request.args:
years = request.args['years'].split(',')
years_clean = []
for year in years:
years_clean.append('"' + year + '"')
years_clean = str(years_clean).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')
columns = sql.SQL('id, ') + sql.SQL(years_clean)
# Just for development # Just for development
# if 'country' in request.args: # if 'country' in request.args:
# country = request.args['country'] # country = request.args['country']
...@@ -30,9 +41,9 @@ def index(): ...@@ -30,9 +41,9 @@ def index():
# values = (*values, n) # adding n to existing tuple # values = (*values, n) # adding n to existing tuple
query = sql.SQL("SELECT array_to_json(array_agg(row_to_json(t))) from (" query = sql.SQL("SELECT array_to_json(array_agg(row_to_json(t))) from ("
"SELECT * FROM stations WHERE lon IS NOT NULL " "SELECT {} FROM stations WHERE lon IS NOT NULL "
"{} " "{} "
") t;").format(wheres) ") t;").format(columns, wheres)
with psycopg2.connect(database='temperatures_berteld_morstein', user='postgres', password='postgres', host='localhost', port=5432) as connection: with psycopg2.connect(database='temperatures_berteld_morstein', user='postgres', password='postgres', host='localhost', port=5432) as connection:
with connection.cursor() as cursor: with connection.cursor() as cursor:
......
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