Bulk groups & people changes

This commit is contained in:
Norm Rasmussen
2023-02-22 17:11:14 -05:00
parent 040991ecee
commit d48e982a3f
8 changed files with 370 additions and 71 deletions

189
app/2 Normal file
View File

@ -0,0 +1,189 @@
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()

View File

@ -2,6 +2,7 @@ from app import app
from flask import request, Flask, flash, render_template, session, make_response
import requests
import json
import itertools
import pandas as pd
import re
@ -32,6 +33,7 @@ def render_home():
@app.route("/get_courses", methods=["GET", "POST"])
def get_courses():
array = []
course_dict = {}
df = pd.DataFrame()
tempdf = pd.DataFrame()
pd.set_option("display.max_colwidth", 100)
@ -44,20 +46,24 @@ def get_courses():
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
response = requests.get(url, headers=headers)
jsonresponse = response.json()
dt = jsonresponse["data"]
next = jsonresponse["links"]
data = response.json()
nextlink = data["links"]
for course in dt:
df = df.append(course["attributes"], ignore_index=True)
# df = df.drop("list_image_url", axis=1)
for response in data["data"]:
uuid = response["id"]
course_dict = {"id": uuid}
for keys, values in response["attributes"].items():
course_dict[keys] = values
array.append(course_dict)
dataframe = pd.DataFrame(array).drop("list_image_url", axis=1)
print(dataframe)
if "next" not in next:
if "next" not in nextlink:
break
dfhtml = df.to_html(col_space=5)
session["dfcsv"] = df.to_csv()
return render_template("table.html", tables=dfhtml, titles="Course List")
dfcourse = dataframe.to_html()
session["dfcsv"] = dataframe.to_csv()
return render_template("table.html", table=dfcourse, title="List of Courses")
else:
return "This isn't working. Let's go our own way."
@ -79,6 +85,7 @@ def download_csv():
@app.route("/get_people", methods=["GET", "POST"])
def get_people():
array = []
ppl_dict = {}
df = pd.DataFrame()
x = 0
@ -88,28 +95,32 @@ def get_people():
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)
jsonresponse = response.json()
dt = jsonresponse["data"]
next = jsonresponse["links"]
data = response.json()
nextlink = data["links"]
for person in dt:
print(person)
df = df.append(person["attributes"], ignore_index=True)
for response in data["data"]:
uuid = response["id"]
ppl_dict = {"id": uuid}
for keys, values in response["attributes"].items():
ppl_dict[keys] = values
array.append(ppl_dict)
dataframe = pd.DataFrame(array).drop("custom_avatar_url", axis=1)
print(dataframe)
if "next" not in next:
if "next" not in nextlink:
break
dfppl = df.to_html(col_space=5)
session["dfcsv"] = df.to_csv()
return render_template("table.html", tables=dfppl, titles="People List")
dfppl = dataframe.to_html()
session["dfcsv"] = dataframe.to_csv()
return render_template("table.html", table=dfppl, title="List of People")
else:
return "what what"
@app.route("/add_options", methods=["POST"])
def add_options():
@app.route("/add_ppl_options", methods=["POST"])
def add_ppl_options():
array = []
dict_response = {}
df = pd.DataFrame()
x = 0
if request.method == "POST":
@ -118,48 +129,118 @@ def add_options():
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.text
df = pd.json_normalize(data)
print(df)
data = response.json()
nextlink = data["links"]
# print(type(response))
# print(response)
# jsonresponse = response.json()
# dt = jsonresponse["data"]
# next = jsonresponse["links"]
for response in data["data"]:
uuid = response["id"]
dict_response = {"id": uuid}
for keys, values in response["attributes"].items():
dict_response[keys] = values
array.append(dict_response)
dataframe = pd.DataFrame(array).drop("group_enrollment_link", axis=1)
print(dataframe)
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:
if "next" not in nextlink:
break
print(df)
session["dfcsv"] = df.to_csv()
dfgroups = df.to_html(col_space=5)
dfgroups = dataframe.to_html()
session["dfcsv"] = dataframe.to_csv()
return render_template(
"bulk_add.html", tables=dfgroups, titles="Bulk Add Learners"
"bulk_add_ppl.html", table=dfgroups, titles="Bulk Add Learners"
)
else:
return "This isn't working. Let's go our own way."
@app.route("/bulk_add", methods=["GET", "POST"])
def bulk_add():
@app.route("/bulk_add_ppl", methods=["GET", "POST"])
def bulk_add_ppl():
emailarr = []
grouparr = []
if request.method == "POST":
emails = request.form.get("emails")
groups = request.form.get("groups")
emails = emails.split(",")
groups = 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"],
}
return payload
@app.route("/add_groups_options", methods=["POST"])
def add_groups_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()
nextlink = data["links"]
for response in data["data"]:
uuid = response["id"]
dict_response = {"id": uuid}
for keys, values in response["attributes"].items():
dict_response[keys] = values
array.append(dict_response)
dataframe = pd.DataFrame(array).drop("group_enrollment_link", axis=1)
print(dataframe)
if "next" not in nextlink:
break
dfgroups = dataframe.to_html()
session["dfcsv"] = dataframe.to_csv()
return render_template(
"bulk_add_groups.html", table=dfgroups, titles="Bulk Add Groups"
)
else:
return "This isn't working. Let's go our own way."
@app.route("/bulk_add_groups", methods=["GET", "POST"])
def bulk_add_groups():
grouparr = []
groupdict = {}
i = 0
if request.method == "POST":
groups = request.form.get("groups")
if "\n" in groups:
groups = groups.split("\n")
groups = [group.strip() for group in groups]
elif "," in groups:
groups = groups.split(",")
groups = [group.strip() for group in groups]
for group in groups:
print(group)
groupdict["name"] = group
grouparr.append(groupdict)
print(groupdict)
print(grouparr)
print(grouparr)
url = "https://api.northpass.com/v2/bulk/people"
payload = {"data": {"attributes": {"groups": grouparr}}}
headers = {
"accept": "application/json",
"content-type": "application/json",
"X-Api-Key": session["key"],
}
return payload
app.secret_key = "@&I\x1a?\xce\x94\xbb0w\x17\xbf&Y\xa2\xc2(A\xf5\xf2\x97\xba\xeb\xfa"

View File

@ -58,7 +58,7 @@ select {
--webkit-box-sizing: border-box;
box-sizing: border-box;
}
/*
.dataframe {
font-size: 11pt;
border-collapse: collapse;
@ -72,13 +72,12 @@ select {
}
.dataframe tr:nth-child(even) {
background: #193D4F;
background: #CCE8EE;
}
.dataframe tr:hover {
background: #096F8E;
cursor: pointer;
}*/
background: #CCD4D8;
}
h1 {
font-size: 4rem;

View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
{% extends 'head.html' %}
{% block content %}
<h4>Hello! Please enter Group Names below.</h4>
{% if error %}
<p class=error><strong> </strong>{{ error }}</p>
{% endif %}
<p><label for="Bulk Add Groups"> Please select the appropriate options below.</label></p>
<form action="{{ url_for("bulk_add_groups")}}" method="post">
<p>Please enter a comma separated list of Groups you'd like to add</p>
<textarea id="groups" name="groups" rows="4" cols="50"></textarea>
<p></p>
<input type="submit" value="Submit"></input>
</form>
<h4>Here is a list of your client's Groups.</h4>
<a id="download" href="/downloadcsv">Click here to download as CSV.</a>
</div>
<div class="panda-table">
{{ table | safe }}
</div>
</div>
</body>
{% endblock %}

View File

@ -6,24 +6,23 @@
<p class=error><strong> </strong>{{ error }}</p>
{% endif %}
<p><label for="Bulk Add"> Please select the appropriate options below.</label></p>
<form action="{{ url_for("bulk_add")}}" method="post">
<p><label for="Bulk Add People"> Please select the appropriate options below.</label></p>
<form action="{{ url_for("bulk_add_ppl")}}" method="post">
<p>Please Copy and Paste Emails of learners you'd like to add</p>
<textarea id="emails" name="emails" rows="4" cols="50"></textarea>
<p>Please paste in the Group UUIDs which these learners should be added to.</p>
<textarea id="groups" name="groups" rows="4" cols="50"></textarea>
<option></option>
<input type="submit" value="Submit"></select>
<p></p>
<input type="submit" value="Submit"></input>
</form>
<h4>Here is a list of your client's Groups.</h4>
<a id="download" href="/downloadcsv">Click here to download as CSV.</a>
</div>
<div class="panda-table">
<!-- Display Converted Table -->
<h2> {{ titles }} </h2>
{{ table }}
{{ table | safe }}
</div>
</div>
</div>
</body>
{% endblock %}

View File

@ -12,8 +12,7 @@
style="cursor:pointer;">
<i class="ri-car-line card__icon"></i>
<p class="card__name">Get People</p>
</a>
</form>
</a></form>
<form class="card"
id="get_courses"
@ -24,19 +23,29 @@
style="cursor:pointer;">
<i class="ri-plane-line card__icon"></i>
<p class="card__name">Get Courses</p>
</a>
</form>
</a></form>
<form class="card"
id="add_options"
action="{{ url_for('add_options')}}"
id="add_ppl_options"
action="{{ url_for('add_ppl_options')}}"
method="post">
<a class="a-card"
onclick="document.forms['add_options'].submit()"
onclick="document.forms['add_ppl_options'].submit()"
style="cursor:pointer;">
<i class="ri-send-plane-line card__icon"></i>
<p class="card__name">Bulk Add People</p>
</a>
</a></form>
<form class="card"
id="add_groups_options"
action="{{ url_for('add_groups_options')}}"
method="post">
<a class="a-card"
onclick="document.forms['add_groups_options'].submit()"
style="cursor:pointer;">
<i class="ri-shape-2-line card__icon"></i>
<p class="card__name">Bulk Add Groups</p>
</a></form>
</div>
</body>

View File

@ -1,4 +1,3 @@
<!DOCTYPE html>
{% extends 'head.html' %}
{% block content %}
@ -6,11 +5,7 @@
<a id="download" href="/downloadcsv">Click here to download as CSV.</a>
</div>
<div class="panda-table">
<!-- Display Converted Table -->
{% for table in tables %}
<h2> {{ titles }}</h2>
{{ table | safe }}
{% endfor %}
</div>
</div>
</body>