2023-02-17 16:59:42 -05:00
|
|
|
import requests
|
2023-02-22 17:11:14 -05:00
|
|
|
import itertools
|
2023-02-17 16:59:42 -05:00
|
|
|
import re
|
2023-03-02 17:56:31 -05:00
|
|
|
import os
|
2023-03-08 16:11:32 -05:00
|
|
|
import csv
|
2023-03-13 17:23:56 -04:00
|
|
|
from .forms import TemplateForm
|
2023-03-10 17:59:16 -05:00
|
|
|
from functools import wraps
|
2023-03-07 14:56:01 -05:00
|
|
|
from flask import (
|
|
|
|
|
redirect,
|
|
|
|
|
flash,
|
|
|
|
|
request,
|
|
|
|
|
render_template,
|
|
|
|
|
session,
|
|
|
|
|
make_response,
|
|
|
|
|
url_for,
|
2023-03-15 17:07:34 -04:00
|
|
|
g,
|
2023-03-07 14:56:01 -05:00
|
|
|
)
|
2023-03-03 20:47:37 -05:00
|
|
|
from werkzeug.utils import secure_filename
|
2023-03-13 17:23:56 -04:00
|
|
|
from app import app, forms
|
2023-03-06 09:33:42 -05:00
|
|
|
|
2023-03-02 17:56:31 -05:00
|
|
|
# Upload folder
|
2023-03-07 14:56:01 -05:00
|
|
|
UPLOAD_FOLDER = "/Users/normrasmussen/Documents/Projects/CSM_webapp/app/static/files"
|
2023-03-03 20:47:37 -05:00
|
|
|
# UPLOAD_FOLDER = 'static/files'
|
2023-03-07 14:56:01 -05:00
|
|
|
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
|
|
|
|
ALLOWED_EXTENSIONS = {"csv"}
|
2023-02-17 16:59:42 -05:00
|
|
|
|
2023-03-13 17:23:56 -04:00
|
|
|
# Global Variables
|
|
|
|
|
url = "https://api.northpass.com/"
|
2023-02-17 16:59:42 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-06 09:33:42 -05:00
|
|
|
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
|
|
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
|
2023-03-01 17:05:16 -05:00
|
|
|
def key_response(response):
|
|
|
|
|
if "402" in str(response):
|
|
|
|
|
error = response.text
|
2023-03-09 15:15:42 -05:00
|
|
|
return render_template("index.html", title="Error Home", error=error)
|
2023-03-02 17:56:31 -05:00
|
|
|
if "401" in str(response):
|
2023-03-10 17:59:16 -05:00
|
|
|
error = "Unauthorized access error.(401)"
|
2023-03-09 15:15:42 -05:00
|
|
|
return render_template("index.html", title="Error Home", error=error)
|
2023-03-02 17:56:31 -05:00
|
|
|
return correct_key(response)
|
2023-02-27 16:04:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def correct_key(response):
|
|
|
|
|
data = response.json()
|
|
|
|
|
session["school"] = data["data"]["attributes"]["properties"]["name"]
|
2023-03-10 17:59:16 -05:00
|
|
|
return render_template("home.html", title="Active Session")
|
2023-02-27 16:04:19 -05:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
|
2023-03-06 09:33:42 -05:00
|
|
|
def allowed_file(filename):
|
2023-03-07 14:56:01 -05:00
|
|
|
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
2023-02-27 16:04:19 -05:00
|
|
|
|
2023-03-08 16:11:32 -05:00
|
|
|
|
2023-03-10 17:59:16 -05:00
|
|
|
def key_required(check):
|
|
|
|
|
@wraps(check)
|
|
|
|
|
def decorated_function(*args, **kwargs):
|
2023-03-15 17:07:34 -04:00
|
|
|
if session.get("key") is None:
|
|
|
|
|
return redirect("/", code=302)
|
2023-03-10 17:59:16 -05:00
|
|
|
return check(*args, **kwargs)
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-10 17:59:16 -05:00
|
|
|
return decorated_function
|
2023-03-06 09:33:42 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-02-17 16:59:42 -05:00
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
|
|
|
def ask_key():
|
2023-03-02 17:56:31 -05:00
|
|
|
"""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.
|
|
|
|
|
"""
|
2023-03-09 15:15:42 -05:00
|
|
|
specials = '"!@#$%^&*()-+?_=,<>/"'
|
2023-02-17 16:59:42 -05:00
|
|
|
if request.method == "POST":
|
2023-02-21 16:59:41 -05:00
|
|
|
session["key"] = request.form.get("apikey")
|
2023-03-15 17:07:34 -04:00
|
|
|
if any(char in specials for char in session["key"]) or re.search(
|
|
|
|
|
r"[\s]", session["key"]
|
|
|
|
|
):
|
2023-03-09 15:15:42 -05:00
|
|
|
error = "Invalid Key."
|
|
|
|
|
session.clear()
|
|
|
|
|
return render_template("index.html", title="Home", error=error)
|
2023-03-02 17:56:31 -05:00
|
|
|
if session["key"] is not None and len(session["key"]) > 10:
|
2023-03-06 09:33:42 -05:00
|
|
|
endpoint = "/v2/properties/school"
|
2023-02-26 22:08:24 -08:00
|
|
|
headers = {"accept": "application/json", "X-Api-Key": session["key"]}
|
2023-03-07 14:56:01 -05:00
|
|
|
response = requests.get(url + endpoint, headers=headers)
|
2023-03-01 17:05:16 -05:00
|
|
|
return key_response(response)
|
2023-03-02 17:56:31 -05:00
|
|
|
error = "Hm. That doesn't seem right"
|
2023-03-09 15:15:42 -05:00
|
|
|
session.clear()
|
2023-03-02 17:56:31 -05:00
|
|
|
return render_template("index.html", title="Home", error=error)
|
2023-03-09 15:15:42 -05:00
|
|
|
session.clear()
|
2023-03-02 17:56:31 -05:00
|
|
|
return render_template("index.html", title="Home")
|
2023-02-17 16:59:42 -05:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
|
2023-03-10 17:59:16 -05:00
|
|
|
@app.route("/render_home", methods=["GET", "POST"])
|
|
|
|
|
@key_required
|
2023-03-06 09:33:42 -05:00
|
|
|
def render_home():
|
2023-03-07 14:56:01 -05:00
|
|
|
if session.get("key"):
|
2023-03-10 17:59:16 -05:00
|
|
|
return render_template("home.html", title="Home")
|
|
|
|
|
return render_template("home.html", title="Enter Key")
|
2023-03-06 09:33:42 -05:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
|
2023-03-09 15:15:42 -05:00
|
|
|
@app.route("/clear_session", methods=["GET", "POST"])
|
2023-03-07 09:59:27 -05:00
|
|
|
def clear_session():
|
2023-03-09 15:15:42 -05:00
|
|
|
if session.get("key"):
|
|
|
|
|
session.clear()
|
2023-03-15 17:07:34 -04:00
|
|
|
error = "Session Cleared!"
|
2023-03-09 15:15:42 -05:00
|
|
|
return render_template("index.html", error=error, title="Home, New session")
|
2023-03-07 09:59:27 -05:00
|
|
|
return render_template("index.html", title="Home, New session")
|
|
|
|
|
|
|
|
|
|
|
2023-03-08 16:11:32 -05:00
|
|
|
@app.route("/table")
|
|
|
|
|
def table():
|
2023-03-10 17:59:16 -05:00
|
|
|
return render_template("table.html", tables=[session["table"]], titles=["Table"])
|
2023-03-08 16:11:32 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-08 16:11:32 -05:00
|
|
|
@app.route("/upload_file", methods=["GET", "POST"])
|
2023-03-10 17:59:16 -05:00
|
|
|
@key_required
|
2023-03-08 16:11:32 -05:00
|
|
|
def upload_file():
|
2023-03-07 14:56:01 -05:00
|
|
|
print("Uploading CSV")
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
if "file" not in request.files:
|
|
|
|
|
flash("No file found or uploaded")
|
2023-03-08 16:11:32 -05:00
|
|
|
return redirect(url_for("bulk_add_opts"))
|
2023-03-07 14:56:01 -05:00
|
|
|
file = request.files["file"]
|
|
|
|
|
if file.filename == "":
|
|
|
|
|
print("no file exists")
|
|
|
|
|
flash("No file found or uploaded")
|
2023-03-10 17:59:16 -05:00
|
|
|
return redirect(url_for("home"))
|
2023-03-08 16:11:32 -05:00
|
|
|
# return redirect(request.url)
|
2023-03-03 20:47:37 -05:00
|
|
|
if file and allowed_file(file.filename):
|
|
|
|
|
filename = secure_filename(file.filename)
|
2023-03-07 14:56:01 -05:00
|
|
|
file_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
|
2023-03-09 15:15:42 -05:00
|
|
|
session["file"] = filename
|
2023-03-07 14:56:01 -05:00
|
|
|
session["filepath"] = file_path
|
2023-03-03 20:47:37 -05:00
|
|
|
file.save(file_path)
|
2023-03-08 16:11:32 -05:00
|
|
|
file = list(csv.reader(open(file_path, "r")))
|
2023-03-09 15:15:42 -05:00
|
|
|
return divide_values(file)
|
2023-03-10 17:59:16 -05:00
|
|
|
return render_template("home.html", title="Bulk Add")
|
2023-03-09 15:15:42 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-09 15:15:42 -05:00
|
|
|
def divide_values(file):
|
|
|
|
|
emails = []
|
|
|
|
|
groups = []
|
2023-03-15 17:07:34 -04:00
|
|
|
selection = request.form.get("learner-groups")
|
|
|
|
|
if request.form["submit"]:
|
2023-03-09 15:15:42 -05:00
|
|
|
if selection == "all-groups":
|
|
|
|
|
for item in file[1:]:
|
|
|
|
|
emails.append(item[0])
|
|
|
|
|
groups.append(item[1:])
|
|
|
|
|
# FEAT: These two extract the groups and emails into two lists
|
|
|
|
|
groups = [item for group in groups for item in group]
|
|
|
|
|
groups = list(set(groups))
|
|
|
|
|
return api_csv_parse(emails, groups)
|
2023-03-10 17:59:16 -05:00
|
|
|
# We're good here. This can now be sent to the api
|
|
|
|
|
# functions with emails and groups.
|
2023-03-09 15:15:42 -05:00
|
|
|
elif selection == "some-groups":
|
|
|
|
|
submissions = []
|
|
|
|
|
for item in file[1:]:
|
|
|
|
|
# FEAT: This extracts each row as a list. Perfect for Learners in Specific Groups.
|
|
|
|
|
submissions.append(item)
|
|
|
|
|
for item in submissions:
|
|
|
|
|
emails.append(item[0])
|
|
|
|
|
print(type(emails))
|
|
|
|
|
groups = item[1:]
|
|
|
|
|
return api_csv_parse(emails, groups)
|
|
|
|
|
return emails
|
|
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
if request.form["preview"]:
|
|
|
|
|
error = "Preview Button Still Under Construction. Try again later."
|
2023-03-09 15:15:42 -05:00
|
|
|
return render_template("bulk_add.html", error=error, title="Preview Not Yet")
|
|
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
return render_template("bulk_add.html", title="Uploaded File")
|
|
|
|
|
|
2023-03-09 15:15:42 -05:00
|
|
|
|
|
|
|
|
def api_csv_parse(emails, groups):
|
|
|
|
|
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)
|
2023-03-15 17:07:34 -04:00
|
|
|
return render_template("bulk_add.html", title="CSV Submission")
|
|
|
|
|
|
2023-03-08 16:11:32 -05:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
@app.route("/bulk_add_opts", methods=["GET", "POST"])
|
2023-03-10 17:59:16 -05:00
|
|
|
@key_required
|
2023-03-07 14:56:01 -05:00
|
|
|
def bulk_add_opts():
|
2023-03-08 16:11:32 -05:00
|
|
|
return render_template("bulk_add.html", titles="Bulk Add Options")
|
|
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
@app.route("/bulk_add", methods=["GET", "POST"])
|
2023-03-10 17:59:16 -05:00
|
|
|
@key_required
|
2023-03-07 14:56:01 -05:00
|
|
|
def bulk_add():
|
2023-02-21 16:59:41 -05:00
|
|
|
if request.method == "POST":
|
|
|
|
|
emails = request.form.get("emails")
|
|
|
|
|
groups = request.form.get("groups")
|
2023-03-07 14:56:01 -05:00
|
|
|
if emails:
|
|
|
|
|
if "\n" in emails:
|
|
|
|
|
emails = emails.split("\n")
|
|
|
|
|
emails = [email.strip() for email in emails]
|
2023-03-08 16:11:32 -05:00
|
|
|
emails = [re.sub(r"[,]", "", email) for email in emails]
|
2023-03-07 14:56:01 -05:00
|
|
|
elif "," in emails:
|
|
|
|
|
emails = emails.split(",")
|
|
|
|
|
emails = [email.strip() for email in emails]
|
2023-03-07 21:35:44 -05:00
|
|
|
else:
|
2023-03-08 16:11:32 -05:00
|
|
|
emails = emails.split()
|
2023-03-10 17:59:16 -05:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
if groups:
|
|
|
|
|
if "\n" in groups:
|
2023-03-07 17:52:36 -05:00
|
|
|
groups = groups.split("\n")
|
2023-03-07 14:56:01 -05:00
|
|
|
groups = [group.strip() for group in groups]
|
2023-03-08 16:11:32 -05:00
|
|
|
groups = [re.sub(r"[,]", "", group) for group in groups]
|
2023-03-07 14:56:01 -05:00
|
|
|
elif "," in groups:
|
2023-03-07 17:52:36 -05:00
|
|
|
groups = groups.split(",")
|
2023-03-07 14:56:01 -05:00
|
|
|
groups = [group.strip() for group in groups]
|
2023-03-07 21:35:44 -05:00
|
|
|
else:
|
2023-03-08 16:11:32 -05:00
|
|
|
groups = groups.split()
|
2023-03-07 21:35:44 -05:00
|
|
|
|
|
|
|
|
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)
|
2023-03-08 16:11:32 -05:00
|
|
|
return render_template("bulk_add.html")
|
2023-03-07 14:56:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def api_add_ppl(emails):
|
2023-03-08 16:11:32 -05:00
|
|
|
payload2 = []
|
2023-03-07 21:35:44 -05:00
|
|
|
endpoint = "v2/bulk/people"
|
2023-03-08 16:11:32 -05:00
|
|
|
for email in emails:
|
2023-03-15 17:07:34 -04:00
|
|
|
payload2.append({"email": email})
|
|
|
|
|
payload = {"data": {"attributes": {"people": payload2}}}
|
2023-03-07 21:35:44 -05:00
|
|
|
headers = {
|
|
|
|
|
"accept": "application/json",
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
"X-Api-Key": session["key"],
|
|
|
|
|
}
|
2023-03-08 16:11:32 -05:00
|
|
|
return payload
|
|
|
|
|
# response = requests.post(url + endpoint, json=payload, headers=headers)
|
|
|
|
|
# return check_response(response)
|
2023-03-07 14:56:01 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def api_add_groups(groups):
|
2023-03-08 16:11:32 -05:00
|
|
|
payload2 = []
|
2023-03-07 21:35:44 -05:00
|
|
|
endpoint = "v2/bulk/people"
|
2023-03-08 16:11:32 -05:00
|
|
|
for group in groups:
|
2023-03-15 17:07:34 -04:00
|
|
|
payload2.append({"groups": group})
|
|
|
|
|
payload = {"data": {"attributes": {"people": payload2}}}
|
2023-03-07 21:35:44 -05:00
|
|
|
headers = {
|
|
|
|
|
"accept": "application/json",
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
"X-Api-Key": session["key"],
|
|
|
|
|
}
|
2023-03-08 16:11:32 -05:00
|
|
|
return payload
|
|
|
|
|
# response = requests.post(url + endpoint, json=payload, headers=headers)
|
|
|
|
|
# return check_response(response)
|
2023-03-07 14:56:01 -05:00
|
|
|
|
2023-03-02 17:56:31 -05:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
def api_add_ppl_groups(emails, groups):
|
2023-03-08 16:11:32 -05:00
|
|
|
payload2 = []
|
|
|
|
|
endpoint = "v2/bulk/people"
|
|
|
|
|
combinations = list(itertools.product(emails, groups))
|
|
|
|
|
for combo in combinations:
|
|
|
|
|
payload2.append({"email": combo[0], "groups": combo[1]})
|
2023-03-15 17:07:34 -04:00
|
|
|
payload = {"data": {"attributes": {"people": payload2}}}
|
2023-03-08 16:11:32 -05:00
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
|
"accept": "application/json",
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
"X-Api-Key": session["key"],
|
|
|
|
|
}
|
|
|
|
|
return payload
|
2023-03-15 17:07:34 -04:00
|
|
|
# response = requests.post(url + endpoint, json=payload, headers=headers)
|
|
|
|
|
# return check_response(response)
|
2023-03-08 16:11:32 -05:00
|
|
|
|
2023-03-07 17:52:36 -05:00
|
|
|
|
2023-03-07 21:35:44 -05:00
|
|
|
def check_response(response):
|
2023-03-07 17:52:36 -05:00
|
|
|
response = str(response)
|
|
|
|
|
if "202" in response:
|
|
|
|
|
error = "Success! People have been added successfully."
|
2023-03-07 21:35:44 -05:00
|
|
|
return render_template("bulk_add.html", title="People Added", error=error)
|
2023-03-07 17:52:36 -05:00
|
|
|
elif "403" in response:
|
|
|
|
|
error = "Uh oh. Looks like you don't have appropriate privileges."
|
2023-03-07 21:35:44 -05:00
|
|
|
return render_template("bulk_add.html", error=error)
|
2023-03-07 17:52:36 -05:00
|
|
|
elif "422" in response:
|
2023-03-07 21:35:44 -05:00
|
|
|
error = "Hm. Looks like something was wrong with the data you added."
|
|
|
|
|
return render_template("bulk_add.html", error=error)
|
2023-03-07 17:52:36 -05:00
|
|
|
else:
|
2023-03-10 17:59:16 -05:00
|
|
|
error = "Something went wrong, but I'm not sure what."
|
|
|
|
|
return render_template("bulk_add.html", title="Shrug", error=error)
|
2023-03-07 14:56:01 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-10 17:59:16 -05:00
|
|
|
@app.route("/load_templates", methods=["GET", "POST"])
|
|
|
|
|
@key_required
|
|
|
|
|
def load_templates():
|
|
|
|
|
count = 0
|
2023-03-15 17:07:34 -04:00
|
|
|
templates = []
|
2023-03-10 17:59:16 -05:00
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
count += 1
|
|
|
|
|
endpoint = "v2/custom_templates"
|
|
|
|
|
headers = {
|
|
|
|
|
"accept": "application/json",
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
"X-Api-Key": session["key"],
|
|
|
|
|
}
|
2023-03-15 17:07:34 -04:00
|
|
|
response = requests.get(url + endpoint, headers=headers)
|
2023-03-10 17:59:16 -05:00
|
|
|
data = response.json()
|
|
|
|
|
nextlink = data["links"]
|
|
|
|
|
for response in data["data"]:
|
2023-03-15 17:07:34 -04:00
|
|
|
last_updated = response["attributes"]["updated_at"].split("T")
|
|
|
|
|
last_updated = last_updated[0]
|
|
|
|
|
name, body, last_updated = (
|
|
|
|
|
response["attributes"]["name"],
|
|
|
|
|
response["attributes"]["body"],
|
|
|
|
|
last_updated,
|
|
|
|
|
)
|
|
|
|
|
templates.append((name, body, last_updated))
|
2023-03-10 17:59:16 -05:00
|
|
|
|
|
|
|
|
if "next" not in nextlink:
|
|
|
|
|
break
|
|
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
save_templates_backup(templates)
|
|
|
|
|
return render_template(
|
|
|
|
|
"templates.html",
|
|
|
|
|
title="Templates",
|
|
|
|
|
templates=templates,
|
|
|
|
|
)
|
2023-03-10 17:59:16 -05:00
|
|
|
|
|
|
|
|
return render_template("options.html")
|
2023-03-08 16:11:32 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-07 14:56:01 -05:00
|
|
|
@app.route("/templates", methods=["GET", "POST"])
|
2023-03-10 17:59:16 -05:00
|
|
|
@key_required
|
2023-03-07 14:56:01 -05:00
|
|
|
def templates():
|
2023-03-10 17:59:16 -05:00
|
|
|
if request.method == "POST":
|
2023-03-15 17:07:34 -04:00
|
|
|
if request.form["submit-template"]:
|
|
|
|
|
name = request.form.get("template_name")
|
|
|
|
|
body = request.form.get("body")
|
|
|
|
|
g.last_template = name
|
2023-03-13 17:23:56 -04:00
|
|
|
if body == "":
|
2023-03-15 17:07:34 -04:00
|
|
|
error = (
|
|
|
|
|
"Ooph. Looks like you didn't load the changes before submitting."
|
|
|
|
|
)
|
2023-03-13 17:23:56 -04:00
|
|
|
return render_template("templates.html", error=error)
|
|
|
|
|
else:
|
|
|
|
|
endpoint = "v2/custom_templates"
|
|
|
|
|
headers = {
|
|
|
|
|
"accept": "application/json",
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
"X-Api-Key": session["key"],
|
|
|
|
|
}
|
2023-03-15 17:07:34 -04:00
|
|
|
payload = {"custom_template": {"name": name, "body": body}}
|
|
|
|
|
response = requests.post(url + endpoint, json=payload, headers=headers)
|
2023-03-13 17:23:56 -04:00
|
|
|
return check_templates(response)
|
|
|
|
|
return load_templates()
|
2023-03-10 17:59:16 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
2023-03-10 17:59:16 -05:00
|
|
|
def check_templates(response):
|
|
|
|
|
print(response)
|
|
|
|
|
response = str(response)
|
|
|
|
|
if "201" in response:
|
|
|
|
|
error = "Success! Templates Uploaded."
|
2023-03-15 17:07:34 -04:00
|
|
|
button = "Undo"
|
|
|
|
|
return render_template(
|
|
|
|
|
"templates.html", title="Templates Added", error=error, button=button
|
|
|
|
|
)
|
2023-03-10 17:59:16 -05:00
|
|
|
elif "403" in response:
|
|
|
|
|
error = "Uh oh. Looks like you don't have appropriate privileges."
|
|
|
|
|
return render_template("templates.html", error=error)
|
2023-03-13 17:23:56 -04:00
|
|
|
elif "404" in response:
|
|
|
|
|
error = "Hm. Looks like something was wrong in the templates."
|
|
|
|
|
return render_template("templates.html", error=error)
|
2023-03-10 17:59:16 -05:00
|
|
|
else:
|
|
|
|
|
error = "Something went wrong, but I'm not sure what."
|
|
|
|
|
return render_template("templates.html", title="Shrug", error=error)
|
2023-02-22 17:11:14 -05:00
|
|
|
|
2023-02-27 16:04:19 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
def save_templates_backup(templates):
|
|
|
|
|
g.client_path = os.path.join(UPLOAD_FOLDER, session["school"])
|
|
|
|
|
if os.path.exists(g.client_path):
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
os.mkdir(g.client_path)
|
2023-03-07 09:59:27 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
for tupe in templates:
|
|
|
|
|
file_name = tupe[0] + ".liquid"
|
|
|
|
|
file_body = tupe[1]
|
|
|
|
|
complete_path = os.path.join(g.client_path, file_name)
|
2023-03-07 09:59:27 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
with open(complete_path, "w+") as temp:
|
|
|
|
|
temp.write(file_body)
|
|
|
|
|
temp.close
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/undo_template", methods=["POST"])
|
2023-03-10 17:59:16 -05:00
|
|
|
@key_required
|
2023-03-15 17:07:34 -04:00
|
|
|
def undo_template():
|
|
|
|
|
print(g.client_path)
|
|
|
|
|
template_path = os.path.join(g.client_path, g.last_template)
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
if request.form["undo_templates"]:
|
|
|
|
|
if os.path.exists(template_path):
|
|
|
|
|
print(template_path)
|
2023-03-07 09:59:27 -05:00
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
|
|
|
|
|
@app.route("/cmtest", methods=["GET", "POST"])
|
2023-03-13 17:23:56 -04:00
|
|
|
def cmtest():
|
|
|
|
|
form = TemplateForm()
|
|
|
|
|
if form.validate_on_submit():
|
|
|
|
|
text = form.template_code.data
|
2023-03-15 17:07:34 -04:00
|
|
|
return render_template("templates.html", form=form)
|
2023-03-07 14:56:01 -05:00
|
|
|
|
2023-02-27 16:04:19 -05:00
|
|
|
|
2023-02-17 16:59:42 -05:00
|
|
|
app.secret_key = "@&I\x1a?\xce\x94\xbb0w\x17\xbf&Y\xa2\xc2(A\xf5\xf2\x97\xba\xeb\xfa"
|
|
|
|
|
|
|
|
|
|
|
2023-03-15 17:07:34 -04:00
|
|
|
# if __name__ == "__main__":
|
2023-03-13 17:23:56 -04:00
|
|
|
# ask_key()
|