Add an active column
This commit is contained in:
parent
66dda06a1d
commit
c17dff1792
|
@ -3,4 +3,5 @@ links.db
|
||||||
password.txt
|
password.txt
|
||||||
/.vs
|
/.vs
|
||||||
/.vscode
|
/.vscode
|
||||||
|
data.json
|
||||||
/links.json
|
/links.json
|
||||||
|
|
|
@ -29,7 +29,7 @@ def get_data_from_query(query):
|
||||||
|
|
||||||
def regen_JSON():
|
def regen_JSON():
|
||||||
"""Gets links from DB and outputs them in 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)
|
links_json = json.dumps(links_list, indent=4)
|
||||||
return links_json
|
return links_json
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ def verify_password(username, password):
|
||||||
|
|
||||||
@app.route('/links', methods = ['GET'])
|
@app.route('/links', methods = ['GET'])
|
||||||
def get_links():
|
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)
|
return jsonify(links_list)
|
||||||
|
|
||||||
@app.route('/editor/links', methods = ['POST'])
|
@app.route('/editor/links', methods = ['POST'])
|
||||||
|
@ -55,7 +55,7 @@ def update_links():
|
||||||
|
|
||||||
links = []
|
links = []
|
||||||
data = request.json['links']
|
data = request.json['links']
|
||||||
items = 'url', 'name', 'clicks'
|
items = 'url', 'name', 'clicks', 'active'
|
||||||
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
|
||||||
|
@ -63,12 +63,13 @@ 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']
|
||||||
|
active = data[i]['active']
|
||||||
position = i
|
position = i
|
||||||
|
|
||||||
newlink = (url, name, clicks, position)
|
newlink = (url, name, clicks, position, active)
|
||||||
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()
|
con.close()
|
||||||
data = regen_JSON()
|
data = regen_JSON()
|
||||||
|
@ -82,14 +83,14 @@ def update_links():
|
||||||
con.close()
|
con.close()
|
||||||
raise e
|
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)
|
return jsonify(links_list)
|
||||||
|
|
||||||
@app.route('/editor/links', methods = ['GET'])
|
@app.route('/editor/links', methods = ['GET'])
|
||||||
@auth.login_required
|
@auth.login_required
|
||||||
def get_editor_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."""
|
||||||
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)
|
return jsonify(links_list)
|
||||||
|
|
||||||
@app.route('/clicks', methods=['POST'])
|
@app.route('/clicks', methods=['POST'])
|
||||||
|
|
|
@ -7,11 +7,11 @@ con = sqlite3.connect(DB_PATH)
|
||||||
|
|
||||||
# array of links to store
|
# array of links to store
|
||||||
links = [
|
links = [
|
||||||
('http://csclub.uwaterloo.ca/','CS Club Website',3,0),
|
('http://csclub.uwaterloo.ca/','CS Club Website',3,0,1),
|
||||||
('https://www.instagram.com/uwcsclub/','Instagram',4,1),
|
('https://www.instagram.com/uwcsclub/','Instagram',4,1,1),
|
||||||
('https://www.facebook.com/uw.computerscienceclub','Facebook',5,2),
|
('https://www.facebook.com/uw.computerscienceclub','Facebook',5,2,1),
|
||||||
('http://twitch.tv/uwcsclub','Twitch',6,3),
|
('http://twitch.tv/uwcsclub','Twitch',6,3,1),
|
||||||
('http://bit.ly/uwcsclub-yt','YouTube',7,4),
|
('http://bit.ly/uwcsclub-yt','YouTube',7,4,1),
|
||||||
]
|
]
|
||||||
|
|
||||||
# SQLite setup
|
# SQLite setup
|
||||||
|
@ -27,9 +27,10 @@ else:
|
||||||
name text NOT NULL,
|
name text NOT NULL,
|
||||||
clicks int NOT NULL,
|
clicks int NOT NULL,
|
||||||
position int NOT NULL UNIQUE,
|
position int NOT NULL UNIQUE,
|
||||||
|
active int NOT NULL,
|
||||||
UNIQUE(url, name)
|
UNIQUE(url, name)
|
||||||
)''')
|
)''')
|
||||||
cur.executemany('INSERT INTO links VALUES (?,?,?,?)', links)
|
cur.executemany('INSERT INTO links VALUES (?,?,?,?,?)', links)
|
||||||
con.commit()
|
con.commit()
|
||||||
|
|
||||||
con.close()
|
con.close()
|
Loading…
Reference in New Issue