Add endpoint to list all attributes (for editor to consume)

This commit is contained in:
Catherine Wan 2021-03-29 18:56:59 -04:00 committed by Neil Parikh
parent 9efdb8fdbf
commit 7389e555fe
3 changed files with 20 additions and 30 deletions

View File

@ -1,22 +0,0 @@
[
{
"url": "http://csclub.uwaterloo.ca/",
"name": "CS Club Website"
},
{
"url": "https://www.instagram.com/uwcsclub/",
"name": "Instagram"
},
{
"url": "https://www.facebook.com/uw.computerscienceclub",
"name": "Facebook"
},
{
"url": "http://twitch.tv/uwcsclub",
"name": "Twitch"
},
{
"url": "http://bit.ly/uwcsclub-yt",
"name": "YouTube"
}
]

View File

@ -1,4 +1,4 @@
from flask import Flask, request
from flask import Flask, request, jsonify
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
import json
@ -9,12 +9,8 @@ DB_PATH = os.path.join(os.path.dirname(__file__), 'links.db')
app = Flask(__name__)
auth = HTTPBasicAuth()
f = open("password.txt","r")
pwd = f.readline().rstrip("\n")
f.close()
users = {
"admin": generate_password_hash(pwd),
"admin": generate_password_hash("test"),
}
def regen_JSON():
@ -68,12 +64,28 @@ def update_links():
data = regen_JSON()
outfile = open('data.json', 'w')
print(data, file=outfile)
f.close()
outfile.close()
except:
cur.execute("rollback")
return "done updating links"
@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()
return jsonify(links_list)
@app.route('/clicks', methods=['POST'])
def update_clicks():
if ('url' not in request.json or 'name' not in request.json):