예제 #1
0
def index():
    form = PostForm()
    page = request.args.get('page', 1, type=int)
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash("Your post is now live!")
        return redirect(url_for('core.index'))
    posts = current_user.feed().paginate(page, current_app.config["POSTS_PER_PAGE"], False)
    prev_url = url_for('core.index', page=posts.prev_num)\
        if posts.has_prev else None
    next_url = url_for('core.index', page=posts.next_num)\
        if posts.has_next else None
    return render_template('index.html', title="Feed", posts=posts.items, form=form,\
        prev_url=prev_url, next_url=next_url)
예제 #2
0
def home():
    page, per_page, offset = get_page_args(
        page_parameter='page',
        per_page_parameter='per_page'
    )
    form = CreateMicropostForm(request.form)

    if form.validate_on_submit():
        form_for_picture = CreateMicropostForm()
        if form_for_picture.picture.data:
            f = form_for_picture.picture.data
            filename = secure_filename(f.filename)
            micropost = Micropost(
                user_id=current_user.id,
                content=form.content.data,
                picture_name=filename
            )
            f.save(os.path.join(
                current_app.config['UPLOAD_FOLDER'],
                'images',
                micropost.picture_name
            ))
            db.session.add(micropost)
            db.session.commit()
            flash("Micropost created!", "success")

    micropost = Micropost(
        content='',
        user_id=current_user.id
    ) if current_user.is_authenticated else None
    if current_user.is_authenticated:
        feed_items = current_user.feed()
        pagination = Pagination(
            page=page,
            per_page=per_page,
            total=len(feed_items),
            search=False,
            record_name='microposts',
            css_framework='bootstrap4'
        )
        return render_template(
            "static_pages/home.html",
            micropost=micropost,
            form=form,
            feed_items=feed_items[offset: offset + per_page],
            pagination=pagination,
            user=current_user
        )
    else:
        pagination = Pagination(
            page=page,
            per_page=per_page,
            total=0,
            search=False,
            record_name='microposts',
            css_framework='bootstrap4'
        )
        return render_template(
            "static_pages/home.html",
            micropost=micropost,
            form=form,
            feed_items=[],
            pagination=pagination,
            user=current_user
        )