From 66dda06a1d131ac497af08fb4e5a7c6d62308e32 Mon Sep 17 00:00:00 2001 From: Neil Parikh Date: Sat, 3 Apr 2021 01:56:12 +0000 Subject: [PATCH] changes --- backend/main.py | 57 +++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/backend/main.py b/backend/main.py index a967899..6551080 100644 --- a/backend/main.py +++ b/backend/main.py @@ -13,20 +13,24 @@ users = { "admin": generate_password_hash("test"), } -def regen_JSON(): - """Gets links from DB and outputs them in JSON""" +def get_data_from_query(query): con = sqlite3.connect(DB_PATH) con.row_factory = sqlite3.Row cur = con.cursor() - cur.execute('SELECT url, name FROM links ORDER BY position') + cur.execute(query) links_list = [] for row in cur.fetchall(): - d = dict(zip(row.keys(), row)) - links_list.append(d) - links_json = json.dumps(links_list, indent=4) + d = dict(zip(row.keys(), row)) + links_list.append(d) con.close() + return links_list + +def regen_JSON(): + """Gets links from DB and outputs them in JSON""" + links_list = get_data_from_query('SELECT url, name FROM links ORDER BY position') + links_json = json.dumps(links_list, indent=4) return links_json @auth.verify_password @@ -35,7 +39,12 @@ def verify_password(username, password): check_password_hash(users.get(username), password): return username -@app.route('/editor/links', methods = ['POST']) +@app.route('/links', methods = ['GET']) +def get_links(): + links_list = get_data_from_query('SELECT name, url FROM links ORDER BY position') + return jsonify(links_list) + +@app.route('/editor/links', methods = ['POST']) @auth.login_required def update_links(): con = sqlite3.connect(DB_PATH) @@ -43,48 +52,44 @@ def update_links(): try: cur.execute("begin") cur.execute('DELETE FROM links') - + links = [] data = request.json['links'] - items = 'url', 'name', 'clicks', 'position' + items = 'url', 'name', 'clicks' for i in range(len(data)): - if not(all(e in data[i] for e in items)): + if not(all(e in data[i] for e in items)): return "Bad request, some items missing from link object", 400 url = data[i]['url'] name = data[i]['name'] clicks = data[i]['clicks'] - position = data[i]['position'] # TODO + position = i newlink = (url, name, clicks, position) links.append(newlink) cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links) con.commit() + con.close() data = regen_JSON() # TODO: Trigger a rebuild of the frontend outfile = open('data.json', 'w') print(data, file=outfile) outfile.close() - except: + except Exception as e: cur.execute("rollback") - return "done updating links" + con.close() + raise e -@app.route('/editor/links', methods = ['GET']) + links_list = get_data_from_query('SELECT name, url, clicks FROM links ORDER BY position') + return jsonify(links_list) + +@app.route('/editor/links', methods = ['GET']) @auth.login_required -def get_links(): - # endpoint lists all URLs and clicks, returns json object for editor. - con = sqlite3.connect(DB_PATH) - cur = con.cursor() - cur.execute('SELECT position, name, url, clicks FROM links ORDER BY position') - - links_list = [] - for row in cur.fetchall(): - d = dict(zip(["position", "name", "url", "clicks"], [row[0], row[1], row[2], row[3]])) - links_list.append(d) - - con.close() +def get_editor_links(): + """endpoint lists all URLs and clicks, returns json object for editor.""" + links_list = get_data_from_query('SELECT name, url, clicks FROM links ORDER BY position') return jsonify(links_list) @app.route('/clicks', methods=['POST'])