示例#1
0
def show_topic(topic_id):
    topic = Topic.query.get_or_404(topic_id)
    topic.read_time += 1
    db.session.commit()
    if topic.saved and current_user != topic.author and not current_user.can(
            'MODERATE'):
        abort(404)
    if not current_user.is_authenticated and topic.group.status_id != 4:
        flash('请登录', 'info')
        return redirect(url_for('auth.login'))

    if current_user.is_authenticated and not current_user.confirmed and topic.group.status_id != 4:
        flash('请等待管理员确认您的账户', 'warning')
        return redirect(url_for('main.index'))

    if not current_user.can('MEMBER') and topic.group.status_id == 1:
        abort(403)

    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['POST_PER_PAGE']
    pagination = Post.query.with_parent(topic).filter_by(saved=False).\
        order_by(Post.timestamp.asc()).paginate(page, per_page)
    posts = pagination.items
    if current_user.is_authenticated:
        current_user.read(topic)
    form = MigrateForm()
    return render_template('main/show_topic.html',
                           topic=topic,
                           posts=posts,
                           pagination=pagination,
                           form=form)
示例#2
0
def new_topic(group_id):
    if group_id == 6:
        abort(403)
    group = Group.query.get_or_404(group_id)
    if group.status_id == 1 and not current_user.can('MEMBER'):
        abort(403)

    if group.status_id == 2 and not current_user.can('MEMBER'):
        abort(403)

    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        topic = Topic(name=title,
                      body=body,
                      group_id=group_id,
                      author=current_user._get_current_object())
        db.session.add(topic)
        if form.publish.data:
            current_user.read(topic)  # 标记自己发表的文章为已读
            db.session.commit()
            if form.notice.data:
                current_user.notice(topic)
            flash('主题已发表', 'success')
            return redirect(url_for('main.show_topic', topic_id=topic.id))
        elif form.save.data:
            topic.saved = True
            db.session.commit()
            flash('主题已保存', 'success')
            return redirect(url_for('user.draft_topic'))
        elif form.save1.data:
            topic.saved = True
            db.session.commit()
            flash('请上传附件', 'info')
            return redirect(url_for('.upload_topic', topic_id=topic.id))
    return render_template('main/new_topic.html', form=form, group=group)
示例#3
0
def edit_topic(topic_id):
    topic = Topic.query.get_or_404(topic_id)
    if current_user != topic.author and current_user != topic.group.admin and not current_user.can(
            'MODERATE'):
        abort(403)  #除了主题的author、主题所在组的管理员或者协管员,都不能编辑主题
    form = PostForm()
    if form.validate_on_submit():
        topic.name = form.title.data
        topic.body = form.body.data
        if form.publish.data:
            current_user.read(topic)  # 标记自己发表的文章为已读
            topic.saved = False
            db.session.commit()
            if form.notice.data:
                current_user.notice(topic)
            flash('主题已发表', 'success')
            return redirect(url_for('main.show_topic', topic_id=topic.id))
        elif form.save.data:
            if topic.posts:
                flash('已有回帖,不允许保存。', 'warning')
                return redirect_back()
            else:
                topic.saved = True
                topic.top = False
                db.session.commit()
                flash('主题已保存', 'success')
                return redirect(url_for('user.draft_topic'))
        elif form.save1.data:
            topic.saved = True
            topic.top = False
            db.session.commit()
            flash('请上传附件', 'info')
            return redirect(url_for('.upload_topic', topic_id=topic.id))
    form.title.data = topic.name
    form.body.data = topic.body
    return render_template('main/edit_topic.html', form=form, topic=topic)