Esempio n. 1
0
def question_edit(id):
    question = Question.query.filter_by(id=id).first()
    form = AddQuestionForm()
    if form.validate_on_submit():
        question.title = form.title.data
        question.content = form.content.data
        tags = form.tags.data
        topics = tags.split(',')
        if len(topics) > 4:
            flash('最多只能关注5个话题。')
            topics = topics[:5]
        db.session.commit()
        for topic in topics:
            tag = Tag.query.filter_by(title=topic).first()
            question.question_follow_tag(tag)
        flash('成功添加问题。')
        return redirect(url_for('main.question', id=question.id))
    form.title.data = question.title
    form.content.data = question.content
    topics = ''
    for question in question.tags.all():
        if not topics:
            topics= topics +question.q_tag.title
        else:
            topics= topics + ',' +question.q_tag.title
    form.tags.data = topics
    return render_template('question_edit.html', form=form)
Esempio n. 2
0
def question_add():
    form = AddQuestionForm()
    if form.validate_on_submit():
        question = Question(title=form.title.data, content=form.content.data)
        if Question.query.filter_by(title=form.title.data).first():
            flash('该问题已创建。')
        else:
            tags = form.tags.data
            topics = tags.split(',')
            if len(topics) > 4:
                flash('最多只能关注5个话题。')
                topics = topics[:5]
            author = User.query.filter_by(id=current_user.id).first()
            question.question_author = author
            db.session.commit()
            for topic in topics:
                if not Tag.query.filter_by(title=topic).first():
                    topic = Tag(title=topic, desc='话题描述')
                    db.session.add(topic)
                    db.session.commit()
                    question.question_follow_tag(topic)
                else:
                    tag = Tag.query.filter_by(title=topic).first()
                    question.question_follow_tag(tag)
            flash('成功添加问题。')
            return redirect(url_for('main.question', id=question.id))
    return render_template('question_add.html', form=form)