Started the CSV Upload functionality

This commit is contained in:
Norm Rasmussen
2023-03-02 17:56:31 -05:00
parent b3a44ce929
commit 074819ce8f
4 changed files with 91 additions and 54 deletions

View File

@ -1,25 +1,31 @@
from app import app
from flask import request, Flask, flash, render_template, session, make_response
import requests import requests
import json
import itertools import itertools
import pandas as pd import pandas as pd
from itables import show
import re import re
import os
import glob
from app import app
from flask import request, render_template, session, make_response
# Upload folder
UPLOAD_FOLDER = 'static/files'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
ALLOWED_EXTENSIONS = {'csv'}
def key_response(response): def key_response(response):
if "402" in str(response): if "402" in str(response):
error = response.text error = response.text
return render_template("index.html", title="Error Home", error=error) return render_template("index.html", title="Error Home", error=error)
elif "401" in str(response): if "401" in str(response):
error = [ error = [
"Unauthorized access error. This can mean a lot of things, such as the key being changed.", "Unauthorized access error.",
"This can mean a lot of things,",
"such as the key being changed.",
"Remember, they are paired to each educator!", "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", error=error)
else: return correct_key(response)
return correct_key(response)
def correct_key(response): def correct_key(response):
@ -29,31 +35,45 @@ def correct_key(response):
return render_template("options.html", title="Options") return render_template("options.html", title="Options")
@app.route("/", methods=["GET", "POST"]) @app.route("/", methods=["GET", "POST"])
def ask_key(): 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() session.clear()
if request.method == "POST": if request.method == "POST":
session["key"] = request.form.get("apikey") session["key"] = request.form.get("apikey")
if re.search("\s", session["key"]): #if re.search(r"\s", session["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: if session["key"] is not None and len(session["key"]) > 10:
url = "https://api.northpass.com/v2/properties/school" url = "https://api.northpass.com/v2/properties/school"
headers = {"accept": "application/json", "X-Api-Key": session["key"]} headers = {"accept": "application/json", "X-Api-Key": session["key"]}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
return key_response(response) return key_response(response)
else: 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)
else: return render_template("index.html", title="Home")
return render_template("index.html", title="Home")
# @app.route("/", method="POST") @app.route("/csv", methods=["GET", "POST"])
# def upload_csv(): def parse_csv():
# pass csvData = pd.DataFrame()
if request.method == 'POST':
uploaded_file = request.files['file']
file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file.filename)
uploaded_file.save(file_path)
col_names = ['Learner Full Name', 'Email','Course Name']
csvData = pd.read_csv(file_path,names=col_names, header=None)
return "SUBMITTED!"
for i,row in csvData.iterrows():
print(i, row['Email'])
return("File uploaded!")
return render_template("csv.html", title="Upload")
@app.route("/", methods=["GET", "POST"]) @app.route("/", methods=["GET", "POST"])
def render_home(): def render_home():
@ -78,18 +98,16 @@ def download_csv():
def get_courses(): def get_courses():
array = [] array = []
course_dict = {} course_dict = {}
df = pd.DataFrame()
tempdf = pd.DataFrame()
pd.set_option("display.max_colwidth", 100) pd.set_option("display.max_colwidth", 100)
x = 0 count = 0
dataframe = pd.DataFrame()
if request.method == "POST": if request.method == "POST":
while True: while True:
x += 1 count += 1
url = f"https://api.northpass.com/v2/courses?page={x}" url = f"https://api.northpass.com/v2/courses?page={count}"
headers = {"accept": "application/json", "X-Api-Key": session["key"]} headers = {"accept": "application/json", "X-Api-Key": session["key"]}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
jsonresponse = response.json()
data = response.json() data = response.json()
nextlink = data["links"] nextlink = data["links"]
@ -105,7 +123,7 @@ def get_courses():
dataframe["full_description"] = dataframe[ dataframe["full_description"] = dataframe[
"full_description" "full_description"
].str.replace(r"<[^<>]*>", "", regex=True) ].str.replace(r"<[^<>]*>", "", regex=True)
print(dataframe) print(dataframe)
if "next" not in nextlink: if "next" not in nextlink:
break break
@ -121,13 +139,13 @@ def get_courses():
def get_people(): def get_people():
array = [] array = []
ppl_dict = {} ppl_dict = {}
df = pd.DataFrame() count = 0
x = 0 dataframe = pd.DataFrame()
if request.method == "POST": if request.method == "POST":
while True: while True:
x += 1 count += 1
url = f"https://api.northpass.com/v2/people?page={x}" url = f"https://api.northpass.com/v2/people?page={count}"
headers = {"accept": "application/json", "X-Api-Key": session["key"]} headers = {"accept": "application/json", "X-Api-Key": session["key"]}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
data = response.json() data = response.json()
@ -140,7 +158,7 @@ def get_people():
ppl_dict[keys] = values ppl_dict[keys] = values
array.append(ppl_dict) array.append(ppl_dict)
dataframe = pd.DataFrame(array).drop("custom_avatar_url", axis=1) dataframe = pd.DataFrame(array).drop("custom_avatar_url", axis=1)
print(dataframe) print(dataframe)
if "next" not in nextlink: if "next" not in nextlink:
break break
@ -156,12 +174,13 @@ def get_people():
def add_ppl_opts(): def add_ppl_opts():
array = [] array = []
dict_response = {} dict_response = {}
df = pd.DataFrame() dataframe = pd.DataFrame()
x = 0 count = 0
if request.method == "POST": if request.method == "POST":
while True: while True:
x += 1 count += 1
url = f"https://api.northpass.com/v2/groups?page={x}" url = f"https://api.northpass.com/v2/groups?page={count}"
headers = {"accept": "application/json", "X-Api-Key": session["key"]} headers = {"accept": "application/json", "X-Api-Key": session["key"]}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
data = response.json() data = response.json()
@ -190,13 +209,12 @@ def add_ppl_opts():
@app.route("/bulk_add_ppl", methods=["GET", "POST"]) @app.route("/bulk_add_ppl", methods=["GET", "POST"])
def bulk_add_ppl(): def bulk_add_ppl():
emailarr = []
grouparr = []
if request.method == "POST": if request.method == "POST":
emails = request.form.get("emails") emails = request.form.get("emails")
groups = request.form.get("groups") groups = request.form.get("groups")
emails = emails.split(",") emails.split(",")
groups = groups.split(",") groups.split(",")
url = "https://api.northpass.com/v2/bulk/people" url = "https://api.northpass.com/v2/bulk/people"
combinations = list(itertools.product(emails, groups)) combinations = list(itertools.product(emails, groups))
print(combinations) print(combinations)
@ -219,9 +237,9 @@ def bulk_add_ppl():
error=error, 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 don't have appropriate privileges."
elif "422" in response: elif "422" in response:
error = "Hm. Looks like something was wrong with the names. Reach out to the manager of this app." error = "Hm. Looks like something was wrong with the names."
return render_template( return render_template(
"bulk_add_people.html", "bulk_add_people.html",
table=session["dfgroups"], table=session["dfgroups"],
@ -237,12 +255,13 @@ def bulk_add_ppl():
def add_groups_opts(): def add_groups_opts():
array = [] array = []
dict_response = {} dict_response = {}
df = pd.DataFrame() count = 0
x = 0 dataframe = pd.DataFrame()
if request.method == "POST": if request.method == "POST":
while True: while True:
x += 1 count += 1
url = f"https://api.northpass.com/v2/groups?page={x}" url = f"https://api.northpass.com/v2/groups?page={count}"
headers = {"accept": "application/json", "X-Api-Key": session["key"]} headers = {"accept": "application/json", "X-Api-Key": session["key"]}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
data = response.json() data = response.json()
@ -272,14 +291,14 @@ def add_groups_opts():
@app.route("/bulk_add_groups", methods=["GET", "POST"]) @app.route("/bulk_add_groups", methods=["GET", "POST"])
def bulk_add_groups(): def bulk_add_groups():
grouparr = [] grouparr = []
i = 0 count = 0
if request.method == "POST": if request.method == "POST":
groups = request.form.get("groups") groups = request.form.get("groups")
if "\n" in groups: if '\n' in groups:
groups = groups.split("\n") groups.split('\n')
groups = [group.strip() for group in groups] groups = [group.strip() for group in groups]
elif "," in groups: elif ',' in groups:
groups = groups.split(",") groups.split(",")
groups = [group.strip() for group in groups] groups = [group.strip() for group in groups]
for group in groups: for group in groups:
groupdict = {} groupdict = {}
@ -305,9 +324,12 @@ def bulk_add_groups():
error=error, 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( return render_template(
"bulk_add_groups.html", "bulk_add_groups.html",
table=session["dfgroups"], table=session["dfgroups"],

14
app/templates/csv.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
{% include 'logo.html' %}
{% extends 'head.html' %}
{% block content %}
<h3> If you'd like to upload a CSV. Please do so here:<h3>
<p></p>
<form method="POST" action="" enctype="multipar/form-data">
<p><input type="file" name="file"></p>
<p><input type="submit" value="Submit CSV"></p>
</form>
{% endblock %}

View File

@ -2,7 +2,8 @@
{% extends 'head.html' %} {% extends 'head.html' %}
{% include 'logo.html' %} {% include 'logo.html' %}
{% block content %} {% block content %}
<h4>Hello! Please find the options for {{ session.school }}.</h4> <h4>Hello! Please find the options for {{ session.school }}.</h4>
<p></p> <p></p>
</div> </div>
<div class="card-grid"> <div class="card-grid">