Exemplo n.º 1
0
def edit_favorite(username, id):
    """
    编辑收藏夹
    :param username:
    :param id:
    :return:
    """
    s = search()
    if s:
        return s
    user = User.query.filter_by(username=username).first()
    favorite = user.favorites.filter_by(id=id).first()
    if current_user != user and not current_user.can(Permission.ADMINISTER):
        flash('没有权限')
        return redirect(url_for('.user', username=user.username))
    form = EditFavoriteForm()
    if form.validate_on_submit():
        favorite.title = form.title.data
        favorite.description = form.description.data
        db.session.add(favorite)
        return redirect(
            url_for('.favorite', username=user.username, id=favorite.id))
    form.title.data = favorite.title
    form.description.data = favorite.description
    context = dict(form=form, favorite=favorite)
    return render_template('favorite/edit_favorite.html', **context)
Exemplo n.º 2
0
def posts():
    s = search()
    if s:
        return s
    page = request.args.get('page', 1, type=int)
    if request.cookies.get('post_order_by') == 'timestamp':
        pagination = Post.query.order_by(Post.timestamp.desc()).paginate(
            page,
            per_page=current_app.config['ZHIDAO_POST_PER_PAGE'],
            error_out=False)
    elif request.cookies.get('post_order_by') == 'likecount':

        pagination = Post.query.order_by(Post.liked_count.desc()).paginate(
            page,
            per_page=current_app.config['ZHIDAO_POST_PER_PAGE'],
            error_out=False)
    else:
        pagination = Post.query.order_by(Post.timestamp.desc()).paginate(
            page,
            per_page=current_app.config['ZHIDAO_POST_PER_PAGE'],
            error_out=False)
    posts = pagination.items
    tags = Tag.query.all()
    context = dict(posts=posts, pagination=pagination, tags=tags)
    return render_template('post/posts.html', **context)
Exemplo n.º 3
0
def post(id):
    """文章详情页"""
    s = search()
    if s:
        return s
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if post.author != current_user:
        post.browsed()
    if form.validate_on_submit():
        comment = Comment.create(author=current_user._get_current_object(),
                                 post=post,
                                 body=form.body.data,
                                 topic_type='post')
        db.session.add(comment)
        db.session.commit()
        flash('评论添加成功', 'success')
        return redirect(url_for('.post', id=post.id))
    page = request.args.get('page', 1, type=int)
    pagination = post.comments.filter_by(topic_type='post'). \
        order_by(Comment.timestamp.desc()).paginate(page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'],
                                                    error_out=False)
    comments = pagination.items
    if post.disable_comment:
        return render_template('post/post.html', post=post)
    context = dict(form=form,
                   pagination=pagination,
                   comments=comments,
                   post=post)
    return render_template('post/post.html', **context)
Exemplo n.º 4
0
def favorite_comments(username, id):
    s = search()
    if s:
        return s
    user = User.query.filter_by(username=username).first()
    favorite = Favorite.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment.create(author=current_user._get_current_object(),
                                 body=form.body.data,
                                 favorite=favorite,
                                 topic_type='favorite')
        db.session.add(comment)
        db.session.commit()
        flash('评论添加成功')
        return redirect(
            url_for('.favorite_comments',
                    username=user.username,
                    id=favorite.id))
    page = request.args.get('page', 1, type=int)
    pagination = favorite.comments.order_by(Comment.timestamp.desc()).paginate(
        page,
        per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'],
        error_out=False)
    comments = pagination.items
    context = dict(form=form,
                   pagination=pagination,
                   comments=comments,
                   user=user,
                   favorite=favorite)
    return render_template('favorite/favorite_comments.html', **context)
Exemplo n.º 5
0
def index():
    page = request.args.get('page', 1, type=int)
    s = search()
    if s:
        return s
    pagination = Question.query.order_by(Question.timestamp.desc()).paginate(
        page,
        per_page=current_app.config['ZHIDAO_QUESTION_PER_PAGE'],
        error_out=False)
    questions = pagination.items
    context = dict(questions=questions, pagination=pagination)
    return render_template('index/index.html', **context)
Exemplo n.º 6
0
def tag_posts(id):
    s = search()
    if s:
        return s
    tag = Tag.query.get_or_404(id)
    page = request.args.get('page', 1, type=int)
    pagination = tag.posts.paginate(
        page,
        per_page=current_app.config['ZHIDAO_POST_PER_PAGE'],
        error_out=False)  # 分页返回PostTag对象
    posts = [item.post for item in pagination.items]  # 提取post对象
    tags = Tag.query.all()
    context = dict(posts=posts, tags=tags, pagination=pagination)
    return render_template('post/posts.html', **context)
Exemplo n.º 7
0
def question(id):
    """
    问题详情页
    :param id:
    :return:
    """

    s = search()
    if s:
        return s
    page = request.args.get('page', 1, type=int)
    question = Question.query.get_or_404(id)
    pagination = question.answers.order_by(Answer.timestamp.desc()). \
        paginate(page, per_page=current_app.config['ZHIDAO_ANSWER_PER_PAGE'], error_out=False)
    answers = pagination.items
    topics = [i.topic for i in question.topics.all()]
    if current_user != question.author:
        question_browsed.send(question)  # 发送信号更新
    recent_q = request.cookies.get('recent_q', '')
    ques_id = str(question.id)
    if recent_q == '':
        # 第一次点击
        recent_q = deque(maxlen=5)
        if ques_id in recent_q:
            recent_q.remove(ques_id)  # 先删除
            recent_q.appendleft(ques_id)  # 再在前面添加
        else:
            recent_q.appendleft(ques_id)
        recent_q = ','.join(recent_q)  # 拼接字符串 类似'4,8,6,9'
        a_context = dict(question=question, answers=answers, pagination=pagination, topics=topics)
        resp = make_response(render_template('question/question.html', **a_context))
        resp.set_cookie('recent_q', recent_q)
        return resp
    else:
        arr = recent_q.split(',')
        recent_q = deque(arr, maxlen=5)  # 生成deque
        if ques_id in recent_q:
            recent_q.remove(ques_id)  # 先删除
            recent_q.appendleft(ques_id)  # 再在前面添加
        else:
            recent_q.appendleft(ques_id)
        recent_q_count=len(recent_q)
        session['recent_q_count']=recent_q_count
        recent_q = ','.join(recent_q)  # 拼接字符串 类似'4,8,6,9'
        context = dict(question=question, answers=answers, pagination=pagination, topics=topics)
        resp = make_response(
            render_template('question/question.html', **context))
        resp.set_cookie('recent_q', recent_q)
        return resp
Exemplo n.º 8
0
def edit_answer(id):
    s = search()
    if s:
        return s
    form = EditAnswerForm()
    answer = Answer.query.get_or_404(id)
    if current_user != answer.author and not current_user.can(
            Permission.ADMINISTER):
        abort(403)  # 只有自己和管理员可以编辑文章,防止别人直接通过路由编辑文章
    if form.validate_on_submit():
        answer.body = form.body.data
        db.session.add(answer)
        flash('答案修改成功', category='success')
        return redirect(url_for('.question', id=answer.question_id))
    form.body.data = answer.body
    return render_template('answer/edit_answer.html', form=form, answer=answer)
Exemplo n.º 9
0
def edit_post(id):
    s = search()
    if s:
        return s
    form = EditArticleForm()
    post = Post.query.get_or_404(id)
    if current_user != post.author and not current_user.can(
            Permission.ADMINISTER):
        abort(403)
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        flash('文章修改成功')
        return redirect(url_for('.post', id=post.id))
    form.body.data = post.body
    return render_template('post/edit_post.html', form=form, post=post)
Exemplo n.º 10
0
def topic_followers(id):
    s = search()
    if s:
        return s
    topic = Topic.query.get_or_404(id)
    page = request.args.get('page', 1, type=int)

    pagination = topic.followers.order_by(
        FollowTopic.timestamp.desc()).paginate(
            page,
            per_page=current_app.config['ZHIDAO_FOLLOW_PER_PAGE'],
            error_out=False)
    follows = [i.follower for i in pagination.items]
    session['topic_id'] = topic.id
    context = dict(pagination=pagination, follows=follows, topic=topic)
    return render_template('topic/topic_followers.html', **context)
Exemplo n.º 11
0
def upload_avatar():
    s = search()
    if s:
        return s
    form = UploadForm()
    if form.validate_on_submit():
        photoname = photos.save(form.photo.data)

        photo_url = photos.url(photoname)  # 原图
        avatar_url_sm = image_resize(photoname, 30)
        avatar_url_nm = image_resize(photoname, 400)
        current_user.avatar_url_sm = avatar_url_sm  # 缩略头像
        current_user.avatar_url_nm = avatar_url_nm  # 正常头像
        db.session.add(current_user)
    else:
        photo_url = None
    return render_template('index/upload.html', form=form, file_url=photo_url)
Exemplo n.º 12
0
def question_followers(id):
    s = search()
    if s:
        return s
    question = Question.query.get_or_404(id)
    if question is None:
        flash('问题不存在')
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = question.followers.order_by(FollowQuestion.timestamp.desc()). \
        paginate(page, per_page=current_app.config['ZHIDAO_FOLLOW_PER_PAGE'], error_out=False)
    follows = [item.follower for item in pagination.items]  # 关注问题的人
    title = '关注问题{}的人'.format(question.title)
    style = 'question_followers'
    context = dict(pagination=pagination, follows=follows,
                   question=question, title=title, style=style, user=question.author)
    return render_template('user/user_follows.html', **context)
Exemplo n.º 13
0
def edit_question(id):
    s = search()
    if s:
        return s
    form = EditQuestionForm()
    question = Question.query.get_or_404(id)
    if current_user != question.author and not current_user.can(Permission.ADMINISTER):
        abort(403)
    if form.validate_on_submit():
        question.description = form.description.data  # 修改问题描述
        question.anonymous = form.anonymous.data  # 选择取消匿名
        db.session.add(question)
        return redirect(url_for('.question', id=question.id))
    form.description.data = question.description
    form.anonymous.data = question.anonymous
    context = dict(form=form, question=question)
    return render_template('question/edit_question.html', **context)
Exemplo n.º 14
0
def create_post():
    s = search()
    if s:
        return s
    form = WriteArticleForm()
    if form.validate_on_submit():
        post = Post.create(title=form.title.data,
                           body=form.body.data,
                           author=current_user._get_current_object(),
                           disable_comment=form.disable_comment.data)
        for each in form.tags.data:
            tag = Tag.query.get_or_404(each)
            if tag.add_post(post):
                db.session.add(tag)
                flash('标签{}添加成功'.format(tag.title), category='success')
        flash('文章添加成功', category='success')
        return redirect(url_for('.posts'))
    return render_template('post/create_post.html', form=form)
Exemplo n.º 15
0
def topics():
    s = search()
    if s:
        return s
    items = Topic.query
    l_items = [
        item for (index, item) in enumerate(items.all(), 0) if index % 2 == 0
    ]
    r_items = [
        item for (index, item) in enumerate(items.all(), 0) if index % 2 != 0
    ]
    hot_topics = items.order_by(Topic.follower_count.desc()).limit(
        3)  # 关注最多的三个话题
    context = dict(l_items=l_items,
                   r_items=r_items,
                   topics=topics,
                   hot_topics=hot_topics)
    return render_template('topic/topics.html', **context)
Exemplo n.º 16
0
def create_favorite(username):
    s = search()
    if s:
        return s
    user = User.query.filter_by(username=username).first()
    if current_user != user and not current_user.can(Permission.ADMINISTER):
        flash('没有权限')
        return redirect(url_for('.user', username=user.username))
    form = FavoriteForm()
    if form.validate_on_submit():
        Favorite.create(title=form.title.data,
                        description=form.description.data,
                        public=form.public.data,
                        user=current_user._get_current_object())

        flash('新建了{}收藏夹'.format(form.title.data), category='info')
        return redirect(url_for('.user', username=user.username))
    return render_template('favorite/create_favorite.html', form=form)  # 渲染表单
Exemplo n.º 17
0
def topic_detail(id):
    s = search()
    if s:
        return s
    topic = Topic.query.get_or_404(id)
    questions = [i.question for i in topic.questions.all()]
    questions_ids_in_this_topic = [i.id for i in questions]
    excellent_authors = []  # 该话题下的优秀回答者
    for each in User.query.all():
        answer_count = each.answers.filter(
            Answer.question_id.in_(questions_ids_in_this_topic)).count()
        if answer_count != 0:
            excellent_authors.append([each, answer_count])
    excellent_authors.sort(key=itemgetter(1), reverse=True)

    context = dict(questions=questions,
                   topic=topic,
                   excellent_authors=excellent_authors[:3])

    return render_template('topic/topic.html', **context)
Exemplo n.º 18
0
def question_comments(id):
    s = search()
    if s:
        return s
    form = CommentForm()
    question = Question.query.get_or_404(id)
    page = request.args.get('page', 1, type=int)
    if form.validate_on_submit():
        Comment.create(author=current_user._get_current_object(), question=question,
                       body=form.body.data, topic_type='question')
        question_comment_add.send(question)
        flash('评论添加成功', category='success')
        return redirect(url_for('.question_comments', id=question.id))

    pagination = question.comments.filter_by(topic_type='question').order_by(Comment.timestamp.desc()).paginate(
        page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'], error_out=False
    )
    comments = pagination.items
    context = dict(pagination=pagination, comments=comments, question=question, form=form)
    return render_template('question/question_comments.html', **context)
Exemplo n.º 19
0
def collect_question(id):
    s = search()
    if s:
        return s
    question = Question.query.get_or_404(id)
    if question.author == current_user:
        flash('不能收藏自己提出的问题')
        return redirect(url_for('.index'))
    form = CollectForm()
    if form.validate_on_submit():
        favorite = current_user.favorites.filter_by(
            id=form.favorite.data).first()  # 当前用户的文件夹
        favorite.collect_question(question)  # 收藏问题
        favorite_question_add.send(favorite)
        flash('收藏了问题{}'.format(question.title), category='info')
        return redirect(
            url_for('.favorite',
                    username=current_user.username,
                    id=favorite.id))
    return render_template('favorite/collect.html', form=form)
Exemplo n.º 20
0
def collect_answer(id):
    s = search()
    if s:
        return s
    answer = Answer.query.get_or_404(id)
    if answer.author == current_user:
        flash('不能收藏自己的问题')
        return redirect(url_for('.index'))
    form = CollectForm()
    if form.validate_on_submit():
        favorite = current_user.favorites.filter_by(
            id=form.favorite.data).first()
        favorite.collect_answer(answer)
        favorite_answer_add.send(favorite)
        flash('收藏了{}的答案'.format(answer.author.username), category='info')
        return redirect(
            url_for('.favorite',
                    username=current_user.username,
                    id=favorite.id))
    return render_template('favorite/collect.html', form=form)
Exemplo n.º 21
0
def write_answer(id):
    s = search()
    if s:
        return s
    question = Question.query.get_or_404(id)
    if question.answers.filter_by(
            author=current_user._get_current_object()).first():
        flash('你已经回答了该问题', category='warning')
        return redirect(url_for('.question', id=question.id))
    form = WriteAnswerForm()
    if form.validate_on_submit():
        Answer.create(body=form.body.data,
                      question=question,
                      author=current_user._get_current_object(),
                      anonymous=form.anonymous.data)
        question_answer_add.send(question)
        flash('添加回答成功', category='success')
        return redirect(url_for('.question', id=question.id))
    return render_template('answer/write_answer.html',
                           form=form,
                           question=question)
Exemplo n.º 22
0
def collect_post(id):
    s = search()
    if s:
        return s
    post = Post.query.get_or_404(id)
    if post.author == current_user:
        flash('不能收藏自己的文章')
        return redirect(url_for('.index'))
    form = CollectForm()
    if form.validate_on_submit():
        favorite = current_user.favorites.filter_by(
            id=form.favorite.data).first()
        favorite.collect_post(post)
        favorite_post_add.send(favorite)
        flash('收藏了{}的文章{}'.format(post.author.username, post.title),
              category='info')
        return redirect(
            url_for('.favorite',
                    username=current_user.username,
                    id=favorite.id))
    return render_template('favorite/collect.html', form=form)
Exemplo n.º 23
0
def pose_question():
    """
    提出问题
    :return:
    """
    s = search()
    if s:
        return s
    form = QuestionForm()
    if form.validate_on_submit():

        question = Question.create(author=current_user._get_current_object(), title=form.title.data,
                                   description=form.description.data, anonymous=form.anonymous.data)

        for each in form.topic.data:
            topic = Topic.query.get(each)
            if topic.add_question(question):
                flash('添加到话题:{}'.format(topic.title), 'success')

        flash('提问成功')
        return redirect(url_for('.index'))
    return render_template('question/pose_question.html', form=form)
Exemplo n.º 24
0
def answer_comments(id):
    s = search()
    if s:
        return s
    answer = Answer.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        Comment.create(author=current_user._get_current_object(),
                       answer=answer,
                       topic_type='answer',
                       body=form.body.data)
        answer_comment_add.send(answer)
        flash('评论添加成功', 'success')
        return redirect(url_for('.answer_comments', id=answer.id))
    page = request.args.get('page', 1, type=int)
    pagination = answer.comments.filter_by(topic_type='answer').order_by(Comment.timestamp.desc()). \
        paginate(page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'], error_out=False)
    comments = pagination.items
    context = dict(form=form,
                   pagination=pagination,
                   comments=comments,
                   answer=answer)
    return render_template('answer/answer_comments.html', **context)