Example #1
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        else:
            cur = db.cursor()
            cur.execute('SELECT id FROM user WHERE username = %s',
                        (username, ))
            rv = cur.fetchone()
            cur.close()

            if rv is not None:
                error = 'User {} is already registered.'.format(username)

        if error is None:
            db = get_db()
            cur = db.cursor()
            cur.execute('INSERT INTO user (username, password) VALUES(%s,%s)',
                        (username, generate_password_hash(password)))
            db.commit()
            cur.close()

            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
Example #2
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        db = get_db()
        error = None
        cur = db.cursor(pymysql.cursors.DictCursor)
        cur.execute(
            'SELECT id, username,password FROM user WHERE username = %s',
            (username, ))
        user = cur.fetchone()
        cur.close()

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')
Example #3
0
def delete(id):
    get_post(id)
    db = get_db()
    cur = db.cursor()
    cur.execute('DELETE FROM post WHERE id = %s', (id, ))
    db.commit()
    cur.close()

    return redirect(url_for('blog.index'))
Example #4
0
def index():
    db = get_db()
    cur = db.cursor(pymysql.cursors.DictCursor)
    cur.execute('SELECT p.id, title, body, created, author_id, username'
                ' FROM post p JOIN user u ON p.author_id = u.id'
                ' ORDER BY created DESC')
    posts = cur.fetchall()
    cur.close()

    return render_template('blog/index.html', posts=posts)
Example #5
0
def load_logged_in_user():
    user_id = session.get('user_id')

    print('user_id = ' + str(user_id))
    if user_id is None:
        g.user = None
    else:
        db = get_db()
        cur = db.cursor(pymysql.cursors.DictCursor)
        cur.execute('SELECT * FROM user WHERE id = %s', (user_id, ))
        g.user = cur.fetchone()
        cur.close()
Example #6
0
def get_post(id, check_author=True):
    db = get_db()
    cur = db.cursor(pymysql.cursors.DictCursor)
    cur.execute(
        'SELECT p.id, title, body, created, author_id, username'
        ' FROM post p JOIN user u ON p.author_id = u.id'
        ' WHERE p.id = %s', (id, ))
    post = cur.fetchone()
    cur.close()

    if post is None:
        abort(404, "Post id {0} doesn't exist.".format(id))

    if check_author and post['author_id'] != g.user['id']:
        abort(403)

    return post
Example #7
0
def create():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            cur = db.cursor()
            cur.execute(
                'INSERT INTO post (title, body, author_id) VALUES (%s,%s,%s)',
                (title, body, g.user['id']))
            db.commit()
            cur.close()

            return redirect(url_for('blog.index'))

    return render_template('blog/create.html')
Example #8
0
def update():
    post = get_post(id)
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            cur = db.cursor()
            cur.execute(
                'UPDATE post SET title = %s, body = %s'
                ' WHERE id = %s', (title, body, id))
            db.commit()
            cur.close()

            return redirect(url_for('blog.index'))

    return render_template('blog/update.html', post=post)