Exemplo n.º 1
0
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE) and form.validate_on_submit():
        post = Post(body=form.body.data,
                    author=current_user._get_current_object())
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.index'))
    # 分页显示
    page = request.args.get('page', 1, type=int)
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    if show_followed:
        query = current_user.followed_posts
    else:
        query = Post.query
    # paginate() 为了显示某页中的记录,只有第一个参数必须指定,per_page 默认20
    pagination = query.order_by(Post.timestamp.desc()).paginate(
        page,
        per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        error_out=False)
    posts = pagination.items
    return render_template('index.html',
                           form=form,
                           posts=posts,
                           show_followed=show_followed,
                           pagination=pagination)
def edit_post(id):
    post = Post.query.get_or_404(id)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.tag = form.tag.data
        post.category = form.category.data
        post.officialLink = form.officialLink.data
        post.sphere = form.sphere.data
        post.city = form.city.data
        post.state = form.state.data
        post.country = form.country.data
        post.description = form.description.data
        db.session.add(post)
        db.session.commit()
        flash(_('As alterações foram salvas'))
        return redirect(url_for('main.explore'))
    form.title.data = post.title
    form.tag.data = post.tag
    form.category.data = post.category
    form.officialLink.data = post.officialLink
    form.sphere.data = post.sphere
    form.city.data = post.city
    form.state.data = post.state
    form.country.data = post.country
    form.description.data = post.description
    return render_template('edit_post.html',
                           title=(_('Editar Fonte')),
                           form=form,
                           post=post)
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == 'UNKNOWN' or len(language) > 5:
            language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash('your post is now live')
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('main.index',
                       page=posts.next_num) if posts.has_next else None
    prev_url = url_for('main.index',
                       page=posts.prev_num) if posts.has_prev else None
    return render_template('index.html',
                           title='Explore',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Exemplo n.º 4
0
Arquivo: routes.py Projeto: bwyt/Blog
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('提交成功')
        return redirect(url_for('main.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(page, current_app.config['POSTS_PER_PAGE'], False)
    # 获取下一页的url,next_num:下一页的页码
    next_url = url_for('main.index', page=posts.next_num) if posts.has_next else None
    # 获取上一页的url,prev_num:上一页的页码
    prev_url = url_for('main.index', page=posts.prev_num) if posts.has_prev else None

    return render_template('index.html', form=form, posts=posts.items, next_url=next_url, prev_url=prev_url)
Exemplo n.º 5
0
def edit(id):
    '''
    编辑文章
    :param id:
    :return:
    '''
    post = Post.query.get_or_404(id)
    if current_user != post.author and not current_user.can(Permission.ADMIN):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        db.session.commit()
        flash('文章已经修改')
        return redirect(url_for('.post', id=post.id))
    form.body.data = post.body
    return render_template('edit_post.html', form=form)
def register_source():
    form = PostForm()
    if form.validate_on_submit():
        sources = Post(title=form.title.data,
                       tag=form.tag.data,
                       category=form.category.data,
                       city=form.city.data,
                       state=form.state.data,
                       country=form.country.data,
                       description=form.description.data,
                       sphere=form.sphere.data,
                       officialLink=form.officialLink.data,
                       author=current_user)
        db.session.add(sources)
        db.session.commit()
        flash(_('Você registrou uma nova Fonte de Dados Abertos'))
        return redirect(url_for('main.explore'))
    return render_template('register_source.html',
                           title=(_('Cadastrar Fonte')),
                           form=form)