Good progress. Bulk adds are working for emails and groups seperately, but not together. I think I need to for loop through each item to make it work.

This commit is contained in:
Norm Rasmussen
2023-03-07 21:35:44 -05:00
parent 1cfc4fd9d1
commit 9e3ac00285
3 changed files with 56 additions and 68 deletions

View File

@ -230,11 +230,6 @@ def bulk_add_groups_opts():
return "This isn't working. Let's go our own way." return "This isn't working. Let's go our own way."
@app.route("/options", methods=["GET", "POST"])
def ppl_to_groups_opts():
pass
@app.route("/bulk_add", methods=["GET", "POST"]) @app.route("/bulk_add", methods=["GET", "POST"])
def bulk_add(): def bulk_add():
if request.method == "POST": if request.method == "POST":
@ -245,20 +240,32 @@ def bulk_add():
emails = emails.split("\n") emails = emails.split("\n")
emails = [email.strip() for email in emails] 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]
print(emails)
print(type(emails))
# return api_add_ppl_groups(emails, groups)
elif "," in emails: elif "," in emails:
emails = emails.split(",") emails = emails.split(",")
emails = [email.strip() for email in emails] emails = [email.strip() for email in emails]
# return api_add_ppl_groups(emails, groups) else:
emails = []
emails.append(emails)
if groups: if groups:
if "\n" in groups: if "\n" in groups:
groups = groups.split("\n") groups = groups.split("\n")
groups = [group.strip() for group in groups] groups = [group.strip() for group in groups]
groups = [re.sub(r'[,]', "", group) for group in groups]
elif "," in groups: elif "," in groups:
groups = groups.split(",") groups = groups.split(",")
groups = [group.strip() for group in groups] groups = [group.strip() for group in groups]
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')
@ -267,16 +274,44 @@ def bulk_add():
# groupdict = {} # groupdict = {}
# groupdict["name"] = group # groupdict["name"] = group
def api_add_ppl(emails): def api_add_ppl(emails):
pass print(emails)
endpoint = "v2/bulk/people"
payload = {
"data": {
"attributes": {"people": [{"email": emails}]}
}
}
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)
def api_add_groups(groups): def api_add_groups(groups):
pass print(groups)
endpoint = "v2/bulk/people"
payload = {
"data": {
"attributes": {"people": [{"groups": groups}]}
}
}
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)
def api_add_ppl_groups(emails, groups): def api_add_ppl_groups(emails, groups):
endpoint = "v2/bulk/people" endpoint = "v2/bulk/people"
print(len(groups))
combinations = list(itertools.product(emails, groups)) combinations = list(itertools.product(emails, groups))
print(combinations) print(combinations)
payload = { payload = {
@ -284,75 +319,35 @@ def api_add_ppl_groups(emails, groups):
"attributes": {"people": [{"email": emails, "groups": groups}]} "attributes": {"people": [{"email": emails, "groups": groups}]}
} }
} }
print(payload)
headers = { headers = {
"accept": "application/json", "accept": "application/json",
"content-type": "application/json", "content-type": "application/json",
"X-Api-Key": session["key"], "X-Api-Key": session["key"],
} }
response = requests.post(url + endpoint, json=payload, headers=headers) response = requests.post(url + endpoint, json=payload, headers=headers)
response = str(response) return check_response(response)
if "202" in response:
error = "Success! People have been added successfully."
return render_template(
"bulk_add_ppl.html",
table=session["dfgroups"],
title="People Added",
error=error,
)
elif "403" in response:
error = "Uh oh. Looks like you don't have appropriate privileges."
elif "422" in response:
error = "Hm. Looks like something was wrong with the names."
return render_template(
"bulk_add_people.html",
table=session["dfgroups"],
title="People Added",
error=error,
)
else:
error = "Shrug"
return render_template("bulk_add_ppl.html", title="Shrug", errors=error)
def api_add_ppl(): def check_response(response):
endpoint = "v2/bulk/people"
payload = {"data": {"attributes": {"people": [{"email": emails}]}}}
headers = {
"accept": "application/json",
"content-type": "application/json",
"X-Api-Key": session["key"],
}
response = requests.post(url + endpoint, json=payload, headers=headers)
response = str(response) response = str(response)
if "202" in response: if "202" in response:
error = "Success! People have been added successfully." error = "Success! People have been added successfully."
return render_template( return render_template("bulk_add.html", title="People Added", error=error)
"bulk_add_ppl.html",
table=session["dfgroups"],
title="People Added",
error=error,
)
elif "403" in response: elif "403" in response:
error = "Uh oh. Looks like you don't have appropriate privileges." error = "Uh oh. Looks like you don't have appropriate privileges."
return render_template("bulk_add.html", error=error)
elif "422" in response: elif "422" in response:
error = "Hm. Looks like something was wrong with the names." error = "Hm. Looks like something was wrong with the data you added."
return render_template( return render_template("bulk_add.html", error=error)
"bulk_add_people.html",
table=session["dfgroups"],
title="People Added",
error=error,
)
else: else:
error = "Shrug" error = "Shrug"
return render_template("bulk_add_ppl.html", title="Shrug", errors=error) return render_template("bulk_add.html", title="Shrug", errors=error)
error = "No Data was Loaded. Try again, bozo."
return render_template("bulk_add_ppl.html", title="No Data", errors=error)
@app.route("/templates", methods=["GET", "POST"]) @app.route("/templates", methods=["GET", "POST"])
def templates(): def templates():
pass pass
'''
@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 = []
@ -408,7 +403,7 @@ def bulk_add_groups():
else: else:
error = "Shrug" error = "Shrug"
return render_template("bulk_add_groups.html", title="Shrug", errors=error) return render_template("bulk_add_groups.html", title="Shrug", errors=error)
'''
@app.route("/bulk_courses_to_groups", methods=["GET", "POST"]) @app.route("/bulk_courses_to_groups", methods=["GET", "POST"])
def bulk_courses_to_groups(): def bulk_courses_to_groups():

View File

@ -5,12 +5,5 @@
{% block content %} {% block content %}
<h2>Hello! You're currently accessing {{ session.school }}.</h2> <h2>Hello! You're currently accessing {{ session.school }}.</h2>
<h3>You have two options here. You can click one of the options above and upload
people/courses/groups manually. Or, you can upload a CSV, and then select one
of the options and perform the action with the data in the CSV. Note that you
may need specific IDs for certain tasks.
<p></p>
{% include 'csv.html' %}
{% endblock %} {% endblock %}