예제 #1
0
def edit_post(post_id):
    try:
        post = dao.query(Post).filter_by(id=post_id).one()
        return render_template('post-edit.html', post=post)
        # render post edit page
    except NoResultFound:
        abort(404)
    except MultipleResultsFound:
        abort(404)
예제 #2
0
def delete_post(post_id):
    try:
        post = dao.query(Post).filter_by(id=post_id).one()
        dao.delete(post)
        dao.commit()
        return redirect(url_for('index'))
    except NoResultFound:
        abort(404)
    except MultipleResultsFound:
        abort(404)
예제 #3
0
def read_post(post_id):
    try:
        post = dao.query(Post).filter_by(id=post_id).one()
        # 이런 걸 모아서 util을 만들어도...
        return render_template('post-read.html', post=post)
        # render post page
    except NoResultFound:
        abort(404)
    except MultipleResultsFound:
        abort(404)
예제 #4
0
def update_post(post_id):
    try:
        post = dao.query(Post).filter_by(id=post_id).one()
        title = request.form.get('title', None)
        content = request.form.get('content', None)
        post.title = title
        post.content = content
        dao.commit()
        return redirect(url_for('read_post', post_id=post_id))
    except NoResultFound:
        abort(404)
    except MultipleResultsFound:
        abort(404)
    except:
        return redirect(url_for('index'))
예제 #5
0
def create_comment(post_id):
    try:
        post = dao.query(Post).filter_by(id=post_id).one()
        name = request.form.get('name', None)
        created_at = datetime.now()
        content = request.form.get('content', None)
        ip = request.remote_addr
        new_comment = Comment(name, created_at, content, post_id, ip=ip)
        dao.add(new_comment)
        dao.commit()
        return redirect(request.referrer
                        or url_for('read_post', post_id=post_id))
    except NoResultFound:
        abort(404)
    except MultipleResultsFound:
        abort(404)
    except:
        pass
예제 #6
0
def index_post():
    page = int(request.args.get('page', 0))
    page_length = 10
    posts = dao.query(Post).all()[(page * page_length):((page + 1) *
                                                        page_length)]
    return render_template('post-index.html', posts=posts)
예제 #7
0
 def recent_comments():
     comments = dao.query(Comment).options(
         joinedload(Comment.post, innerjoin=True)).order_by(
             Comment.created_at.desc()).all()[:5]
     return comments
예제 #8
0
 def recent_posts():
     posts = dao.query(Post).order_by(Post.created_at.desc()).all()[:5]
     return posts
예제 #9
0
def index():
    posts = dao.query(Post).options(joinedload(Post.comments)).order_by(
        Post.created_at.desc()).all()[:3]
    # cause N+1 query
    # http://docs.sqlalchemy.org/en/latest/orm/loading_relationships.html 이걸 참고해서 고쳐봅시다
    return render_template('index.html', posts=posts)