def side_menu(): db = con() user_id = session.get("user_id") # select all data from users table semesters = db.execute("SELECT * FROM semesters WHERE user_id=?", (user_id, )) sub_sem_list = make_list(semesters) return dict(sub_sem_list=sub_sem_list)
def single(sub_id): """display all the assignments as per subjects""" db = con() assignments = db.execute("SELECT * FROM assignments WHERE subject_id =?", (sub_id, )).fetchall() subject = db.execute("SELECT * FROM subjects WHERE subject_id =?", (sub_id, )).fetchone() if len(assignments) < 1: return render_template("single.html", message="No subject found", subject=subject) return render_template("single.html", assignments=assignments, subject=subject)
def index(): """ gets all the data from database and display""" db = con() user_id = session.get("user_id") #"INSERT INTO users(first_name, last_name, email) VALUES ('Sandipa', 'Rijal', '*****@*****.**')" #table ="CREATE TABLE subjects (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, semester_id INTEGER, user_id INTEGER, FOREIGN KEY (user_id) REFERENCES users(id))" # select all data from users table user = db.execute("SELECT * FROM users WHERE id =?", (user_id, )) semesters = db.execute("SELECT * FROM semesters WHERE user_id=?", (user_id, )) sub_sem_list = make_list(semesters) # return the template index return render_template("index.html")
def semester(): """ add semesters """ db = con() # userId will come from session user_id = session.get("user_id") print(user_id) if request.method == "POST": title = request.form.get("title") if not title: flash("Semester cannot be blanked") return redirect("/manage/semester") db.execute("INSERT INTO semesters (title, user_id) VALUES (?,?)", (title, user_id)) get_db().commit() return redirect("/manage/semester") datas = db.execute("SELECT * FROM semesters WHERE user_id =?", (user_id, )) return render_template("semester.html", datas=datas)
def confirm_email(token): db = con() try: email = s.loads(token, salt='email-confirm', max_age=3600) except SignatureExpired: return 'The Token is expired' except BadTimeSignature: return 'Invalid Token' user = db.execute("SELECT * FROM users WHERE email=?", (email, )).fetchall() if len(user) != 1: return "Invalid Token" db.execute("UPDATE users SET confirm_email =1 WHERE id =?", (user[0]['id'], )) get_db().commit() return redirect("/login")
def register(): db = con() if request.method == "POST": first_name = request.form.get("first_name") last_name = request.form.get("last_name") email = request.form.get("email") password = request.form.get("password") password_confirmation = request.form.get("password_confirmation") found = db.execute("SELECT email FROM users WHERE email=?", (email, )).fetchall() if len(found) >= 1: return render_template("register.html", message="Email already exist") if not first_name and not last_name: return render_template("register.html", message="Provide name") if not email: return render_template("register.html", message="Provide Email") if not password and not password_confirmation: return render_template("register.html", message="Provide password") if password != password_confirmation: return render_template("register.html", message="Password does not match") hashed_pass = generate_password_hash(password) db.execute( "INSERT INTO users (first_name, last_name, email, hash_password, confirm_email, admin) VALUES (?, ?, ?,?, 0, 0)", (first_name, last_name, email, hashed_pass)) get_db().commit() token = s.dumps(email, salt='email-confirm') msg = Message('Confirm email', sender='*****@*****.**', recipients=[email]) link = url_for('confirm_email', token=token, _external=True) msg.body = f'Thank you for signing up. Please click this link for activate your account {link}' mail.send(msg) return redirect("/") else: return render_template("register.html")
def subject(semester_id=None): db = con() user_id = session.get("user_id") sem_id = semester_id # display subjects subjects = db.execute( """SELECT subjects.subject_id, subjects.subject_title, semesters.id, semesters.title FROM subjects LEFT JOIN semesters ON subjects.semester_id = semesters.id WHERE subjects.user_id= ?""", (user_id, )).fetchall() semesters = db.execute("SELECT id, title FROM semesters WHERE user_id =?", (user_id, )) # add new subject to database if request.method == "POST": title = request.form.get('subject') if not title: return render_template("subject.html", subjects=subjects, semesters=semesters, message="Title cannot be blanked") semester_id = request.form.get('semesters_list') if semester_id == "select": return render_template("subject.html", subjects=subjects, semesters=semesters, message="Please select semester") # save to database db.execute( "INSERT INTO subjects (subject_title, semester_id, user_id) VALUES(?,?,?)", (title, semester_id, user_id)) get_db().commit() return redirect("manage/subject") return render_template("subject.html", subjects=subjects, semesters=semesters)
def login(): """ Log in """ db = con() if request.method == "POST": # assign user input to variable email = request.form.get("email") password = request.form.get("password") # check email and password provided if not email: return render_template("login.html", message="Please provie email") if not password: return render_template("login.html", message="Please provie password") # query email row = db.execute("SELECT * FROM users WHERE email =?", (email, )).fetchall() # verify email and password if len(row) != 1 or not check_password_hash(row[0]["hash_password"], password): return render_template("login.html", message="Email/Password did not match") if not row[0]['confirm_email']: return render_template("login.html", message="Activate your account") if row[0]['admin']: session["admin_id"] = row[0]['id'] return redirect(url_for('admin')) # remember user id session["user_id"] = row[0]["id"] session["user_name"] = row[0]["first_name"] #redirect to index return redirect("/") else: return render_template("login.html")
def note(): db = con() user_id = session.get("user_id") if request.method == "POST": text = request.form['notes'] print(text) assign_id = request.form['assign_id'] note = db.execute("SELECT * FROM notes WHERE assign_id = ?", (assign_id, )).fetchall() if len(note) < 1: db.execute( "INSERT INTO notes (note_title, user_id, assign_id) VALUES(?,?,?)", (text, user_id, assign_id)) get_db().commit() return redirect(url_for('assignment')) else: db.execute("UPDATE notes SET note_title= ? WHERE assign_id =?", (text, assign_id)) get_db().commit() return redirect(url_for('assignment')) return render_template("note.html")
def myaccount(): user_id = session.get("user_id") db = con() details = db.execute( "SELECT first_name, last_name, email FROM users WHERE id=?", (user_id, )) if request.method == "POST": reset_password = request.form.get('reset_password') if not reset_password: return render_template('user.html', details=details, message="Password cannot be empty.") confirm_reset_password = request.form['confirm_reset_password'] if not confirm_reset_password: return render_template('user.html', details=details, message="Confirm field cannot be empty.") if reset_password != confirm_reset_password: return render_template('user.html', details=details, message="Password does not match.") hash_password = generate_password_hash(confirm_reset_password) success = db.execute("UPDATE users SET hash_password=? WHERE id=?", (hash_password, user_id)).rowcount get_db().commit() if success: flash("Password updated!") return redirect("/myaccount") else: flash("Error: Password could not updated!") return redirect("/myaccount") return render_template('user.html', details=details)
def delete(subject_id=None, sem_id=None, assign_id=None): user_id = session.get("user_id") db = con() if subject_id: #check subject empty or not #delete subject db.execute("DELETE FROM subjects WHERE subject_id = ? and user_id =?", (subject_id, user_id)) get_db().commit() flash("Item deleted") return redirect("/manage/subject") elif sem_id: # delete semester and its subjects db.execute("DELETE FROM subjects WHERE semester_id = ? and user_id =?", (sem_id, user_id)) get_db().commit() db.execute("DELETE FROM semesters WHERE id = ? and user_id =?", (sem_id, user_id)) get_db().commit() flash("Item deleted") return redirect("/manage/semester") elif assign_id: # delete semester and its subjects db.execute( "DELETE FROM assignments WHERE assign_id = ? and user_id =?", (assign_id, user_id)) get_db().commit() flash("Item deleted") return redirect("/manage/assignment")
def update(subject_id=None, assign_id=None, sem_id=None): # connection to data base db = con() user_id = session.get("user_id") if subject_id: # check semester exist in database datas = db.execute("SELECT * FROM subjects where subject_id=?", (subject_id, )).fetchone() if len(datas) < 1: return page_not_found(404) if request.method == "POST": title = request.form.get("title") if not title: flash("Subject cannot be blanked") return redirect(f"/manage/semester/{datas['semester_id']}") db.execute( "UPDATE subjects SET subject_title=? WHERE subject_id =? and user_id =?", (title, subject_id, user_id)) # save the changes into database get_db().commit() flash("Subject updated") return redirect("/manage/subject") return render_template("update.html", datas=datas) if sem_id: # check semester exist in database semester = db.execute("SELECT * FROM semesters WHERE id=?", (sem_id, )).fetchone() if len(semester) < 1: return page_not_found(404) if request.method == "POST": title = request.form.get("title") if not title: flash("Semester cannot be blanked") return redirect(f"/manage/semester/update/{semester['id']}") # save the changes into database db.execute( "UPDATE semesters SET title=? WHERE id = ? and user_id =?", (title, semester['id'], user_id)) get_db().commit() flash("Semester updated") return redirect("manage/semester") return render_template("update.html", semester=semester) if assign_id: # check semester exist in database assignment = db.execute("SELECT * FROM assignments WHERE assign_id=?", (assign_id, )).fetchone() subjects = db.execute( "SELECT * FROM subjects WHERE user_id=? and subject_id !=?", (user_id, assignment['subject_id'])).fetchall() subject = db.execute( "SELECT subject_id,subject_title FROM subjects WHERE subject_id=?", (assignment['subject_id'], )).fetchone() if len(assignment) < 1: return page_not_found(404) if request.method == "POST": title = request.form.get("title") if not title: flash("Assignment cannot be blanked") return redirect("/manage/assignment") subject_id = request.form.get("subjects_list") due_date = request.form.get("due_date") due_date = format_date(due_date) # save the changes into database db.execute( "UPDATE assignments SET assign_title=?, subject_id =?, due_date=DATETIME(?) WHERE assign_id =? and user_id =?", (title, subject_id, due_date, assign_id, user_id)) get_db().commit() flash("Updated") return redirect("/manage/assignment") return render_template("update.html", assignment=assignment, subjects=subjects, subject=subject)
def assignment(assign_id=None): db = con() user_id = session.get("user_id") # display subjects semester and assignment assignments = db.execute( """SELECT assignments.assign_id, assignments.assign_title, DATE(assignments.due_date), subjects.subject_title, semesters.title FROM assignments LEFT JOIN (semesters INNER JOIN subjects ON subjects.semester_id =semesters.id) ON assignments.subject_id = subjects.subject_id WHERE assignments.user_id = ?""", (user_id, )).fetchall() semesters = db.execute("SELECT * FROM semesters WHERE user_id=?", (user_id, )).fetchall() subjects = db.execute("SELECT * FROM subjects WHERE user_id=?", (user_id, )).fetchall() # get method for displaying assignment note if assign_id: note = db.execute("SELECT * FROM notes WHERE assign_id =?", (assign_id, )).fetchone() if not note: return jsonify({"error": "no data found"}) return jsonify(dict(note)) # add new subject to database if request.method == "POST": title = request.form.get('assignment') if not title: return render_template("assignment.html", subjects=subjects, semesters=semesters, assignments=assignments, message="Title cannot be blanked") subject_id = request.form.get('subjects_list') if subject_id == "select": return render_template("assignment.html", subjects=subjects, semesters=semesters, assignments=assignments, message="Please select Subject") semester_id = db.execute( "SELECT semester_id FROM subjects WHERE subject_id=?", (subject_id, )).fetchone() due_date = request.form.get('due_date') due_date = due_date.split("/") due_date.reverse() due_date = '-'.join(due_date) # save to database db.execute( "INSERT INTO assignments (assign_title, semester_id, subject_id, user_id, due_date) VALUES(?,?,?,?,DATETIME(?))", (title, semester_id['semester_id'], subject_id, user_id, due_date)) get_db().commit() return redirect("manage/assignment") return render_template("assignment.html", subjects=subjects, semesters=semesters, assignments=assignments)
def admin(): db = con() users = db.execute("SELECT * FROM users").fetchall() return render_template("admin.html", users=users)