Ejemplo n.º 1
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_session().query(User).filter(User.id == user_id).first()
Ejemplo n.º 2
0
def delete(id):
    get_post(id)
    db = get_session()
    post = db.query(Post).filter(Post.id == id).first()
    db.delete(post)
    db.commit()
    db.close()
    return redirect(url_for('blog.index'))
Ejemplo n.º 3
0
def get_post(id, check_author=True):
    post = get_session().query(Post).join(User).filter(Post.id == id).first()
    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
Ejemplo n.º 4
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        orm_session = get_session()
        user = orm_session.query(
            User.UserId).filter(User.UserId == user_id).first()
        g.user = user[0]
Ejemplo n.º 5
0
def flag_comment(id):
    user_id = g.user.id
    db = get_session()
    if db.query(FlaggedComment).filter(FlaggedComment.user_id == user_id)\
        .filter(FlaggedComment.comment_id == id).first() is None :

        flagged_comment = FlaggedComment(user_id, id)
        db.add(flagged_comment)
        db.commit()
        db.close()
    return redirect(url_for('blog.index'))
Ejemplo n.º 6
0
def index():
    db = get_session()
    posts = db.query(Post, User)\
        .join(User)\
        .all()
    comments = db.query(Comment, User)\
    .join(User)\
    .all()
    flagged_comments = db.query(FlaggedComment.comment_id, func.count(FlaggedComment.comment_id))\
        .group_by(FlaggedComment.comment_id).all()
    return render_template('blog/index.html',
                           posts=posts,
                           comments=comments,
                           flagged_comments=flagged_comments)
Ejemplo n.º 7
0
def comment(id):
    if request.method == 'POST':
        body = request.form['body']
        error = None

        if not body:
            error = 'Body is required.'

        if error is not None:
            flash(error)
        else:
            db = get_session()
            comment = Comment(body, g.user.id, id)
            db.add(comment)
            db.commit()
            db.close()
            return redirect(url_for('blog.index'))

    return render_template('blog/comment.html')
Ejemplo n.º 8
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_session()
        error = None
        user = db.query(User).filter(User.username == username).first()

        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')
Ejemplo n.º 9
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        orm_session = get_session()
        error = None
        user = orm_session.query(User).filter(User.UserId == username).first()

        if user is None:
            error = 'Incorrect username.'
        elif not user.HashedPassword == password:
            error = 'Incorrect password.'

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

        flash(error)

    return render_template('auth/login.html')
Ejemplo n.º 10
0
def update(id):
    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_session()
            post = db.query(Post).filter(Post.id == id).first()
            post.title = title
            post.body = body
            db.commit()
            db.close()
            return redirect(url_for('blog.index'))

    return render_template('blog/update.html', post=post)
Ejemplo n.º 11
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 not body:
            error = 'Body is required.'

        if error is not None:
            flash(error)
        else:
            db = get_session()
            post = Post(title, body, g.user.id)
            db.add(post)
            db.commit()
            db.close()
            return redirect(url_for('blog.index'))

    return render_template('blog/create.html')
Ejemplo n.º 12
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_session()
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        elif len(db.query(User).filter(User.username == username).all()) != 0:
            error = 'User {} is already registered.'.format(username)

        if error is None:
            user = User(username, generate_password_hash(password))
            db.add(user)
            db.commit()
            db.close()
            return redirect(url_for('auth.login'))

        flash(error)

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

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'
        else:
            if len(
                    orm_session.query(User.UserId).filter(
                        User.UserId == username).all()) > 0:
                error = 'User {} is already registered.'.format(username)

        if error is None:
            new_user = User(UserId=username, HashedPassword=password)
            orm_session.add(new_user)
            orm_session.commit()
            return redirect(url_for('auth.login'))

    return render_template('auth/register.html')