def refresh(): data = [] links = set() with connect_db() as db: sheets = db("SELECT url, sheet, secure FROM sources WHERE course=(%s)", [get_course()]).fetchall() for url, sheet, secure in sheets: try: csvr = read_spreadsheet(url=url, sheet_name=sheet) except: return error(f"Failed to read spreadsheet {url} (Sheet: {sheet})") headers = [x.lower() for x in csvr[0]] for row in csvr[1:]: row = row + [""] * 5 shortlink = row[headers.index("shortlink")] if shortlink in links: return error( f"Duplicate shortlink `{shortlink}` found, aborting.") links.add(shortlink) url = row[headers.index("url")] creator = row[headers.index("creator")] data.append([shortlink, url, creator, secure, get_course()]) with connect_db() as db: db("DELETE FROM shortlinks WHERE course=%s", [get_course()]) db( "INSERT INTO shortlinks (shortlink, url, creator, secure, course) VALUES (%s, %s, %s, %s, %s)", data, ) return html("Links updated")
def add_admin(course): email = request.form["email"] with connect_db() as db: check = db( "SELECT * FROM course_admins WHERE email=(%s) AND course=(%s)", [email, course], ).fetchone() if check: return error("User is already an admin"), 409 with connect_db() as db: db( "INSERT INTO course_admins VALUES (%s, %s, %s, %s)", [email, "Unknown", course, get_name()], ) # make sure that you can't accidentally lock yourself out with connect_db() as db: check = db( "SELECT * FROM course_admins WHERE email=(%s) AND course=(%s)", [get_email(), course], ).fetchone() if not check: db( "INSERT INTO course_admins VALUES (%s, %s, %s, %s)", [get_email(), get_name(), course, get_name()], ) return redirect(url_for("index"))
def handler(path): url, creator, secure = lookup(path) if not url: return error("Target not found!") if secure and not is_staff(get_course()): return login() return redirect(add_url_params(url, request.query_string.decode("utf-8")))