Finished the manual upload and now working on CSV
This commit is contained in:
Binary file not shown.
291
app/routes.py
291
app/routes.py
@ -3,7 +3,7 @@ import itertools
|
||||
import pandas as pd
|
||||
import re
|
||||
import os
|
||||
import glob
|
||||
import csv
|
||||
from app import app
|
||||
from flask import (
|
||||
redirect,
|
||||
@ -59,6 +59,7 @@ def correct_key(response):
|
||||
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")
|
||||
@ -106,6 +107,10 @@ def clear_session():
|
||||
return render_template("index.html", title="Home, New session")
|
||||
|
||||
|
||||
@app.route("/table")
|
||||
def table():
|
||||
return render_template("table.html", tables=[session["dfhtml"]], titles=["Table"])
|
||||
|
||||
"""
|
||||
uploaded_file = request.files['file']
|
||||
if uploaded_file.filename != '':
|
||||
@ -114,44 +119,99 @@ uploaded_file.save(uploaded_file.filename)
|
||||
return render_template("options.html", title="Home, Now with CSV!")
|
||||
"""
|
||||
|
||||
|
||||
@app.route("/csv", methods=["GET", "POST"])
|
||||
def csv():
|
||||
@app.route("/upload_file", methods=["GET", "POST"])
|
||||
@app.route("/bulk_add", methods=["GET", "POST"])
|
||||
def upload_file():
|
||||
print("Uploading CSV")
|
||||
csvData = pd.DataFrame()
|
||||
if request.method == "POST":
|
||||
if "file" not in request.files:
|
||||
print("file not in request.files")
|
||||
flash("No file found or uploaded")
|
||||
return redirect(request.url)
|
||||
return redirect(url_for("bulk_add_opts"))
|
||||
file = request.files["file"]
|
||||
if file.filename == "":
|
||||
print("no file exists")
|
||||
flash("No file found or uploaded")
|
||||
return redirect(request.url)
|
||||
return redirect(url_for("bulk_add_opts"))
|
||||
# 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)
|
||||
session["filepath"] = file_path
|
||||
file.save(file_path)
|
||||
csvData = pd.read_csv(file_path, usecols=["Email"], index_col=False)
|
||||
html_data = csvData.to_html()
|
||||
# csvData = pd.read_csv(file_path)
|
||||
file = list(csv.reader(open(file_path, "r")))
|
||||
emails = []
|
||||
groups = []
|
||||
for col in file:
|
||||
emails.append(col[0])
|
||||
#groups.append(col(range(1,20)))
|
||||
print(emails)
|
||||
#print(groups)
|
||||
|
||||
#print(emails)
|
||||
# for item in data:
|
||||
# print(item[0])
|
||||
# lines = reader(csvData)
|
||||
# csvData = list(lines)
|
||||
# print(csvData)
|
||||
selection = request.form.get('learner-groups')
|
||||
if selection == "all-groups":
|
||||
if request.form['preview']:
|
||||
return api_csv_all_groups(csvData)
|
||||
elif request.form['submit']:
|
||||
return "Submitted Selection"
|
||||
elif selection == "some-groups":
|
||||
return api_csv_some_groups(csvData)
|
||||
return render_template(
|
||||
"options.html", table=html_data, title="Uploaded File"
|
||||
"bulk_add.html", table=html_data, title="Uploaded File"
|
||||
)
|
||||
# TODO: Figure out how to delete the file after use.
|
||||
print("nothing happened")
|
||||
return render_template("options.html", title="Home, now with a CSV Table!")
|
||||
|
||||
|
||||
@app.route("/table")
|
||||
def table():
|
||||
return render_template("table.html", tables=[session["dfhtml"]], titles=["Table"])
|
||||
def api_csv_all_groups(csvData):
|
||||
# htmlcsv = csvData.to_html()
|
||||
# for items in csvData:
|
||||
|
||||
|
||||
# emails = csvData['Email'].values.tolist()
|
||||
# emails = [nan for nan in emails if str(nan) != 'nan']
|
||||
|
||||
# groups = csvData['Groups'].values.tolist()
|
||||
# groups = [nan for nan in groups if str(nan) != 'nan']
|
||||
|
||||
if emails and groups:
|
||||
return api_add_ppl_groups(emails, groups)
|
||||
elif emails:
|
||||
return api_add_ppl(emails)
|
||||
elif groups:
|
||||
return api_add_groups(groups)
|
||||
return render_template("bulk_add.html", table=htmlcsv, title="CSV Submission")
|
||||
|
||||
def api_csv_some_groups(csvData):
|
||||
htmlcsv = csvData.to_html()
|
||||
|
||||
emails = csvData['Email'].values.tolist()
|
||||
emails = [nan for nan in emails if str(nan) != 'nan']
|
||||
|
||||
groups = csvData['Groups'].values.tolist()
|
||||
groups = [nan for nan in groups if str(nan) != 'nan']
|
||||
|
||||
print(emails)
|
||||
print(groups)
|
||||
|
||||
# print(email)
|
||||
# return groups
|
||||
# row_list = csvData.loc[2, :].values.flatten().tolist()
|
||||
|
||||
return htmlcsv
|
||||
|
||||
@app.route("/bulk_add_opts", methods=["GET", "POST"])
|
||||
def bulk_add_opts():
|
||||
return render_template("bulk_add.html", titles="Bulk Add Options")
|
||||
|
||||
'''
|
||||
array = []
|
||||
dict_response = {}
|
||||
dataframe = pd.DataFrame()
|
||||
@ -159,48 +219,9 @@ def bulk_add_opts():
|
||||
|
||||
if request.method == "POST":
|
||||
if session.get("file"):
|
||||
print("file exists! uploading data...")
|
||||
return "File Exists! Test Complete"
|
||||
else:
|
||||
while True:
|
||||
count += 1
|
||||
endpoint = f"v2/groups?page={count}"
|
||||
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
||||
response = requests.get(url + endpoint, 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.html", table=dfgroups, titles="Bulk Add"
|
||||
)
|
||||
else:
|
||||
return "This isn't working. Let's go our own way."
|
||||
|
||||
|
||||
@app.route("/bulk_add_groups_opts", methods=["GET", "POST"])
|
||||
def bulk_add_groups_opts():
|
||||
array = []
|
||||
dict_response = {}
|
||||
count = 0
|
||||
dataframe = pd.DataFrame()
|
||||
|
||||
if request.method == "POST":
|
||||
pass
|
||||
#print("file exists! uploading data...")
|
||||
#return "File Exists! Test Complete"
|
||||
while True:
|
||||
count += 1
|
||||
endpoint = f"v2/groups?page={count}"
|
||||
@ -215,20 +236,20 @@ def bulk_add_groups_opts():
|
||||
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)
|
||||
dataframe = pd.DataFrame(array).drop(
|
||||
"group_enrollment_link", axis=1
|
||||
)
|
||||
print(dataframe)
|
||||
|
||||
if "next" not in nextlink:
|
||||
break
|
||||
|
||||
session["dfgroups"] = dataframe.to_html()
|
||||
dfgroups = dataframe.to_html()
|
||||
session["dfcsv"] = dataframe.to_csv()
|
||||
return render_template(
|
||||
"bulk_add_groups.html", table=session["dfgroups"], titles="Bulk Add Groups"
|
||||
)
|
||||
return render_template("bulk_add.html", table=dfgroups, titles="Bulk Add")
|
||||
else:
|
||||
return "This isn't working. Let's go our own way."
|
||||
|
||||
'''
|
||||
|
||||
@app.route("/bulk_add", methods=["GET", "POST"])
|
||||
def bulk_add():
|
||||
@ -239,35 +260,36 @@ def bulk_add():
|
||||
if "\n" in emails:
|
||||
emails = emails.split("\n")
|
||||
emails = [email.strip() for email in emails]
|
||||
emails = [re.sub(r'[,]', "", email) for email in emails]
|
||||
emails = [re.sub(r"[,]", "", email) for email in emails]
|
||||
elif "," in emails:
|
||||
emails = emails.split(",")
|
||||
emails = [email.strip() for email in emails]
|
||||
else:
|
||||
emails = []
|
||||
emails.append(emails)
|
||||
emails = emails.split()
|
||||
else:
|
||||
emails = []
|
||||
emails.append(emails)
|
||||
if groups:
|
||||
if "\n" in groups:
|
||||
groups = groups.split("\n")
|
||||
groups = [group.strip() for group in groups]
|
||||
groups = [re.sub(r'[,]', "", group) for group in groups]
|
||||
groups = [re.sub(r"[,]", "", group) for group in groups]
|
||||
elif "," in groups:
|
||||
groups = groups.split(",")
|
||||
groups = [group.strip() for group in groups]
|
||||
else:
|
||||
groups = []
|
||||
groups.append(groups)
|
||||
groups = groups.split()
|
||||
else:
|
||||
groups = []
|
||||
groups.append(groups)
|
||||
|
||||
if emails and groups:
|
||||
print(emails)
|
||||
print(groups)
|
||||
return api_add_ppl_groups(emails, groups)
|
||||
elif emails:
|
||||
return api_add_ppl(emails)
|
||||
elif groups:
|
||||
return api_add_groups(groups)
|
||||
|
||||
return render_template('bulk_add.html')
|
||||
return render_template("bulk_add.html")
|
||||
|
||||
|
||||
# for group in groups:
|
||||
@ -276,57 +298,56 @@ def bulk_add():
|
||||
|
||||
|
||||
def api_add_ppl(emails):
|
||||
print(emails)
|
||||
payload2 = []
|
||||
endpoint = "v2/bulk/people"
|
||||
payload = {
|
||||
"data": {
|
||||
"attributes": {"people": [{"email": emails}]}
|
||||
}
|
||||
}
|
||||
for email in emails:
|
||||
payload2.append({"email": email })
|
||||
payload = {"data": {"attributes": {"people": payload2 }}}
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json",
|
||||
"X-Api-Key": session["key"],
|
||||
}
|
||||
response = requests.post(url + endpoint, json=payload, headers=headers)
|
||||
return check_response(response)
|
||||
return payload
|
||||
# response = requests.post(url + endpoint, json=payload, headers=headers)
|
||||
# return check_response(response)
|
||||
|
||||
|
||||
def api_add_groups(groups):
|
||||
print(groups)
|
||||
payload2 = []
|
||||
endpoint = "v2/bulk/people"
|
||||
payload = {
|
||||
"data": {
|
||||
"attributes": {"people": [{"groups": groups}]}
|
||||
}
|
||||
}
|
||||
for group in groups:
|
||||
payload2.append({"groups" : group })
|
||||
payload = {"data": {"attributes": {"people": payload2 }}}
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json",
|
||||
"X-Api-Key": session["key"],
|
||||
}
|
||||
response = requests.post(url + endpoint, json=payload, headers=headers)
|
||||
return check_response(response)
|
||||
return payload
|
||||
# response = requests.post(url + endpoint, json=payload, headers=headers)
|
||||
# return check_response(response)
|
||||
|
||||
|
||||
def api_add_ppl_groups(emails, groups):
|
||||
endpoint = "v2/bulk/people"
|
||||
print(len(groups))
|
||||
combinations = list(itertools.product(emails, groups))
|
||||
print(combinations)
|
||||
payload = {
|
||||
"data": {
|
||||
"attributes": {"people": [{"email": emails, "groups": groups}]}
|
||||
}
|
||||
}
|
||||
print(payload)
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json",
|
||||
"X-Api-Key": session["key"],
|
||||
}
|
||||
response = requests.post(url + endpoint, json=payload, headers=headers)
|
||||
return check_response(response)
|
||||
payload2 = []
|
||||
endpoint = "v2/bulk/people"
|
||||
combinations = list(itertools.product(emails, groups))
|
||||
for combo in combinations:
|
||||
payload2.append({"email": combo[0], "groups": combo[1]})
|
||||
payload = {
|
||||
"data": {"attributes": {"people": payload2 }}
|
||||
}
|
||||
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json",
|
||||
"X-Api-Key": session["key"],
|
||||
}
|
||||
return payload
|
||||
#response = requests.post(url + endpoint, json=payload, headers=headers)
|
||||
#return check_response(response)
|
||||
|
||||
|
||||
def check_response(response):
|
||||
response = str(response)
|
||||
@ -343,67 +364,11 @@ def check_response(response):
|
||||
error = "Shrug"
|
||||
return render_template("bulk_add.html", title="Shrug", errors=error)
|
||||
|
||||
|
||||
@app.route("/templates", methods=["GET", "POST"])
|
||||
def templates():
|
||||
pass
|
||||
|
||||
'''
|
||||
@app.route("/bulk_add_groups", methods=["GET", "POST"])
|
||||
def bulk_add_groups():
|
||||
grouparr = []
|
||||
count = 0
|
||||
if request.method == "POST":
|
||||
groups = request.form.get("groups")
|
||||
if "\n" in groups:
|
||||
groups.split("\n")
|
||||
groups = [group.strip() for group in groups]
|
||||
elif "," in groups:
|
||||
groups.split(",")
|
||||
groups = [group.strip() for group in groups]
|
||||
for group in groups:
|
||||
groupdict = {}
|
||||
groupdict["name"] = group
|
||||
grouparr.append(groupdict)
|
||||
|
||||
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 + endpoint, json=payload, headers=headers)
|
||||
print(type(response))
|
||||
response = str(response)
|
||||
if "202" in response:
|
||||
error = "Success! Groups have been added successfully."
|
||||
return render_template(
|
||||
"bulk_add_groups.html",
|
||||
table=session["dfgroups"],
|
||||
title="Groups Added",
|
||||
error=error,
|
||||
)
|
||||
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.",
|
||||
]
|
||||
elif "422" in response:
|
||||
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,
|
||||
)
|
||||
else:
|
||||
error = "Shrug"
|
||||
return render_template("bulk_add_groups.html", title="Shrug", errors=error)
|
||||
'''
|
||||
|
||||
@app.route("/bulk_courses_to_groups", methods=["GET", "POST"])
|
||||
def bulk_courses_to_groups():
|
||||
|
||||
4
app/static/files/FLASK-TEST_-_Sheet1.csv
Normal file
4
app/static/files/FLASK-TEST_-_Sheet1.csv
Normal file
@ -0,0 +1,4 @@
|
||||
Email,Groups
|
||||
norm+72@northpass.com,All Apologies,Come as you are
|
||||
norm+90@northpass.com,Come as you are
|
||||
norm+98@northpass.com,The Pines,The Sea,An Ocean
|
||||
|
4
app/static/files/FLASK-TEST_-_Sheet1_1.csv
Normal file
4
app/static/files/FLASK-TEST_-_Sheet1_1.csv
Normal file
@ -0,0 +1,4 @@
|
||||
Email,Groups,
|
||||
norm+72@northpass.com,All Apologies,Come As You Are
|
||||
norm+90@northpass.com,Come As You Are,
|
||||
norm+98@northpass.com,Unplugged,
|
||||
|
@ -38,7 +38,7 @@
|
||||
<p> </p>
|
||||
<div class="man-csv-opts">
|
||||
<div class="manual-opts">
|
||||
<p><label for="Bulk Add People"> Each item must be comma separated or placed on a
|
||||
<p><label for="Bulk Add"> Each item must be comma separated or placed on a
|
||||
new line.</label></p>
|
||||
<form action="{{ url_for("bulk_add")}}" method="post">
|
||||
<p>Emails</p>
|
||||
@ -52,5 +52,7 @@
|
||||
{% include 'csv.html' %}
|
||||
</div>
|
||||
</div>
|
||||
{% include 'table.html' %}
|
||||
{% if table %}
|
||||
{% include 'table.html' %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
@ -3,18 +3,19 @@
|
||||
<h3> If you'd like to upload a CSV. Please do so here:</h3>
|
||||
<p></p>
|
||||
<form method="POST"
|
||||
action="{{ url_for('csv') }}"
|
||||
action="{{ url_for('upload_file') }}"
|
||||
enctype="multipart/form-data">
|
||||
<p><input type="file" name="file"></p>
|
||||
<div class="radio-options">
|
||||
<input type="radio" id="all-groups" name="all-groups" value="all-groups">
|
||||
<label for="all-groups">All Learners in All Groups</label>
|
||||
<input type="radio" id="all-groups" name="learner-groups" value="all-groups" checked>
|
||||
<label for="learner-groups">All Learners in All Groups</label>
|
||||
</div>
|
||||
<div class="radio-options">
|
||||
<input type="radio" id="some-groups" name="some-groups" value="some-groups">
|
||||
<label for="some-groups">Learners Only in Adjacent Groups</label>
|
||||
<input type="radio" id="some-groups" name="learner-groups" value="some-groups">
|
||||
<label for="learner-groups">Learners Only in Adjacent Groups</label>
|
||||
</div>
|
||||
<p><input type="submit" value="Submit CSV"></p>
|
||||
<p><input type="submit" name="preview" value="Preview"></p>
|
||||
<input type="submit" name="submit" value="Submit to Academy"></input>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user