コード例 #1
0
def vote():
    topic = g.topic
    data = request.get_json()
    up = data.get('up', True)

    try:
        vote = topic.vote(user=g.user, ip=request.remote_addr, up=up)
        if vote.id:
            # this is an updated vote
            if vote.changed:
                points = topic.update_points(up=up, points=2)
                topic.user.update_points(up=up, points=6)
            else:
                points = topic.points
        else:
            # this is a new vote
            points = topic.update_points(up)
            user_points = 1
            if up:
                user_points = 5
            topic.user.update_points(up=up, points=user_points)

        db_session.commit()
        data = {"points": points}

        cache.update_topic(topic.id, topic)
        cache.update_sorted_topics(topic, 'points')
        return jsonify({"data": data})
    except Exception, e:
        logging.error(e)
        db_session.rollback()
        return json_error_database()
コード例 #2
0
ファイル: api_topics.py プロジェクト: laoshanlung/flask-demo
def vote():
    topic = g.topic
    data = request.get_json()
    up = data.get('up', True)

    try:
        vote = topic.vote(user=g.user, ip=request.remote_addr, up=up)
        if vote.id:
            # this is an updated vote
            if vote.changed:
                points = topic.update_points(up=up, points=2)
                topic.user.update_points(up=up, points=6)
            else:
                points = topic.points
        else:
            # this is a new vote
            points = topic.update_points(up)
            user_points = 1
            if up:
                user_points = 5
            topic.user.update_points(up=up, points=user_points)
        
        db_session.commit()
        data = {
            "points": points
        }

        cache.update_topic(topic.id, topic)
        cache.update_sorted_topics(topic, 'points')
        return jsonify({"data": data})
    except Exception, e:
        logging.error(e)
        db_session.rollback()
        return json_error_database()
コード例 #3
0
def create():
    data = request.get_json()

    form = TopicForm(**data)
    if form.validate():
        form_data = form.data
        form_data['ip'] = request.remote_addr

        try:
            topic = g.user.create_topic(**form_data)
            alert = dict(
                type='success',
                messages=
                _("Your topic has been created successfully. You will be redirected to it shortly"
                  ))

            redirect = topic.get_url('view')
            cache.update_sorted_topics(topic, 'date_created')
            return jsonify({
                "data": topic.json_data(),
                "alert": alert,
                "redirect": redirect
            })
        except ModelException, me:
            db_session.rollback()
            return json_error(type=me.type, messages=me.message)
        except Exception, e:
            logging.error(e)
            db_session.rollback()
            return json_error_database()
コード例 #4
0
def comment_create():
    data = request.get_json()
    topic = g.topic

    form = CommentForm(**data)
    if form.validate():
        form_data = form.data
        form_data['user'] = g.user
        form_data['ip'] = request.remote_addr

        try:
            comment = topic.create_comment(**form_data)
            topic.update_comment_count()
            topic.update_user_comment_count(user_id=comment.user_id)

            db_session.commit()

            cache.update_topic(topic.id, topic)
            cache.update_sorted_topics(topic, 'comment_count')
            return jsonify({"data": comment.json_data()})
        except ModelException, me:
            db_session.rollback()
            return json_error(type=me.type, messages=me.message)
        except Exception, e:
            logging.error(e)
            db_session.rollback()
            return json_error_database()
コード例 #5
0
ファイル: api_topics.py プロジェクト: laoshanlung/flask-demo
def create():
    data = request.get_json()
    
    form = TopicForm(**data)
    if form.validate():
        form_data = form.data
        form_data['ip'] = request.remote_addr

        try:
            topic = g.user.create_topic(**form_data)
            alert = dict(
                type='success',
                messages=_("Your topic has been created successfully. You will be redirected to it shortly")
            )

            redirect = topic.get_url('view')
            cache.update_sorted_topics(topic, 'date_created')
            return jsonify({"data": topic.json_data(), "alert": alert, "redirect": redirect})
        except ModelException, me:
            db_session.rollback()
            return json_error(type=me.type, messages=me.message)
        except Exception, e:
            logging.error(e)
            db_session.rollback()
            return json_error_database()
コード例 #6
0
ファイル: api_topics.py プロジェクト: laoshanlung/flask-demo
def comment_create():
    data = request.get_json()
    topic = g.topic
    
    form = CommentForm(**data)
    if form.validate():
        form_data = form.data
        form_data['user'] = g.user
        form_data['ip'] = request.remote_addr

        try:
            comment = topic.create_comment(**form_data)
            topic.update_comment_count()
            topic.update_user_comment_count(user_id=comment.user_id)

            db_session.commit()

            cache.update_topic(topic.id, topic)
            cache.update_sorted_topics(topic, 'comment_count')
            return jsonify({"data": comment.json_data()})
        except ModelException, me:
            db_session.rollback()
            return json_error(type=me.type, messages=me.message)
        except Exception, e:
            logging.error(e)
            db_session.rollback()
            return json_error_database()
コード例 #7
0
ファイル: topics.py プロジェクト: laoshanlung/flask-demo
def view(slug, id):
    topic = g.topic
    user = g.user

    if topic.update_view_count(ip=request.remote_addr, user=user, commit=True):
        cache.update_topic(topic.id, topic)
        cache.update_sorted_topics(topic, 'view_count')

    topic_data = topic.json_data()
    topic_data['comments'] = topic.get_comments(json=True)
    topic_data['user_vote'] = Topic.check_vote(user, topic.id)
    topic_data['user_comment'] = Topic.check_comment(user, topic.id)
    context = {
        'js_module': 'view_topic',
        'style': 'view_topic',
        'topic': topic_data,
        'sub_title': topic.title
    }
    return render_template('topics/view.html', **context)
コード例 #8
0
ファイル: topics.py プロジェクト: tanqhnguyen/flask-demo
def view(slug, id):
    topic = g.topic
    user = g.user

    if topic.update_view_count(ip=request.remote_addr, user=user, commit=True):
        cache.update_topic(topic.id, topic)
        cache.update_sorted_topics(topic, 'view_count')

    topic_data = topic.json_data()
    topic_data['comments'] = topic.get_comments(json=True)
    topic_data['user_vote'] = Topic.check_vote(user, topic.id)
    topic_data['user_comment'] = Topic.check_comment(user, topic.id)
    context = {
        'js_module': 'view_topic',
        'style': 'view_topic',
        'topic': topic_data,
        'sub_title': topic.title
    }
    return render_template('topics/view.html', **context)
コード例 #9
0
def unvote():
    topic = g.topic
    vote = topic.unvote(g.user)

    if vote:
        try:
            topic.update_points(up=not vote.up)

            user_points = 1
            if vote.up:
                user_points = 5
            topic.user.update_points(up=not vote.up, points=user_points)

            db_session.commit()
            data = {"points": topic.points}

            cache.update_topic(topic.id, topic)
            cache.update_sorted_topics(topic, 'points')
            return jsonify({"data": data})
        except Exception, e:
            logging.error(e)
            return json_error_database()
コード例 #10
0
ファイル: api_topics.py プロジェクト: laoshanlung/flask-demo
def comment_delete():
    data = request.get_json()

    id = data.get('id')

    comment = Comment.find_by_pk(id)

    if comment:
        try:
            comment.topic.update_comment_count(offset=-1)
            comment.topic.update_user_comment_count(offset=-1, user_id=comment.user_id)
            comment.delete()
            db_session.commit()

            cache.update_topic(comment.topic.id, comment.topic)
            cache.update_sorted_topics(comment.topic, 'comment_count')
            return json_data(data)
        except Exception:
            db_session.rollback()
            return json_error_database()
    else:
        return json_error_invalid_request()
コード例 #11
0
def comment_delete():
    data = request.get_json()

    id = data.get('id')

    comment = Comment.find_by_pk(id)

    if comment:
        try:
            comment.topic.update_comment_count(offset=-1)
            comment.topic.update_user_comment_count(offset=-1,
                                                    user_id=comment.user_id)
            comment.delete()
            db_session.commit()

            cache.update_topic(comment.topic.id, comment.topic)
            cache.update_sorted_topics(comment.topic, 'comment_count')
            return json_data(data)
        except Exception:
            db_session.rollback()
            return json_error_database()
    else:
        return json_error_invalid_request()
コード例 #12
0
ファイル: api_topics.py プロジェクト: laoshanlung/flask-demo
def unvote():
    topic = g.topic
    vote = topic.unvote(g.user)

    if vote:
        try:
            topic.update_points(up= not vote.up)

            user_points = 1
            if vote.up:
                user_points = 5
            topic.user.update_points(up= not vote.up, points=user_points)

            db_session.commit()
            data = {
                "points": topic.points
            }

            cache.update_topic(topic.id, topic)
            cache.update_sorted_topics(topic, 'points')
            return jsonify({"data": data})
        except Exception, e:
            logging.error(e)
            return json_error_database()