Exemplo n.º 1
0
def signup():
    conn = db_connection()
    cur = conn.cursor()
    msg = ''
    if request.method == 'POST' and 'name' in request.form and 'password' in request.form and 'email' in request.form:
        # Create variables for easy access
        name = request.form['name']
        password = request.form['password']
        password = generate_password_hash(password)
        email = request.form['email']
        sql = "SELECT * FROM user WHERE email=? and password=?"
        cur.execute(sql, (email, password))
        account = cur.fetchone()
        if account:
            msg = 'You already have an account, login please!'
        elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
            msg = 'Invalid email address!'
        elif not re.match(r'[A-Za-z0-9]+', name):
            msg = 'Username must contain only characters and numbers!'
        elif not name or not password or not email:
            msg = 'Please fill out the form!'
        else:
            sql = "INSERT INTO user VALUES (NULL, ?, ?, ?)"
            cur.execute(sql, (email, password, name))
            conn.commit()
            conn.close()
            flash('You have successfully registered!')
            return redirect(url_for('auth.login'))
    elif request.method == 'POST':
        # Form is empty... (no POST data)
        msg = 'Please fill out the form!'
    # Show registration form with message (if any)
    return render_template('signup.html', msg=msg)
Exemplo n.º 2
0
def books():
    conn = db_connection()
    cur = conn.cursor()
    if request.method == "GET":
        cursor = conn.execute("SELECT * FROM book")
        result = [
            dict(id=row[0], author=row[1], title=row[2],
            shortDescription=row[3], thumbnailUrl=row[4], status=row[5],
            pageCount=row[6])
            for row in cursor.fetchall()
        ]
        if result is not None:
            return jsonify(result)

    if request.method == "POST":
        new_author = request.form["author"]
        new_title = request.form["title"]
        new_sDescription = request.form["shortDescription"]
        new_Url = request.form["thumbnailUrl"]
        new_status = request.form["status"]
        new_pageCount = request.form["pageCount"]

        sql = """INSERT INTO book (author, title, shortDescription, thumbnailUrl,
        status, pageCount)
                 VALUES (?, ?, ?, ?, ?, ?)"""
        cursor = cur.execute(sql, (new_author, new_title, new_sDescription,
        new_Url, new_status, new_pageCount))
        conn.commit()
        conn.close()
        return f"Book with the id: {cursor.lastrowid} created successfully", 201
Exemplo n.º 3
0
def home():
    conn = db_connection()
    cur = conn.cursor()
    cursor = conn.execute("SELECT * FROM book")
    result = [
        dict(id=row[0], author=row[1], title=row[2],
             shortDescription=row[3], thumbnailUrl=row[4], status=row[5],
             pageCount=row[6])
        for row in cursor.fetchall()
    ]
    if result is not None:
        return render_template('home.html', books=result)
Exemplo n.º 4
0
def edit_book(book_id):
    conn = db_connection()
    cursor = conn.cursor()
    book = {}
    if request.method == 'GET':
        cursor.execute("SELECT * FROM book WHERE id=?", (book_id,))
        rows = cursor.fetchall()
        for r in rows:
            book = r
        book = {
            "id": book[0],
            "author": book[1],
            "title": book[2],
            "shortDescription": book[3],
            "thumbnailUrl": book[4],
            "status": book[5],
            "pageCount": book[6],

        }
        return render_template('book_edit.html', book=book)

    if request.method == 'POST':
        sql = """UPDATE book
                        SET author=?,
                            pageCount=?,
                            shortDescription=?,
                            status=?,
                            thumbnailUrl=?,
                            title=?
                        WHERE id=? """
        author = request.form["author"]
        pageCount = request.form["pageCount"]
        shortDescription = request.form["shortDescription"]
        status = request.form["status"]
        thumbnailUrl = request.form["thumbnailUrl"]
        title = request.form["title"]
        updated_book = {
            "id": book_id,
            "author": author,
            "pageCount": pageCount,
            "shortDescription": shortDescription,
            "status": status,
            "thumbnailUrl": thumbnailUrl,
            "title": title,
        }
        conn.execute(sql, (author, pageCount, shortDescription,
                           status, thumbnailUrl, title, book_id))
        conn.commit()
        return redirect(url_for('website.single_book', id=book_id))
Exemplo n.º 5
0
def books():
    conn = db_connection()
    cur = conn.cursor()
    if request.method == "POST":
        new_author = request.form["author"]
        new_title = request.form["title"]
        new_sDescription = request.form["shortDescription"]
        new_tUrl = request.form["thumbnailUrl"]
        new_status = request.form["status"]
        new_pageCount = request.form["pageCount"]

        sql = """INSERT INTO book (author, title, shortDescription, thumbnailUrl,
        status, pageCount)
                         VALUES (?, ?, ?, ?, ?, ?)"""
        cursor = cur.execute(sql, (new_author, new_title, new_sDescription,
                                   new_tUrl, new_status, new_pageCount))
        conn.commit()
        conn.close()
        return redirect(url_for('website.home'), code=302)
Exemplo n.º 6
0
def login():
    conn = db_connection()
    cur = conn.cursor()
    if request.method == 'POST' and 'email' in request.form and 'password' in request.form:
        print('login req get')
        email = request.form['email']
        password = request.form['password']
        sql = "SELECT * FROM user WHERE email=? and password=?"
        cur.execute(sql, (email, password))
        account = cur.fetchone()
        print(account)
        if account:
            session['logged_in'] = True
            session['id'] = account[0]
            session['email'] = account[1]
            session['name'] = account[3]
            # flash('LoggedIn successfully!')
            return redirect(url_for('website.home'))
    if request.method == 'GET':
        return render_template('login.html')
Exemplo n.º 7
0
def single_book(id):
    conn = db_connection()
    cursor = conn.cursor()
    book = {}
    if request.method == "GET":
        cursor.execute("SELECT * FROM book WHERE id=?", (id,))
        rows = cursor.fetchall()
        for r in rows:
            book = r
        book = {
            "id": book[0],
            "author": book[1],
            "title": book[2],
            "shortDescription": book[3],
            "thumbnailUrl": book[4],
            "status": book[5],
            "pageCount": book[6],

        }
        if book is not None:
            return render_template('book_detail.html', book=book)
        else:
            return "Something wrong", 404
Exemplo n.º 8
0
def single_book(id):
    conn = db_connection()
    cursor = conn.cursor()
    book = {}
    if request.method == "GET":
        cursor.execute("SELECT * FROM book WHERE id=?", (id,))
        rows = cursor.fetchall()
        for r in rows:
            book = r
        book = {
            "id": book[0],
            "author": book[1],
            "title": book[2],
            "shortDescription": book[3],
            "thumbnailUrl": book[4],
            "status": book[5],
            "pageCount": book[6],
            
        }
        conn.close()
        if book is not None:
            return jsonify(book), 200
        else:
            return "Something wrong", 404
    if request.method == "PUT":
        sql = """UPDATE book
                SET author=?,
                    pageCount=?,
                    shortDescription=?,
                    status=?,
                    thumbnailUrl=?,
                    title=?
                WHERE id=? """
        author = request.form["author"]
        pageCount = request.form["pageCount"]
        shortDescription = request.form["shortDescription"]
        status = request.form["status"]
        thumbnailUrl = request.form["thumbnailUrl"]
        title = request.form["title"]
        updated_book = {
            "id": id,
            "author": author,
            "pageCount": pageCount,
            "shortDescription": shortDescription,
            "status": status,
            "thumbnailUrl": thumbnailUrl,
            "title": title,
        }
        conn.execute(sql, (author, pageCount, shortDescription,
        status, thumbnailUrl, title, id))
        conn.commit()
        conn.close()
        return jsonify(updated_book)

    if request.method == "DELETE":
        sql_query = "DELETE FROM book WHERE id=?"
        try:
            conn.execute(sql_query, (id,))
            conn.commit()
            conn.close()
            return 'Done with delete'
        except Error:
            print(Error.__name__)
            raise Error