changes
This commit is contained in:
parent
ffc1ed9d21
commit
66dda06a1d
|
@ -13,20 +13,24 @@ users = {
|
||||||
"admin": generate_password_hash("test"),
|
"admin": generate_password_hash("test"),
|
||||||
}
|
}
|
||||||
|
|
||||||
def regen_JSON():
|
def get_data_from_query(query):
|
||||||
"""Gets links from DB and outputs them in JSON"""
|
|
||||||
con = sqlite3.connect(DB_PATH)
|
con = sqlite3.connect(DB_PATH)
|
||||||
con.row_factory = sqlite3.Row
|
con.row_factory = sqlite3.Row
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute('SELECT url, name FROM links ORDER BY position')
|
cur.execute(query)
|
||||||
|
|
||||||
links_list = []
|
links_list = []
|
||||||
for row in cur.fetchall():
|
for row in cur.fetchall():
|
||||||
d = dict(zip(row.keys(), row))
|
d = dict(zip(row.keys(), row))
|
||||||
links_list.append(d)
|
links_list.append(d)
|
||||||
links_json = json.dumps(links_list, indent=4)
|
|
||||||
|
|
||||||
con.close()
|
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
|
return links_json
|
||||||
|
|
||||||
@auth.verify_password
|
@auth.verify_password
|
||||||
|
@ -35,6 +39,11 @@ def verify_password(username, password):
|
||||||
check_password_hash(users.get(username), password):
|
check_password_hash(users.get(username), password):
|
||||||
return username
|
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'])
|
@app.route('/editor/links', methods = ['POST'])
|
||||||
@auth.login_required
|
@auth.login_required
|
||||||
def update_links():
|
def update_links():
|
||||||
|
@ -46,7 +55,7 @@ def update_links():
|
||||||
|
|
||||||
links = []
|
links = []
|
||||||
data = request.json['links']
|
data = request.json['links']
|
||||||
items = 'url', 'name', 'clicks', 'position'
|
items = 'url', 'name', 'clicks'
|
||||||
for i in range(len(data)):
|
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
|
return "Bad request, some items missing from link object", 400
|
||||||
|
@ -54,37 +63,33 @@ def update_links():
|
||||||
url = data[i]['url']
|
url = data[i]['url']
|
||||||
name = data[i]['name']
|
name = data[i]['name']
|
||||||
clicks = data[i]['clicks']
|
clicks = data[i]['clicks']
|
||||||
position = data[i]['position'] # TODO
|
position = i
|
||||||
|
|
||||||
newlink = (url, name, clicks, position)
|
newlink = (url, name, clicks, position)
|
||||||
links.append(newlink)
|
links.append(newlink)
|
||||||
|
|
||||||
cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links)
|
cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links)
|
||||||
con.commit()
|
con.commit()
|
||||||
|
con.close()
|
||||||
data = regen_JSON()
|
data = regen_JSON()
|
||||||
# TODO: Trigger a rebuild of the frontend
|
# TODO: Trigger a rebuild of the frontend
|
||||||
outfile = open('data.json', 'w')
|
outfile = open('data.json', 'w')
|
||||||
print(data, file=outfile)
|
print(data, file=outfile)
|
||||||
outfile.close()
|
outfile.close()
|
||||||
|
|
||||||
except:
|
except Exception as e:
|
||||||
cur.execute("rollback")
|
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'])
|
@app.route('/editor/links', methods = ['GET'])
|
||||||
@auth.login_required
|
@auth.login_required
|
||||||
def get_links():
|
def get_editor_links():
|
||||||
# endpoint lists all URLs and clicks, returns json object for editor.
|
"""endpoint lists all URLs and clicks, returns json object for editor."""
|
||||||
con = sqlite3.connect(DB_PATH)
|
links_list = get_data_from_query('SELECT name, url, clicks FROM links ORDER BY position')
|
||||||
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()
|
|
||||||
return jsonify(links_list)
|
return jsonify(links_list)
|
||||||
|
|
||||||
@app.route('/clicks', methods=['POST'])
|
@app.route('/clicks', methods=['POST'])
|
||||||
|
|
Loading…
Reference in New Issue