Add an endpoint to update links in DB

This commit is contained in:
Yueran Zhang 2021-03-29 15:48:22 -04:00 committed by Neil Parikh
parent b00093bd2f
commit 9efdb8fdbf
4 changed files with 80 additions and 8 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
venv venv
links.db links.db
password.txt
/.vs /.vs
/.vscode /.vscode

22
backend/data.json Normal file
View File

@ -0,0 +1,22 @@
[
{
"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,15 +1,22 @@
from flask import Flask, request from flask import Flask, request
app = Flask(__name__) from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
import json import json
import sqlite3 import sqlite3
import os import os
DB_PATH = os.path.join(os.path.dirname(__file__), 'links.db') DB_PATH = os.path.join(os.path.dirname(__file__), 'links.db')
@app.route('/') app = Flask(__name__)
def hello_world(): auth = HTTPBasicAuth()
return 'Hello from backend!'
f = open("password.txt","r")
pwd = f.readline().rstrip("\n")
f.close()
users = {
"admin": generate_password_hash(pwd),
}
def regen_JSON(): def regen_JSON():
"""Gets links from DB and outputs them in JSON""" """Gets links from DB and outputs them in JSON"""
con = sqlite3.connect(DB_PATH) con = sqlite3.connect(DB_PATH)
@ -26,6 +33,47 @@ def regen_JSON():
con.close() con.close()
return links_json return links_json
@auth.verify_password
def verify_password(username, password):
if username in users and \
check_password_hash(users.get(username), password):
return username
@app.route('/editor/links', methods = ['POST'])
@auth.login_required
def update_links():
con = sqlite3.connect(DB_PATH)
cur = con.cursor()
try:
cur.execute("begin")
cur.execute('DELETE FROM links')
links = []
data = request.json['links']
items = 'url', 'name', 'clicks', 'position'
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
url = data[i]['url']
name = data[i]['name']
clicks = data[i]['clicks']
position = data[i]['position'] # TODO
newlink = (url, name, clicks, position)
links.append(newlink)
cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links)
con.commit()
data = regen_JSON()
outfile = open('data.json', 'w')
print(data, file=outfile)
f.close()
except:
cur.execute("rollback")
return "done updating links"
@app.route('/clicks', methods=['POST']) @app.route('/clicks', methods=['POST'])
def update_clicks(): def update_clicks():
if ('url' not in request.json or 'name' not in request.json): if ('url' not in request.json or 'name' not in request.json):
@ -41,4 +89,4 @@ def update_clicks():
return 'ok' return 'ok'
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True) app.run(debug=True)

View File

@ -17,3 +17,4 @@ types-typing-extensions==3.7.2
types-Werkzeug==0.1.1 types-Werkzeug==0.1.1
Werkzeug==1.0.1 Werkzeug==1.0.1
wrapt==1.12.1 wrapt==1.12.1
Flask-HTTPAuth==4.2.0