Trying to get exceptions and errors to work. I also added a new feature. I need to consolidate the API calls, but not sure how yet... I have a function per action when all that changes is the url...

This commit is contained in:
Norm Rasmussen
2023-02-27 16:04:19 -05:00
parent f1f8c84bbf
commit deb11e07ae
7 changed files with 106 additions and 75 deletions

View File

@ -4,9 +4,41 @@ import requests
import json
import itertools
import pandas as pd
from itables import show
import re
def handle_responses(response):
print(response.status_code)
print(response.text)
try:
response.raise_for_status()
print(response.raise_for_status())
except requests.exceptions as err:
return render_template("index.html", title="Error Home", error=err)
print(err)
finally:
return correct_key(response)
def temp():
if "401" in str(response):
error = [
"Unauthorized access error. This can mean a lot of things.",
"Has the key changed, recently?",
]
return render_template("index.html", title="Error Home", error=error)
else:
return correct_key(response)
def correct_key(response):
data = response.json()
session["school"] = data["data"]["attributes"]["properties"]["name"]
print(session["school"])
return render_template("options.html", title="Options")
@app.route("/", methods=["GET", "POST"])
def ask_key():
session.clear()
@ -19,10 +51,7 @@ def ask_key():
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 handle_responses(response)
else:
error = "Hm. That doesn't seem right"
return render_template("index.html", title="Home", error=error)
@ -36,6 +65,20 @@ 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 = []
@ -61,7 +104,11 @@ def get_courses():
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)
dataframe = pd.DataFrame(array).drop(
["list_image_url", "permalink"],
axis=1
)
dataframe['full_description'] = dataframe['full_description'].str.replace(r'<[^<>]*>', '', regex=True)
print(dataframe)
if "next" not in nextlink:
@ -69,25 +116,11 @@ def get_courses():
dfcourse = dataframe.to_html()
session["dfcsv"] = dataframe.to_csv()
return render_template("table.html", table=dfcourse, title="List of Courses")
return render_template("get.html", table=dfcourse, title="List of Courses")
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 = []
@ -117,14 +150,15 @@ def get_people():
break
dfppl = dataframe.to_html()
dfshow = show(dfppl)
session["dfcsv"] = dataframe.to_csv()
return render_template("table.html", table=dfppl, title="List of People")
return render_template("get.html", table=dfppl, title="List of People")
else:
return "what what"
@app.route("/add_ppl_options", methods=["POST"])
def add_ppl_options():
@app.route("/add_ppl_opts", methods=["POST"])
def add_ppl_opts():
array = []
dict_response = {}
df = pd.DataFrame()
@ -182,8 +216,8 @@ def bulk_add_ppl():
return payload
@app.route("/add_groups_options", methods=["POST"])
def add_groups_options():
@app.route("/add_groups_opts", methods=["POST"])
def add_groups_opts():
array = []
dict_response = {}
df = pd.DataFrame()
@ -268,6 +302,29 @@ def bulk_add_groups():
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
@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"
payload = {
"payload": {
"person_ids": person_ids,
"group_ids": group_ids,
}
}
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"

BIN
app/static/np-favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -1,43 +1,5 @@
<!DOCTYPE html>
{% extends 'head.html' %}
{% block content %}
<h4>Hello! Please select what you'd like to do.</h4>
</div>
<div class="card-grid">
<form class="card"
action="{{ url_for('get_people')}}"
method="post">
<a class="a-card"
onclick="document.forms[0].submit()"
style="cursor:pointer;">
<i class="ri-car-line card__icon"></i>
<p class="card__name">Get People</p>
</a>
</form>
<form class="card"
id="get_courses"
action="{{ url_for('get_courses')}}"
method="post">
<a class="a-card"
onclick="document.forms['get_courses'].submit()"
style="cursor:pointer;">
<i class="ri-plane-line card__icon"></i>
<p class="card__name">Get Courses</p>
</a>
</form>
<div class="card"
href = "/add"
>
<a class="a-card"
onclick="document.forms['bulk_add'].submit()"
style="cursor:pointer;">
<i class="ri-plane-line card__icon"></i>
<p class="card__name">Get Courses</p>
</a>
</div>
</body>
{% endblock %}
{% include 'table.html' %}
{% endblock %}

View File

@ -8,7 +8,7 @@
href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css"
rel="stylesheet"
/>
<link rel="shortcut icon" href="{{ url_for('static', filename='np-favicon.png') }}">
<title>CSM Bulk App</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />

View File

@ -2,9 +2,9 @@
{% extends 'head.html' %}
{% block content %}
<h4>Hello! Please click below to enter your key.</h4>
{% if error %}
<p class=error><strong> </strong>{{ error }}
{% endif %}
{% for error in error %}
<h2 class=error><strong> {{ error }} </strong></h2>
{% endfor %}
<form action="{{ url_for("ask_key")}}" method="post">
<input type="text" name="apikey">
<input type="submit" value="Submit">

View File

@ -4,6 +4,7 @@
<h4>Hello! Please find the options for {{ session.school }}.</h4>
<p></p>
</div>
<div class="card-grid">
<form class="card"
@ -28,27 +29,38 @@
</a></form>
<form class="card"
id="add_ppl_options"
action="{{ url_for('add_ppl_options')}}"
id="add_ppl_opts"
action="{{ url_for('add_ppl_opts')}}"
method="post">
<a class="a-card"
onclick="document.forms['add_ppl_options'].submit()"
onclick="document.forms['add_ppl_opts'].submit()"
style="cursor:pointer;">
<i class="ri-send-plane-line card__icon"></i>
<p class="card__name">Bulk Add People</p>
</a></form>
<form class="card"
id="add_groups_options"
action="{{ url_for('add_groups_options')}}"
id="add_groups_opts"
action="{{ url_for('add_groups_opts')}}"
method="post">
<a class="a-card"
onclick="document.forms['add_groups_options'].submit()"
onclick="document.forms['add_groups_opts'].submit()"
style="cursor:pointer;">
<i class="ri-shape-2-line card__icon"></i>
<p class="card__name">Bulk Add Groups</p>
</a></form>
<form class="card"
id="ppl_to_groups_opts"
action="{{ url_for('ppl_to_groups_opts')}}"
method="post">
<a class="a-card"
onclick="document.forms['ppl_to_groups_opts'].submit()"
style="cursor:pointer;">
<i class="ri-group-line card__icon"></i>
<p class="card__name">Add Active People to Groups</p>
</div>
</body>
{% endblock %}