Add an active column

This commit is contained in:
Yueran Zhang 2021-04-02 23:18:05 -04:00 committed by Neil Parikh
parent 66dda06a1d
commit c17dff1792
3 changed files with 16 additions and 13 deletions

1
.gitignore vendored
View File

@ -3,4 +3,5 @@ links.db
password.txt
/.vs
/.vscode
data.json
/links.json

View File

@ -29,7 +29,7 @@ def get_data_from_query(query):
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_list = get_data_from_query('SELECT url, name FROM links WHERE active=1 ORDER BY position')
links_json = json.dumps(links_list, indent=4)
return links_json
@ -41,7 +41,7 @@ def verify_password(username, password):
@app.route('/links', methods = ['GET'])
def get_links():
links_list = get_data_from_query('SELECT name, url FROM links ORDER BY position')
links_list = get_data_from_query('SELECT url, name FROM links WHERE active=1 ORDER BY position')
return jsonify(links_list)
@app.route('/editor/links', methods = ['POST'])
@ -55,7 +55,7 @@ def update_links():
links = []
data = request.json['links']
items = 'url', 'name', 'clicks'
items = 'url', 'name', 'clicks', 'active'
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
@ -63,12 +63,13 @@ def update_links():
url = data[i]['url']
name = data[i]['name']
clicks = data[i]['clicks']
active = data[i]['active']
position = i
newlink = (url, name, clicks, position)
newlink = (url, name, clicks, position, active)
links.append(newlink)
cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links)
cur.executemany('INSERT INTO links VALUES (?,?,?,?,?)', links)
con.commit()
con.close()
data = regen_JSON()
@ -82,14 +83,14 @@ def update_links():
con.close()
raise e
links_list = get_data_from_query('SELECT name, url, clicks FROM links ORDER BY position')
links_list = get_data_from_query('SELECT name, url, clicks, active FROM links ORDER BY position')
return jsonify(links_list)
@app.route('/editor/links', methods = ['GET'])
@auth.login_required
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')
links_list = get_data_from_query('SELECT name, url, clicks, active FROM links ORDER BY position')
return jsonify(links_list)
@app.route('/clicks', methods=['POST'])

View File

@ -7,11 +7,11 @@ con = sqlite3.connect(DB_PATH)
# array of links to store
links = [
('http://csclub.uwaterloo.ca/','CS Club Website',3,0),
('https://www.instagram.com/uwcsclub/','Instagram',4,1),
('https://www.facebook.com/uw.computerscienceclub','Facebook',5,2),
('http://twitch.tv/uwcsclub','Twitch',6,3),
('http://bit.ly/uwcsclub-yt','YouTube',7,4),
('http://csclub.uwaterloo.ca/','CS Club Website',3,0,1),
('https://www.instagram.com/uwcsclub/','Instagram',4,1,1),
('https://www.facebook.com/uw.computerscienceclub','Facebook',5,2,1),
('http://twitch.tv/uwcsclub','Twitch',6,3,1),
('http://bit.ly/uwcsclub-yt','YouTube',7,4,1),
]
# SQLite setup
@ -27,9 +27,10 @@ else:
name text NOT NULL,
clicks int NOT NULL,
position int NOT NULL UNIQUE,
active int NOT NULL,
UNIQUE(url, name)
)''')
cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links)
cur.executemany('INSERT INTO links VALUES (?,?,?,?,?)', links)
con.commit()
con.close()