Example #1
0
def about_the_tag(id):
    if current_user.is_anony():
        return render_template('403.html')
    pt = PostTag.query.filter(PostTag.tagid == id).all()
    ques_ = []
    for p in pt:
        ques_.append(p.postid)
    ques = Items.query.filter(Items.id.in_(ques_), Items.deleted == 0).all()
    tag = Items.query.filter_by(id=id).first()
    return render_template('topic.html', ques=ques, tag=tag)
Example #2
0
def follow_question_or_tag(id):
    if current_user.is_anony():
        return render_template('403.html')
    p = Items.query.filter_by(id=id).first()
    f = FocusTagQues(personid=current_user.id, focusedid=id)
    db.session.add(f)
    db.session.commit()
    if p.types == 0:
        return redirect(url_for('question', id=id))
    return redirect(url_for('about_the_tag', id=id))
Example #3
0
def edit_tags(id):
    if current_user.is_anony():
        return render_template('403.html')
    ques = Items.query.get(id)
    pt = PostTag.query.filter(PostTag.postid == id).all()
    ptnum = PostTag.query.filter(PostTag.postid == id).count()
    taglist = []
    for p in pt:
        taglist.append(p.items_.title_or_ans)
    while (len(taglist) < 3):
        taglist.append('')
    tagval1 = taglist[0]
    tagval2 = taglist[1]
    tagval3 = taglist[2]
    if request.method == 'POST':
        thesetags = []
        tagval1 = request.form.get('tag1', '???')
        tagval2 = request.form.get('tag2', '???')
        tagval3 = request.form.get('tag3', '???')
        thesetags.append(tagval1)
        thesetags.append(tagval2)
        thesetags.append(tagval3)
        PostTag.query.filter(PostTag.postid == id).delete()
        db.session.commit()
        for t in thesetags:
            if t != '???' and t.strip() != '':
                existing_tag = Items.query.filter(Items.types == 3).filter(
                    Items.title_or_ans == t).first()
                if existing_tag is None:
                    p = Items(title_or_ans=t, types=3)
                    db.session.add(p)
                    db.session.add(p)
                    new_tag = Items.query.filter(Items.types == 3).filter(
                        Items.title_or_ans == t).first()
                    pt = PostTag(postid=id, tagid=new_tag.id)
                    db.session.add(pt)
                    db.session.commit()
                else:
                    old_tag = Items.query.filter(Items.types == 3).filter(
                        Items.title_or_ans == t).first()
                    pt = PostTag(postid=id, tagid=old_tag.id)
                    db.session.add(pt)
                    db.session.commit()
        return redirect(url_for('question', id=ques.id))
    return render_template('edit_tags.html', tagval1 = tagval1, tagval2 = tagval2, tagval3 = tagval3, ques = ques,\
      Permission = Permission)
Example #4
0
def upvote(quesid, id):
    if current_user.is_anony():
        return render_template('403.html')
    ans = Items.query.get(id)
    if ans.author_id == current_user.id:
        return render_template('not.html', text=u'您不能对自己的回答点赞')
    u = Upvote.query.filter(Upvote.upvoter == current_user.id).filter(
        Upvote.answerid == id).first()
    if u is None:
        ans = Items.query.filter_by(id=id).first().answer
        it = Items.query.filter_by(id=id).first()
        ans.upvote = ans.upvote + 1
        us = User.query.filter_by(id=it.author_id).first()
        us.ups = us.ups + 1
        up = Upvote(upvoter=current_user.id, answerid=id, descr=2)
        db.session.add(ans)
        db.session.add(us)
        db.session.add(up)
        db.session.commit()
        return redirect(url_for('question', id=quesid))
    else:
        ans = Items.query.filter_by(id=id).first().answer
        it = Items.query.filter_by(id=id).first()
        if u.descr == 2:
            ans.upvote = ans.upvote - 1
            us = User.query.get(it.author_id)
            us.ups = us.ups - 1
            up = Upvote.query.filter(Upvote.upvoter == current_user.id).filter(
                Upvote.answerid == id).filter(Upvote.descr == 2).first()
            db.session.delete(up)
        elif u.descr == 0:
            ans.upvote = ans.upvote + 1
            ans.downvote = ans.downvote - 1
            us = User.query.filter_by(id=it.author_id).first()
            us.ups = us.ups + 1
            up = Upvote.query.filter(Upvote.upvoter == current_user.id).filter(
                Upvote.answerid == id).filter(Upvote.descr == 0).first()
            up.descr = 2
            db.session.add(up)
        db.session.add(ans)
        db.session.add(us)
        db.session.commit()
        return redirect(url_for('question', id=quesid))
Example #5
0
def comment_on_article(id):
    if current_user.is_anony():
        return render_template('403.html')
    art = Items.query.get(id)
    page = request.args.get('page', 1, type=int)
    pagination = Comment.query.filter(
        Comment.itemid == id, Comment.deleted == 0).order_by(
            Comment.timestamp.desc()).paginate(page, 3, error_out=False)
    c = pagination.items
    cform = CommentForm()
    if cform.validate_on_submit():
        co = Comment(content=cform.body.data,
                     userid=current_user.id,
                     itemid=id)
        db.session.add(co)
        db.session.commit()
        return redirect(url_for('comment_on_article', id=id))
    return render_template('comment_on_article.html', art = art, Permission = Permission, comments = c, \
     cform = cform, pagination = pagination )
Example #6
0
def raise_question():
    if current_user.is_anony():
        return render_template('403.html')
    form = QuestionForm()
    if request.method == 'POST':
        title = request.form.get('title')
        body = request.form.get('body')
        ques = Question(description=body)
        db.session.add(ques)
        db.session.commit()
        q = Question.query.filter_by(description=body).first()
        it = Items(title_or_ans=title,
                   author_id=current_user.id,
                   types=0,
                   ques_id=q.id)
        db.session.add(it)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('raise_question.html', form=form)
Example #7
0
def write_articles():
    if current_user.is_anony():
        return render_template('403.html')
    form = PostForm()
    if request.method == 'POST':
        title = request.form.get('title')
        body = request.form.get('body')
        ar = Article(content=body)
        db.session.add(ar)
        db.session.commit()
        ar = Article.query.filter_by(content=body).first()
        it = Items(title_or_ans=title,
                   author_id=current_user.id,
                   types=1,
                   art_id=ar.id)
        db.session.add(it)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('write_articles.html', form=form)
Example #8
0
def focus():
    if current_user.is_anony():
        return render_template('403.html')
    page = request.args.get('page', 1, type=int)
    follow = current_user.followed.all()
    focusid = []
    for i in follow:
        focusid.append(i.followed_id)
    all_p = Items.query.filter(
        Items.author_id.in_(focusid)).filter(Items.types <= 2).all()
    all_p_id = []
    for a in all_p:
        all_p_id.append(a.id)

    ftq = FocusTagQues.query.filter(FocusTagQues.personid.in_(focusid)).all()
    upt = Upvote.query.filter(Upvote.upvoter.in_(focusid)).all()
    dic = {}
    dicvote = {}
    for f in ftq:
        if f.focusedid not in all_p_id:
            value = dic.get(f.focusedid, 'empty')
            if value == 'empty':
                tmp = []
                tmp.append(f.personid)
                dic[f.focusedid] = tmp

            else:
                tmp = dic[f.focusedid]
                tmp.append(f.personid)
                dic[f.focusedid] = tmp
    for u in upt:
        if u.answerid not in all_p_id:
            value = dicvote.get(u.answerid, 'empty')
            if value == 'empty':
                tmp = []
                tmp.append(u.upvoter)
                dicvote[u.answerid] = tmp
            else:
                tmp = dicvote[u.answerid]
                tmp.append(u.upvoter)
                dicvote[u.answerid] = tmp

    all = []
    for a in all_p:
        all.append(a.id)
    for a in ftq:
        all.append(a.focusedid)
    for a in upt:
        all.append(a.answerid)
    all = list(set(all))
    all.sort()
    pagination = Items.query.filter(Items.id.in_(all), Items.deleted ==0).paginate(\
             page, per_page=10, error_out=False)
    posts = pagination.items
    posts.sort(key=lambda x: x.timestamp, reverse=True)
    u = User.query.filter(User.id.in_(focusid)).all()
    ques = Items.query.filter(Items.types == 0, Items.deleted == 0).all()
    return render_template('focus.html',
                           posts=posts,
                           pagination=pagination,
                           u=u,
                           dic=dic,
                           dicvote=dicvote,
                           ques=ques)
Example #9
0
def question(id):
    if current_user.is_anony():
        return redirect(url_for('question_for_anonymous', id=id))
    ques = Items.query.filter(Items.id == id, Items.deleted == 0).first()
    ansform = AnswerForm()
    if ansform.validate_on_submit():
        allans = Answer.query.filter_by(belongtoques=id).all()
        for a in allans:
            p = Items.query.filter_by(ans_id=a.id).first()
            if p.author_id == current_user.id:
                return render_template('not.html', text=u'您不能回答同一个问题两次')
        ans = Answer(belongtoques=id)
        db.session.add(ans)
        db.session.commit()
        a = Answer.query.order_by(
            Answer.id.desc()).filter_by(belongtoques=id).first()
        e = Items(title_or_ans=ansform.answer.data,
                  author_id=current_user.id,
                  types=2,
                  ans_id=a.id)
        db.session.add(e)
        db.session.add(a)
        db.session.commit()
    ansnum = Answer.query.filter(Answer.belongtoques == id).count()
    allans = Answer.query.filter(Answer.belongtoques == id).all()
    for a in allans:
        if Items.query.filter_by(ans_id=a.id).first().deleted == 1:
            ansnum = ansnum - 1
    ans_ = Answer.query.filter(Answer.belongtoques == id).all()
    ans_id_ = []
    ansdo = []
    for a in ans_:
        ans_id_.append(a.id)
    ans = Items.query.filter(Items.types == 2, Items.deleted == 0).filter(
        Items.ans_id.in_(ans_id_)).all()
    ans_id = []
    for a in ans:
        ans_id.append(a.id)
    i_do_to_the_ans = Upvote.query.filter(
        Upvote.upvoter == current_user.id).filter(
            Upvote.answerid.in_(ans_id)).all()
    for i in i_do_to_the_ans:
        if Items.query.get(i.answerid).deleted == 0:
            ansdo.append((i.answerid, i.descr))
    pt = PostTag.query.filter_by(postid=id).all()
    the_ques_tags_id = []
    related = []
    for p in pt:
        the_ques_tags_id.append(p.tagid)
    the_ques_tags = Items.query.filter(Items.id.in_(the_ques_tags_id)).all()
    rposts_rcds = PostTag.query.filter(
        PostTag.tagid.in_(the_ques_tags_id)).all()
    for r in rposts_rcds:
        related.append(r.postid)
    related = list(set(related))
    if len(related) != 0:
        related.remove(id)
        rposts = Items.query.filter(Items.id.in_(related),
                                    Items.deleted == 0).all()
    else:
        rposts = []
    thank = []
    thank_ = Thank.query.filter(Thank.personid == current_user.id).filter(
        Thank.postid.in_(ans_id)).all()
    for t in thank_:
        thank.append(t.postid)
    return render_template('question.html', ques = ques, form = ansform, Permission = Permission, \
     ans = ans, ansdo = ansdo, tags= the_ques_tags, ansnum = ansnum, rposts = rposts, thank = thank)