コード例 #1
0
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()
コード例 #2
0
def findAllJournals():
    con = dbconnection()
    # con.row_factory = sqlite3.Row
    cur = con.cursor()
    cur.execute("SELECT * FROM Journal")
    rows = cur.fetchall()
    return rows
コード例 #3
0
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()
コード例 #4
0
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)
コード例 #5
0
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()
コード例 #6
0
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()
コード例 #7
0
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'))
コード例 #8
0
def journals():
    con = dbconnection()
    cur = con.cursor()
    cur.execute("SELECT * FROM Journal")
    rows = cur.fetchall()
    return render_template("journals.html", journals=rows)