Ejemplo n.º 1
0
def update(id):
    post = get_post(id)

    if request.method == 'POST':
        if "thread_id" in session:
            thread_id = session["thread_id"]
        if "category_id" in session:
            category_id = session["category_id"]
        body = request.form['body']
        error = None
        if not body:
            error = 'Body is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            query_db('UPDATE post SET body = %s'
                     ' WHERE id = %s', (
                         body,
                         id,
                     ))
            db.commit()
            return redirect(
                url_for('forum.thread',
                        category_id=category_id,
                        thread_id=thread_id))

    return render_template('forum/update.html', post=post)
Ejemplo n.º 2
0
def create():
    if request.method == 'POST':
        body = request.form['body']
        if "thread_id" in session:
            thread_id = session["thread_id"]
        if "category_id" in session:
            category_id = session["category_id"]
        error = None
        if not body:
            error = 'Body is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            query_db(
                'INSERT INTO post (body, author_id, thread_id, post_username)'
                ' VALUES ( %s, %s, %s, %s)',
                (body, g.user['id'], thread_id, g.user['username']))
            db.commit()
            return redirect(
                url_for('forum.thread',
                        category_id=category_id,
                        thread_id=thread_id))

    return render_template('forum/create.html')
Ejemplo n.º 3
0
def delete(id):
    if "thread_id" in session:
        thread_id = session["thread_id"]
    if "category_id" in session:
        category_id = session["category_id"]
    get_post(id)
    db = get_db()
    query_db('DELETE FROM post WHERE id = %s', (id, ))
    db.commit()
    return redirect(
        url_for('forum.thread', category_id=category_id, thread_id=thread_id))
Ejemplo n.º 4
0
def index():
    posts = query_db(
        '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'
    )
    return render_template('blog/index.html', posts=posts)
Ejemplo n.º 5
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = query_db('SELECT * FROM user WHERE id = %s', (user_id, ))[0]
Ejemplo n.º 6
0
def get_comment(id):
    comments = query_db(
        'SELECT username, body, created'
        ' FROM comment c JOIN user u ON c.reviewer_id = u.id'
        ' WHERE c.post_id = %s ORDER BY created DESC',
        (id))

    return comments
Ejemplo n.º 7
0
def thread(thread_id, category_id):
    session["thread_id"] = thread_id
    #db = get_db()
    posts = query_db('SELECT id, * FROM post WHERE thread_id=%s',
                     (thread_id, ))
    return render_template('forum/posts.html',
                           posts=posts,
                           thread_id=thread_id)
Ejemplo n.º 8
0
def get_post(id, check_author=True):
    post = query_db(
        '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,)
    )[0]

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

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

    return post
Ejemplo n.º 9
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        error = None
        try:
            user = query_db('SELECT * FROM user WHERE username = %s',
                            (username, ))[0]
        except IndexError:
            user = None
        if not user:
            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('space.personal', username=username))

        flash(error)

    return render_template('auth/login.html')
Ejemplo n.º 10
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif query_db('SELECT id FROM user WHERE username = %s', (username, )):
            error = 'User {} is already registered'.format(username)

        if error is None:
            if not insert_db(
                    'INSERT INTO user (username, password) VALUES (%s, %s)',
                (username, generate_password_hash(password))):
                error = 'System error, please try again.'
            else:
                return redirect(url_for('auth.login'))

        flash(error)
    return render_template('auth/register.html')
Ejemplo n.º 11
0
def personal(username):
    posts = query_db(
        'SELECT title, body, created, p.id, author_id'
        ' FROM post p JOIN user u ON u.username = %s and p.author_id = u.id',
        (username, ))
    return render_template("space/personal.html", posts=posts)
Ejemplo n.º 12
0
def posts():
    #db = get_db()
    posts = query_db('SELECT p.id, body, created, author_id, username'
                     ' FROM post p JOIN usertemp u ON p.author_id = u.id'
                     ' ORDER BY created DESC')
    return render_template('forum/index.html', posts=posts)
Ejemplo n.º 13
0
def category(category_id):
    session["category_id"] = category_id
    #db = get_db()
    threads = query_db('SELECT id, * FROM thread WHERE category_id=%s;',
                       (category_id, ))
    return render_template('forum/threads.html', threads=threads)
Ejemplo n.º 14
0
def index():
    #db = get_db()
    categories = query_db('SELECT id, * FROM category;')
    return render_template('forum/index.html', categories=categories)