Moved table.html as an include instead of on each page. Added the school name to the main options page for clarity and understanding.
This commit is contained in:
189
app/2
189
app/2
@ -1,189 +0,0 @@
|
|||||||
from app import app
|
|
||||||
from flask import request, Flask, flash, render_template, session, make_response
|
|
||||||
import requests
|
|
||||||
import json
|
|
||||||
import pandas as pd
|
|
||||||
import re
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", methods=["GET", "POST"])
|
|
||||||
def ask_key():
|
|
||||||
session.clear()
|
|
||||||
if request.method == "POST":
|
|
||||||
session["key"] = request.form.get("apikey")
|
|
||||||
if re.search("\s", session["key"]):
|
|
||||||
error = "Hm. That doesn't seem right"
|
|
||||||
return render_template("index.html", title="Home", error=error)
|
|
||||||
elif session["key"] is not None and len(session["key"]) > 10:
|
|
||||||
return render_template("options.html", title="Options")
|
|
||||||
else:
|
|
||||||
error = "Hm. That doesn't seem right"
|
|
||||||
return render_template("index.html", title="Home", error=error)
|
|
||||||
|
|
||||||
else:
|
|
||||||
return render_template("index.html", title="Home")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", methods=["GET", "POST"])
|
|
||||||
def render_home():
|
|
||||||
return render_template("index.html", title="Home")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/get_courses", methods=["GET", "POST"])
|
|
||||||
def get_courses():
|
|
||||||
array = []
|
|
||||||
df = pd.DataFrame()
|
|
||||||
tempdf = pd.DataFrame()
|
|
||||||
pd.set_option("display.max_colwidth", 100)
|
|
||||||
x = 0
|
|
||||||
|
|
||||||
if request.method == "POST":
|
|
||||||
while True:
|
|
||||||
x += 1
|
|
||||||
url = f"https://api.northpass.com/v2/courses?page={x}"
|
|
||||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
jsonresponse = response.json()
|
|
||||||
dt = jsonresponse["data"]
|
|
||||||
next = jsonresponse["links"]
|
|
||||||
|
|
||||||
for course in dt:
|
|
||||||
df = df.append(course["attributes"], ignore_index=True)
|
|
||||||
# df = df.drop("list_image_url", axis=1)
|
|
||||||
|
|
||||||
if "next" not in next:
|
|
||||||
break
|
|
||||||
|
|
||||||
dfhtml = df.to_html(col_space=5)
|
|
||||||
session["dfcsv"] = df.to_csv()
|
|
||||||
return render_template("table.html", tables=dfhtml, titles="Course List")
|
|
||||||
|
|
||||||
else:
|
|
||||||
return "This isn't working. Let's go our own way."
|
|
||||||
|
|
||||||
|
|
||||||
@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_people", methods=["GET", "POST"])
|
|
||||||
def get_people():
|
|
||||||
array = []
|
|
||||||
ppl_dict = {}
|
|
||||||
df = pd.DataFrame()
|
|
||||||
x = 0
|
|
||||||
|
|
||||||
if request.method == "POST":
|
|
||||||
while True:
|
|
||||||
x += 1
|
|
||||||
url = f"https://api.northpass.com/v2/people?page={x}"
|
|
||||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
data = response.json()
|
|
||||||
|
|
||||||
for response in data["data"]:
|
|
||||||
print(response)
|
|
||||||
uuid = response["id"]
|
|
||||||
ppl_dict = {"id": uuid}
|
|
||||||
for keys, values in response["attributes"].items():
|
|
||||||
ppl_dict[keys] = values
|
|
||||||
array.append(ppl_dict)
|
|
||||||
|
|
||||||
print(array)
|
|
||||||
|
|
||||||
next = data["links"]
|
|
||||||
if "next" not in next:
|
|
||||||
break
|
|
||||||
|
|
||||||
Table = []
|
|
||||||
for key, value in array.items(): # or .items() in Python 3
|
|
||||||
temp = []
|
|
||||||
temp.extend([key,value]) #Note that this will change depending on the structure of your dictionary
|
|
||||||
Table.append(temp)
|
|
||||||
return render_template("table.html", tables=Table, titles="People List")
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
for person in dt:
|
|
||||||
print(person)
|
|
||||||
df = df.append(person["attributes"], ignore_index=True)
|
|
||||||
|
|
||||||
|
|
||||||
dfppl = df.to_html(col_space=5)
|
|
||||||
session["dfcsv"] = df.to_csv()
|
|
||||||
|
|
||||||
else:
|
|
||||||
return "what what"
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/add_options", methods=["POST"])
|
|
||||||
def add_options():
|
|
||||||
array = []
|
|
||||||
dict_response = {}
|
|
||||||
df = pd.DataFrame()
|
|
||||||
x = 0
|
|
||||||
if request.method == "POST":
|
|
||||||
while True:
|
|
||||||
x += 1
|
|
||||||
url = f"https://api.northpass.com/v2/groups?page={x}"
|
|
||||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
data = response.json()
|
|
||||||
# data = json.loads(data)
|
|
||||||
print(data)
|
|
||||||
|
|
||||||
# print(type(response))
|
|
||||||
# print(response)
|
|
||||||
# jsonresponse = response.json()
|
|
||||||
# dt = jsonresponse["data"]
|
|
||||||
# next = jsonresponse["links"]
|
|
||||||
|
|
||||||
# for group in dt:
|
|
||||||
# df = df.from_dict(group["attributes"], orient="index")
|
|
||||||
# df = df.append(group["id"], ignore_index=True)
|
|
||||||
# df = df.append(group["attributes"], ignore_index=True)
|
|
||||||
|
|
||||||
# if "next" not in next:
|
|
||||||
# break
|
|
||||||
|
|
||||||
session["dfcsv"] = df.to_csv()
|
|
||||||
dfgroups = df.to_html(col_space=5)
|
|
||||||
return render_template(
|
|
||||||
"bulk_add.html", tables=dfgroups, titles="Bulk Add Learners"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/bulk_add", methods=["GET", "POST"])
|
|
||||||
def bulk_add():
|
|
||||||
if request.method == "POST":
|
|
||||||
emails = request.form.get("emails")
|
|
||||||
groups = request.form.get("groups")
|
|
||||||
url = "https://api.northpass.com/v2/bulk/people"
|
|
||||||
|
|
||||||
payload = {
|
|
||||||
"data": {"attributes": {"people": [{"email": emails, "groups": groups}]}}
|
|
||||||
}
|
|
||||||
|
|
||||||
headers = {
|
|
||||||
"accept": "application/json",
|
|
||||||
"content-type": "application/json",
|
|
||||||
"X-Api-Key": session["key"],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
app.secret_key = "@&I\x1a?\xce\x94\xbb0w\x17\xbf&Y\xa2\xc2(A\xf5\xf2\x97\xba\xeb\xfa"
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
ask_key()
|
|
||||||
Binary file not shown.
@ -16,6 +16,12 @@ def ask_key():
|
|||||||
error = "Hm. That doesn't seem right"
|
error = "Hm. That doesn't seem right"
|
||||||
return render_template("index.html", title="Home", error=error)
|
return render_template("index.html", title="Home", error=error)
|
||||||
elif session["key"] is not None and len(session["key"]) > 10:
|
elif session["key"] is not None and len(session["key"]) > 10:
|
||||||
|
url = "https://api.northpass.com/v2/properties/school"
|
||||||
|
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
data = response.json()
|
||||||
|
session["school"] = data["data"]["attributes"]["properties"]["name"]
|
||||||
|
print(session["school"])
|
||||||
return render_template("options.html", title="Options")
|
return render_template("options.html", title="Options")
|
||||||
else:
|
else:
|
||||||
error = "Hm. That doesn't seem right"
|
error = "Hm. That doesn't seem right"
|
||||||
@ -241,12 +247,22 @@ def bulk_add_groups():
|
|||||||
response = str(response)
|
response = str(response)
|
||||||
if "202" in response:
|
if "202" in response:
|
||||||
error = "Success! Groups have been added successfully."
|
error = "Success! Groups have been added successfully."
|
||||||
return render_template("bulk_add_groups.html", table=session["dfgroups"], title="Groups Added", error=error)
|
return render_template(
|
||||||
|
"bulk_add_groups.html",
|
||||||
|
table=session["dfgroups"],
|
||||||
|
title="Groups Added",
|
||||||
|
error=error,
|
||||||
|
)
|
||||||
elif "403" in response:
|
elif "403" in response:
|
||||||
error = "Uh oh. Looks like you're not the admin or don't have appropriate privileges. Please talk to your academy admin."
|
error = "Uh oh. Looks like you're not the admin or don't have appropriate privileges. Please talk to your academy admin."
|
||||||
elif "422" in response:
|
elif "422" in response:
|
||||||
error = "Hm. Looks like something was wrong with the group names. Reach out to the manager of this app."
|
error = "Hm. Looks like something was wrong with the group names. Reach out to the manager of this app."
|
||||||
return render_template("bulk_add_groups.html", table=session["dfgroups"], title="Groups Added", error=error)
|
return render_template(
|
||||||
|
"bulk_add_groups.html",
|
||||||
|
table=session["dfgroups"],
|
||||||
|
title="Groups Added",
|
||||||
|
error=error,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
error = "Shrug"
|
error = "Shrug"
|
||||||
return render_template("bulk_add_groups.html", title="Shrug", error=error)
|
return render_template("bulk_add_groups.html", title="Shrug", error=error)
|
||||||
|
|||||||
@ -14,15 +14,5 @@
|
|||||||
<p></p>
|
<p></p>
|
||||||
<input type="submit" value="Submit"></input>
|
<input type="submit" value="Submit"></input>
|
||||||
</form>
|
</form>
|
||||||
|
{% include 'table.html' %}
|
||||||
<h4>Here is a list of your client's Groups.</h4>
|
{% endblock %}
|
||||||
<a id="download" href="/downloadcsv">Click here to download as CSV.</a>
|
|
||||||
</div>
|
|
||||||
<div class="panda-table">
|
|
||||||
{{ table | safe }}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
{% endblock %}
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,14 +15,5 @@
|
|||||||
<p></p>
|
<p></p>
|
||||||
<input type="submit" value="Submit"></input>
|
<input type="submit" value="Submit"></input>
|
||||||
</form>
|
</form>
|
||||||
|
{% include 'table.html' %}
|
||||||
<h4>Here is a list of your client's Groups.</h4>
|
{% endblock %}
|
||||||
<a id="download" href="/downloadcsv">Click here to download as CSV.</a>
|
|
||||||
</div>
|
|
||||||
<div class="panda-table">
|
|
||||||
{{ table | safe }}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
{% endblock %}
|
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
{% extends 'head.html' %}
|
{% extends 'head.html' %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h4>Hello! Please select what you'd like to do.</h4>
|
|
||||||
|
|
||||||
|
<h4>Hello! Please find the options for {{ session.school }}.</h4>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-grid">
|
<div class="card-grid">
|
||||||
<form class="card"
|
<form class="card"
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
{% extends 'head.html' %}
|
|
||||||
{% block content %}
|
|
||||||
<h4>Hello! Here are your results.</h4>
|
<h4>Hello! Here are your results.</h4>
|
||||||
<a id="download" href="/downloadcsv">Click here to download as CSV.</a>
|
<a id="download" href="/downloadcsv">Click here to download as CSV.</a>
|
||||||
</div>
|
</div>
|
||||||
@ -9,4 +7,3 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
{% endblock %}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user