def updateJournal(): msg = "" try: _id = request.form['id'] title = request.form['title'] date = request.form['date'] # author = request.form['author'] tag = request.form['tag'] emotion = request.form['emotion'] content = request.form['content'] con = dbconnection() cur = con.cursor() cur.execute( "UPDATE Journal SET title = %s, date = %s, content = %s, emotion = %s, tag = %s WHERE id = %s", (title, date, content, emotion, tag, _id)) con.commit() msg = "Journal successfully updated!" return redirect("/journals") except Exception as e: print("Error " + e) con.rollback() msg = "There is an error, rollback the change." finally: cur.close() con.close()
def findAllJournals(): con = dbconnection() # con.row_factory = sqlite3.Row cur = con.cursor() cur.execute("SELECT * FROM Journal") rows = cur.fetchall() return rows
def addJournal(): msg = "" try: # Insert to DB title = request.form['title'] date = request.form['date'] author = request.form['author'] tag = request.form['tag'] emotion = request.form['emotion'] content = request.form['content'] con = dbconnection() cur = con.cursor() cur.execute( "INSERT INTO Journal (title, date, author, tag, emotion, content) VALUES (%s,%s,%s,%s,%s,%s); ", (title, date, author, tag, emotion, content)) con.commit() msg = "Journal successfully saved" return redirect("/journals") except Exception as e: print("Hey there is an error: " + e) msg = e finally: cur.close() con.close()
def findUserByEmail(email): try: con = dbconnection() cur = con.cursor() cur.execute("select * from users where email = %s", (email, )) row = cur.fetchone() if row is None: return None else: user = User(row[0], row[1], row[2], row[3], row[4]) return user except Exception as e: print("error" + e)
def deleteJournal(id): msg = "" try: con = dbconnection() cur = con.cursor() cur.execute("delete from Journal where id = %s", (str(id), )) con.commit() msg = "Journal deleted successfully!" # return render_template("result.html", msg=msg) return redirect("/journals") except Exception as e: print("Error " + e) con.rollback() msg = "There is an error while deleting." finally: cur.close() con.close()
def editJournal(id): try: con = dbconnection() cur = con.cursor() cur.execute("select * from Journal where id = %s", (str(id), )) row = cur.fetchone() print("Edit") print(row) if row: return render_template("editJournal.html", aJournal=row) else: msg = "Cannot find the journal with this id" except Exception as e: print("There is an error", e) msg = e finally: cur.close() con.close()
def signup(): name = request.form.get('name') email = request.form.get('email') password = request.form.get('password') role = 'user' print("signup: " + name + " " + email + " " + password) user = findUserByEmail(email) if user: flash("Email address already exists") return redirect(url_for('signup')) con = dbconnection() cur = con.cursor() cur.execute( "INSERT INTO users (email, password, name, role) VALUES (%s,%s,%s,%s); ", (email, generate_password_hash(password, method='sha256'), name, role)) con.commit() return redirect(url_for('login'))
def journals(): con = dbconnection() cur = con.cursor() cur.execute("SELECT * FROM Journal") rows = cur.fetchall() return render_template("journals.html", journals=rows)