Lots of changed. merging csv upload with main options page.
This commit is contained in:
208
app/routes.py
208
app/routes.py
@ -8,6 +8,9 @@ from app import app
|
||||
from flask import redirect, flash, request, render_template, session, make_response
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
# Global Variables
|
||||
url = "https://api.northpass.com/"
|
||||
|
||||
# Upload folder
|
||||
UPLOAD_FOLDER = '/Users/normrasmussen/Documents/Projects/CSM_webapp/app/static/files'
|
||||
# UPLOAD_FOLDER = 'static/files'
|
||||
@ -15,10 +18,17 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||
ALLOWED_EXTENSIONS = {'csv'}
|
||||
|
||||
|
||||
def download_csv():
|
||||
if request.method == "GET":
|
||||
download = make_response(session["dfcsv"])
|
||||
download.headers["Content-Disposition"] = "attachment; filename=export.csv"
|
||||
download.headers["Content-Type"] = "text/csv"
|
||||
return download
|
||||
|
||||
def key_response(response):
|
||||
if "402" in str(response):
|
||||
error = response.text
|
||||
return render_template("index.html", title="Error Home", error=error)
|
||||
return render_template("index.html", title="Error Home", errors=error)
|
||||
if "401" in str(response):
|
||||
error = [
|
||||
"Unauthorized access error.",
|
||||
@ -26,7 +36,7 @@ def key_response(response):
|
||||
"such as the key being changed.",
|
||||
"Remember, they are paired to each educator!",
|
||||
]
|
||||
return render_template("index.html", title="Error Home", error=error)
|
||||
return render_template("index.html", title="Error Home", errors=error)
|
||||
return correct_key(response)
|
||||
|
||||
|
||||
@ -36,33 +46,55 @@ def correct_key(response):
|
||||
print(session["school"])
|
||||
return render_template("options.html", title="Options")
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
|
||||
@app.route("/dev", methods=["GET", "POST"])
|
||||
def dev_test():
|
||||
return render_template("options.html", title="Dev Test")
|
||||
# TODO: Remove header for main page.
|
||||
# TODO: Leave boxes but change outcome depending if file has been uploaded.
|
||||
'''
|
||||
So create a session['file'] variable with the recently uploaded file name.
|
||||
Then, when someone clicks one of the buttons,
|
||||
after that if request == "POST",
|
||||
create a secondary if statement for if file == session['file'],
|
||||
directly upload emails etc else,
|
||||
bring to the secondary pages already created and
|
||||
allow them to copy and paste.
|
||||
'''
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
def ask_key():
|
||||
"""This is the main function that asks for the API Key.
|
||||
Without this key, no other functions will work.
|
||||
It also assigns the api key to the session and clears the session upon each reload.
|
||||
"""
|
||||
session.clear()
|
||||
while session["key"]:
|
||||
return render_template("options.html", title="Options Home")
|
||||
if request.method == "POST":
|
||||
session["key"] = request.form.get("apikey")
|
||||
#if re.search(r"\s", session["key"]):
|
||||
# error = "Hm. That doesn't seem right"
|
||||
# return render_template("index.html", title="Home", error=error)
|
||||
# return render_template("index.html", title="Home", errors=error)
|
||||
if session["key"] is not None and len(session["key"]) > 10:
|
||||
url = "https://api.northpass.com/v2/properties/school"
|
||||
endpoint = "/v2/properties/school"
|
||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
||||
response = requests.get(url, headers=headers)
|
||||
response = requests.get(url+endpoint, headers=headers)
|
||||
return key_response(response)
|
||||
error = "Hm. That doesn't seem right"
|
||||
return render_template("index.html", title="Home", error=error)
|
||||
|
||||
return render_template("index.html", title="Home")
|
||||
|
||||
def allowed_file(filename):
|
||||
return '.' in filename and \
|
||||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
@app.route("/options", methods=["GET", "POST"])
|
||||
def render_home():
|
||||
if session['key'] is not None:
|
||||
return render_template("options.html", title="Home")
|
||||
return render_template("index.html", title="Enter Key")
|
||||
|
||||
|
||||
@app.route("/csv", methods=["GET", "POST"])
|
||||
def parse_csv():
|
||||
@ -77,33 +109,22 @@ def parse_csv():
|
||||
return redirect(request.url)
|
||||
if file and allowed_file(file.filename):
|
||||
filename = secure_filename(file.filename)
|
||||
session['file'] = filename
|
||||
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
||||
file_path = session['filepath']
|
||||
file.save(file_path)
|
||||
csvData = pd.read_csv(file_path, usecols = ['Email'], index_col=False)
|
||||
html_data = csvData.to_html()
|
||||
return render_template("csv.html", table=html_data, title="Uploaded File")
|
||||
# TODO: Figure out how to delete the file after use.
|
||||
return render_template("csv.html", title="Upload")
|
||||
|
||||
|
||||
@app.route("/", methods=["GET", "POST"])
|
||||
def render_home():
|
||||
return render_template("index.html", title="Home")
|
||||
|
||||
|
||||
@app.route("/table")
|
||||
def table():
|
||||
return render_template("table.html", tables=[session["dfhtml"]], titles=["Table"])
|
||||
|
||||
|
||||
@app.route("/downloadcsv", methods=["GET", "POST"])
|
||||
def download_csv():
|
||||
if request.method == "GET":
|
||||
download = make_response(session["dfcsv"])
|
||||
download.headers["Content-Disposition"] = "attachment; filename=export.csv"
|
||||
download.headers["Content-Type"] = "text/csv"
|
||||
return download
|
||||
|
||||
|
||||
@app.route("/get_courses", methods=["GET", "POST"])
|
||||
def get_courses():
|
||||
array = []
|
||||
@ -115,9 +136,9 @@ def get_courses():
|
||||
if request.method == "POST":
|
||||
while True:
|
||||
count += 1
|
||||
url = f"https://api.northpass.com/v2/courses?page={count}"
|
||||
endpoint = f"v2/courses?page={count}"
|
||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
||||
response = requests.get(url, headers=headers)
|
||||
response = requests.get(url+endpoint, headers=headers)
|
||||
data = response.json()
|
||||
nextlink = data["links"]
|
||||
|
||||
@ -128,11 +149,11 @@ def get_courses():
|
||||
course_dict[keys] = values
|
||||
array.append(course_dict)
|
||||
dataframe = pd.DataFrame(array).drop(
|
||||
["list_image_url", "permalink"], axis=1
|
||||
)
|
||||
["list_image_url", "permalink"], axis=1
|
||||
)
|
||||
dataframe["full_description"] = dataframe[
|
||||
"full_description"
|
||||
].str.replace(r"<[^<>]*>", "", regex=True)
|
||||
"full_description"
|
||||
].str.replace(r"<[^<>]*>", "", regex=True)
|
||||
print(dataframe)
|
||||
|
||||
if "next" not in nextlink:
|
||||
@ -144,7 +165,6 @@ def get_courses():
|
||||
else:
|
||||
return "This isn't working. Let's go our own way."
|
||||
|
||||
|
||||
@app.route("/get_people", methods=["GET", "POST"])
|
||||
def get_people():
|
||||
array = []
|
||||
@ -153,11 +173,12 @@ def get_people():
|
||||
dataframe = pd.DataFrame()
|
||||
|
||||
if request.method == "POST":
|
||||
print("get People POST")
|
||||
while True:
|
||||
count += 1
|
||||
url = f"https://api.northpass.com/v2/people?page={count}"
|
||||
endpoint = f"v2/people?page={count}"
|
||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
||||
response = requests.get(url, headers=headers)
|
||||
response = requests.get(url+endpoint, headers=headers)
|
||||
data = response.json()
|
||||
nextlink = data["links"]
|
||||
|
||||
@ -177,10 +198,9 @@ def get_people():
|
||||
session["dfcsv"] = dataframe.to_csv()
|
||||
return render_template("get.html", table=dfppl, title="List of People")
|
||||
else:
|
||||
return "what what"
|
||||
return render_template("get.html", error="Something went wrong")
|
||||
|
||||
|
||||
@app.route("/add_ppl_opts", methods=["POST"])
|
||||
@app.route("/options", methods=["GET", "POST"])
|
||||
def add_ppl_opts():
|
||||
array = []
|
||||
dict_response = {}
|
||||
@ -188,11 +208,13 @@ def add_ppl_opts():
|
||||
count = 0
|
||||
|
||||
if request.method == "POST":
|
||||
#if session['file']:
|
||||
# print("file exists! uploading data...")
|
||||
while True:
|
||||
count += 1
|
||||
url = f"https://api.northpass.com/v2/groups?page={count}"
|
||||
endpoint = f"v2/groups?page={count}"
|
||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
||||
response = requests.get(url, headers=headers)
|
||||
response = requests.get(url+endpoint, headers=headers)
|
||||
data = response.json()
|
||||
nextlink = data["links"]
|
||||
|
||||
@ -216,52 +238,7 @@ def add_ppl_opts():
|
||||
else:
|
||||
return "This isn't working. Let's go our own way."
|
||||
|
||||
|
||||
@app.route("/bulk_add_ppl", methods=["GET", "POST"])
|
||||
def bulk_add_ppl():
|
||||
if request.method == "POST":
|
||||
emails = request.form.get("emails")
|
||||
groups = request.form.get("groups")
|
||||
emails.split(",")
|
||||
groups.split(",")
|
||||
|
||||
url = "https://api.northpass.com/v2/bulk/people"
|
||||
combinations = list(itertools.product(emails, groups))
|
||||
print(combinations)
|
||||
payload = {
|
||||
"data": {"attributes": {"people": [{"email": emails, "groups": groups}]}}
|
||||
}
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json",
|
||||
"X-Api-Key": session["key"],
|
||||
}
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
response = str(response)
|
||||
if "202" in response:
|
||||
error = "Success! People have been added successfully."
|
||||
return render_template(
|
||||
"bulk_add_ppl.html",
|
||||
table=session["dfgroups"],
|
||||
title="People Added",
|
||||
error=error,
|
||||
)
|
||||
elif "403" in response:
|
||||
error = "Uh oh. Looks like you don't have appropriate privileges."
|
||||
elif "422" in response:
|
||||
error = "Hm. Looks like something was wrong with the names."
|
||||
return render_template(
|
||||
"bulk_add_people.html",
|
||||
table=session["dfgroups"],
|
||||
title="People Added",
|
||||
error=error,
|
||||
)
|
||||
else:
|
||||
error = "Shrug"
|
||||
return render_template("bulk_add_ppl.html", title="Shrug", error=error)
|
||||
|
||||
|
||||
@app.route("/add_groups_opts", methods=["POST"])
|
||||
@app.route("/options", methods=["GET", "POST"])
|
||||
def add_groups_opts():
|
||||
array = []
|
||||
dict_response = {}
|
||||
@ -271,9 +248,9 @@ def add_groups_opts():
|
||||
if request.method == "POST":
|
||||
while True:
|
||||
count += 1
|
||||
url = f"https://api.northpass.com/v2/groups?page={count}"
|
||||
endpoint = f"v2/groups?page={count}"
|
||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
||||
response = requests.get(url, headers=headers)
|
||||
response = requests.get(url+endpoint, headers=headers)
|
||||
data = response.json()
|
||||
nextlink = data["links"]
|
||||
|
||||
@ -297,6 +274,54 @@ def add_groups_opts():
|
||||
else:
|
||||
return "This isn't working. Let's go our own way."
|
||||
|
||||
@app.route("/options", methods=["GET", "POST"])
|
||||
def ppl_to_groups_opts():
|
||||
pass
|
||||
|
||||
|
||||
@app.route("/bulk_add_ppl", methods=["GET", "POST"])
|
||||
def bulk_add_ppl():
|
||||
if request.method == "POST":
|
||||
emails = request.form.get("emails")
|
||||
groups = request.form.get("groups")
|
||||
emails.split(",")
|
||||
groups.split(",")
|
||||
|
||||
endpoint = "v2/bulk/people"
|
||||
combinations = list(itertools.product(emails, groups))
|
||||
print(combinations)
|
||||
payload = {
|
||||
"data": {"attributes": {"people": [{"email": emails, "groups": groups}]}}
|
||||
}
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json",
|
||||
"X-Api-Key": session["key"],
|
||||
}
|
||||
response = requests.post(url+endpoint, json=payload, headers=headers)
|
||||
response = str(response)
|
||||
if "202" in response:
|
||||
error = "Success! People have been added successfully."
|
||||
return render_template(
|
||||
"bulk_add_ppl.html",
|
||||
table=session["dfgroups"],
|
||||
title="People Added",
|
||||
error=error,
|
||||
)
|
||||
elif "403" in response:
|
||||
error = "Uh oh. Looks like you don't have appropriate privileges."
|
||||
elif "422" in response:
|
||||
error = "Hm. Looks like something was wrong with the names."
|
||||
return render_template(
|
||||
"bulk_add_people.html",
|
||||
table=session["dfgroups"],
|
||||
title="People Added",
|
||||
error=error,
|
||||
)
|
||||
else:
|
||||
error = "Shrug"
|
||||
return render_template("bulk_add_ppl.html", title="Shrug", errors=error)
|
||||
|
||||
|
||||
@app.route("/bulk_add_groups", methods=["GET", "POST"])
|
||||
def bulk_add_groups():
|
||||
@ -315,14 +340,14 @@ def bulk_add_groups():
|
||||
groupdict["name"] = group
|
||||
grouparr.append(groupdict)
|
||||
|
||||
url = "https://api.northpass.com/v2/bulk/groups"
|
||||
endpoint = "v2/bulk/groups"
|
||||
payload = {"data": {"attributes": {"groups": grouparr}}}
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json",
|
||||
"X-Api-Key": session["key"],
|
||||
}
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
response = requests.post(url+endpoint, json=payload, headers=headers)
|
||||
print(type(response))
|
||||
response = str(response)
|
||||
if "202" in response:
|
||||
@ -348,19 +373,14 @@ def bulk_add_groups():
|
||||
)
|
||||
else:
|
||||
error = "Shrug"
|
||||
return render_template("bulk_add_groups.html", title="Shrug", error=error)
|
||||
|
||||
|
||||
@app.route("/ppl_to_groups_opts", methods=["GET", "POST"])
|
||||
def ppl_to_groups_opts():
|
||||
pass
|
||||
return render_template("bulk_add_groups.html", title="Shrug", errors=error)
|
||||
|
||||
|
||||
@app.route("/ppl_to_groups", methods=["GET", "POST"])
|
||||
def ppl_to_groups():
|
||||
person_ids = []
|
||||
group_ids = []
|
||||
url = "https://api.northpass.com/v2/bulk/people/membership"
|
||||
endpoint = "v2/bulk/people/membership"
|
||||
payload = {
|
||||
"payload": {
|
||||
"person_ids": person_ids,
|
||||
|
||||
Reference in New Issue
Block a user