def delete_period(): """Deletes one period""" s = request.form.get("subject") t = request.form.get("type") l = request.form.get("lecturer") d = request.form.get("day") p = request.form.get("place") st = request.form.get("start") e = request.form.get("end") if not s or not t or not l or not d or not p or not st or not e: return apology("something unusual happened") q = db.execute("SELECT * FROM subjects WHERE user_id = :id AND subject = :s AND type = :t AND lecturer = :l AND day = :d AND place = :p AND start_time = :st AND end_time = :e", {"id": session["user_id"], "s": s, "t": t, "l": l, "d": d, "p": p, "st": st, "e": e}).fetchall() if not q: return apology(message="subject doesn't exist") db.execute("DELETE FROM subjects WHERE user_id = :id AND subject = :s AND type = :t AND lecturer = :l AND day = :d AND place = :p AND start_time = :st AND end_time = :e", {"id": session["user_id"], "s": s, "t": t, "l": l, "d": d, "p": p, "st": st, "e": e}) db.commit() # Update session['subjects'] (used in navbar and filling forms) because this might be the last period of the subject (which deletes it entirely) subject_count = db.execute("SELECT COUNT(subject) FROM subjects WHERE user_id = :id AND subject = :s", {"id": session["user_id"], 's': s}).fetchone()[0] if subject_count == 0: session["subjects"] = rowproxy_to_dict(db.execute("SELECT DISTINCT subject FROM subjects WHERE user_id = :id ORDER BY subject", {"id": session["user_id"]}).fetchall()) # Delete the subject's dues db.execute("DELETE FROM dues WHERE user_id = :id AND subject = :s", {"id": session["user_id"], "s": s}) # Delete the subjetc's notes db.execute("DELETE FROM notes WHERE user_id = :id AND subject = :s", {"id": session["user_id"], "s": s}) db.commit() flash("Period deleted!") return redirect("/schedule")
def delete_subject(): """Deletes entire subject""" subject = request.form.get("subject") if not subject: return apology("subject doesn't exist") try: db.execute("DELETE FROM subjects WHERE user_id = :id AND subject = :s", { "id": session["user_id"], "s": subject }) db.execute("DELETE FROM dues WHERE user_id = :id AND subject = :s", { "id": session["user_id"], "s": subject }) db.execute("DELETE FROM notes WHERE user_id = :id AND subject = :s", { "id": session["user_id"], "s": subject }) db.commit() except: return apology("somethign went wrong") # Update the session['subjects'] session["subjects"] = rowproxy_to_dict( db.execute( "SELECT DISTINCT subject FROM subjects WHERE user_id = :id ORDER BY subject", { "id": session["user_id"] }).fetchall()) flash("Subject deleted!") return redirect("/")
def edit_period(): """Edit a subject""" if request.method == "POST": # recieved_token = request.form.get("token") # if not recieved_token or recieved_token != session["form_token"]: # return apology("f**k off cross-site forgeing piece of shit") subject = request.form.get("subject") subject_type = request.form.get("type") lecturer = request.form.get("lecturer") place = request.form.get("place") start = request.form.get("start") end = request.form.get("end") day = request.form.get("day") old_subject = request.form.get("old_subject") old_subject_type = request.form.get("old_type") old_lecturer = request.form.get("old_lecturer") old_place = request.form.get("old_place") old_start = request.form.get("old_start") old_end = request.form.get("old_end") old_day = request.form.get("old_day") if not subject or not subject_type or not lecturer or not place or not start or not end or not day: return apology("please fill the form") subject = subject.title() lecturer = lecturer.title() place = place.title() day = day.capitalize() if not old_subject or not old_subject_type or not old_lecturer or not old_place or not old_start or not old_end or not old_day: return apology("something went form") try: db.execute("UPDATE subjects SET subject = :s, type = :t, lecturer = :l, place = :p, start_time = :st, end_time = :e, day = :d WHERE user_id = :id AND subject = :o_s AND type = :o_t AND lecturer = :o_l AND place = :o_p AND start_time = :o_st AND end_time = :o_e AND day = :o_d", {"s": subject, "t": subject_type, "p": place, "l": lecturer, "st": start, "e": end, "d": day, "id": session["user_id"], "o_s": old_subject, "o_t": old_subject_type, "o_p": old_place, "o_l": old_lecturer, "o_st": old_start, "o_e": old_end, "o_d": old_day}) db.commit() except: return apology("something went wrong") session["subjects"] = rowproxy_to_dict(db.execute("SELECT DISTINCT subject FROM subjects WHERE user_id = :id ORDER BY subject", {"id": session["user_id"]}).fetchall()) flash("Period edited!") return redirect(f"/subjects/{subject}") else: # token = secrets.token_hex() # session["form_token"] = token subject = request.args.get("subject") subject_type = request.args.get("type") lecturer = request.args.get("lecturer") place = request.args.get("place") start = request.args.get("start") end = request.args.get("end") day = request.args.get("day") if not subject or not subject_type or not lecturer or not place or not start or not end or not day: return apology(message="please fill the form") q = db.execute("SELECT * FROM subjects WHERE user_id = :id AND subject = :s AND type = :t AND lecturer = :l AND place = :p AND start_time = :s_t AND end_time = :e AND day = :d", {"id": session["user_id"], "s": subject, "t": subject_type, "l": lecturer, "p": place, "s_t": start, "e": end, "d": day}).fetchall() if not q: return apology("subject doesn't exist") return render_template("periods/edit_period.html", subject=subject, type=subject_type, lecturer=lecturer, place=place, start=start, end=end, day=day)
def data(date): """Return today's data as an HTML snippet""" try: quote = cache["quote"] except: cache["quote"] = quote_of_the_day() quote = cache["quote"] date = date.split("-") today_date = datetime(year=int(date[0]), month=int(date[1]), day=int(date[2])) today_subjects = db.execute( "SELECT * FROM subjects WHERE user_id = :id AND day = :day ORDER BY start_time", { "id": session["user_id"], "day": today_date.strftime("%A") }).fetchall() tomorrow_subjects = db.execute( "SELECT * FROM subjects WHERE user_id = :id AND day = :tomorrow ORDER BY start_time", { "id": session["user_id"], "tomorrow": (today_date + timedelta(days=1)).strftime("%A") }).fetchall() res = db.execute( "SELECT DISTINCT subject FROM subjects WHERE user_id = :id ORDER BY subject", { "id": session["user_id"] }).fetchall() #changed RowProxy result to python dict cuz sessions can't deal with RowProxy cuz they are unserializable json session["subjects"] = rowproxy_to_dict(res) week = today_date + timedelta(days=7) # Getting this week's dues dues = db.execute( "SELECT * FROM dues WHERE user_id = :id AND deadline <= :w AND deadline >= :d ORDER BY deadline", { "id": session["user_id"], 'w': week, "d": today_date.strftime("%Y-%m-%d") }).fetchall() #head = render_template("head.html", subjects=today_subjects, tomorrow_subjects=tomorrow_subjects, dues=dues, quote=quote) main = render_template("main/main.html", subjects=today_subjects, tomorrow_subjects=tomorrow_subjects, dues=dues, quote=quote) return jsonify({ "main": main, "periods_count": len(today_subjects), "dues_count": len(dues) })
def add_period(): """Adds a period""" subject = request.form.get("subject") subject_type = request.form.get("type") lecturer = request.form.get("lecturer") place = request.form.get("place") start = request.form.get("start") end = request.form.get("end") days = request.form.getlist("day") if not subject or not subject_type or not lecturer or not place or not start or not end or not days: return apology(message="please fill the form") if end <= start: return apology("end time is before start time") if len(subject) > 80: return apology("maximum subject length is 80 characters") if (len(lecturer) or len(place)) > 50: return apology("maximum lecturer or place length is 50 characters") # days = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] # day = days.index(day) subject = subject.strip().title() subject_type = subject_type.capitalize() lecturer = lecturer.strip().title() place = place.strip().title() if subject_type not in ["Lab", "Lecture", "Section"]: return apology("invalid subject type") for day in days: q = db.execute("SELECT * FROM subjects WHERE user_id = :id AND subject = :s AND type = :t AND lecturer = :l AND place = :p AND start_time = :s_t AND end_time = :e AND day = :d", {"id": session["user_id"], "s": subject, "t": subject_type, "l": lecturer, "p": place, "s_t": start, "e": end, "d": day}).fetchall() if q: continue try: db.execute("INSERT INTO subjects (user_id, subject, type, lecturer, place, start_time, end_time, day) VALUES(:id, :subject, :type, :lecturer, :place, :start, :end, :day)", {"id": session["user_id"], "subject": subject, "type": subject_type, "lecturer": lecturer, "place": place, "start": start, "end": end, "day": day}) db.commit() session["subjects"] = rowproxy_to_dict(db.execute("SELECT DISTINCT subject FROM subjects WHERE user_id = :id ORDER BY subject", {"id": session["user_id"]}).fetchall()) except: return apology("something went wrong with the database") flash("Period added!") return redirect(f"/subjects/{subject}")
def rename_subject(): old_name = request.form.get("old_name") new_name = request.form.get("new_name") if not old_name or not new_name: return apology("something went wrong") try: new_name = new_name.strip().title() if len(new_name) < 2: return apology("Nnew name must be more than 1 character long") db.execute( "UPDATE subjects SET subject = :new_name WHERE subject = :old_name AND user_id = :id", { "new_name": new_name, "old_name": old_name, "id": session["user_id"] }) db.execute( "UPDATE dues SET subject = :new_name WHERE subject = :old_name AND user_id = :id", { "new_name": new_name, "old_name": old_name, "id": session["user_id"] }) db.execute( "UPDATE notes SET subject = :new_name WHERE subject = :old_name AND user_id = :id", { "new_name": new_name, "old_name": old_name, "id": session["user_id"] }) db.commit() session["subjects"] = rowproxy_to_dict( db.execute( "SELECT DISTINCT subject FROM subjects WHERE user_id = :id ORDER BY subject", { "id": session["user_id"] }).fetchall()) flash("Subject name updated!") return redirect(f"/subjects/{new_name.replace(' ', '_')}") except: return apology("something went wrong", 403)
def index(): """Display index page""" try: quote = cache["quote"] except: cache["quote"] = quote_of_the_day() quote = cache["quote"] today_subjects = db.execute( "SELECT * FROM subjects WHERE user_id = :id AND day = :day ORDER BY start_time", { "id": session["user_id"], "day": session['today_name'] }).fetchall() tomorrow_subjects = db.execute( "SELECT * FROM subjects WHERE user_id = :id AND day = :next ORDER BY start_time", { "id": session["user_id"], "next": session['tomorrow_name'] }).fetchall() res = db.execute( "SELECT DISTINCT subject FROM subjects WHERE user_id = :id ORDER BY subject", { "id": session["user_id"] }).fetchall() #changed RowProxy result to python dict cuz sessions can't deal with RowProxy cuz they are unserializable json session["subjects"] = rowproxy_to_dict(res) week = session['today_date_object'] + timedelta(days=7) # Getting this week's dues dues = db.execute( "SELECT * FROM dues WHERE user_id = :id AND deadline <= :w AND deadline >= :d ORDER BY deadline", { "id": session["user_id"], 'w': week, "d": session['today_date'] }).fetchall() return render_template("index.html", subjects=today_subjects, tomorrow_subjects=tomorrow_subjects, dues=dues, quote=quote)