changes
This commit is contained in:
parent
ffc1ed9d21
commit
66dda06a1d
|
@ -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)
|
||||
|
||||
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,6 +39,11 @@ def verify_password(username, password):
|
|||
check_password_hash(users.get(username), password):
|
||||
return username
|
||||
|
||||
@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():
|
||||
|
@ -46,7 +55,7 @@ def update_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)):
|
||||
return "Bad request, some items missing from link object", 400
|
||||
|
@ -54,37 +63,33 @@ def update_links():
|
|||
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
|
||||
|
||||
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'])
|
||||
|
|
Loading…
Reference in New Issue