Esempio n. 1
0
def unban():
    data = request.get_json()
    id = data.get('id')

    user = User.find_by_pk(id)

    if user:
        user.unban(commit=True)
        return json_data(data)
    else:
        return json_error_invalid_request()
Esempio n. 2
0
def ban():
    data = request.get_json()
    id = data.get('id')
    reason = data.get('reason', 'You have been banned')

    user = User.find_by_pk(id)

    if user:
        user.ban(reason=reason, commit=True)
        return json_data(data)
    else:
        return json_error_invalid_request()
Esempio n. 3
0
def avatar():
    id = request.args.get('id')
    size = request.args.get('size', 100)

    user = User.find_by_pk(id)

    if not user:
        return json_error_invalid_request()

    avatar = None
    if user.login_type == 'facebook':
        avatar = "http://graph.facebook.com/%s/picture?width=%s&height=%s" % (
            user.login_id, size, size)
    elif user.login_type == 'github':
        avatar = "http://www.gravatar.com/avatar/%s?s=%s" % (
            user.social_data.get('gravatar_id'), size)
    elif user.login_type == 'google':
        avatar = user.social_data.get('picture') + "?sz=" + size

    if not avatar:
        return json_error_invalid_request()
    return redirect(avatar, 302)
Esempio n. 4
0
        def decorated_function(*args, **kwargs):
            data = request.get_json()

            # support both json and normal request
            if not data:
                data = request.args
                # now kiss
                data = dict(request.args.items() + kwargs.items())

            topic_id = data.get('id')
            topic_id = data.get('topic_id', topic_id)

            topic = Topic.find_by_pk(topic_id)
            if not topic:
                if json:
                    return json_error_invalid_request()
                else:
                    return abort(400)

            g.topic = topic
            return f(*args, **kwargs)
Esempio n. 5
0
        def decorated_function(*args, **kwargs):
            data = request.get_json()

            # support both json and normal request
            if not data:
                data = request.args
                # now kiss
                data = dict(request.args.items() + kwargs.items())

            article_id = data.get('id')
            article_id = data.get('article_id', article_id)

            article = Article.find_by_pk(article_id)
            if not article:
                if json:
                    return json_error_invalid_request()
                else:
                    return abort(400)

            g.article = article
            return f(*args, **kwargs)
Esempio n. 6
0
        def decorated_function(*args, **kwargs):
            data = request.get_json()

            # support both json and normal request
            if not data:
                data = request.args
                # now kiss
                data = dict(request.args.items() + kwargs.items())

            question_id = data.get('id')
            question_id = data.get('question_id', question_id)

            question = Question.find_by_pk(question_id)
            if not question:
                if json:
                    return json_error_invalid_request()
                else:
                    return abort(400)

            g.question = question
            return f(*args, **kwargs)
Esempio n. 7
0
        def decorated_function(*args, **kwargs):
            data = request.get_json()

            # support both json and normal request
            if not data:
                data = request.args
                # now kiss
                data = dict(request.args.items() + kwargs.items())

            question_id = data.get('id')
            question_id = data.get('question_id', question_id)

            question = Question.find_by_pk(question_id)
            if not question:
                if json:
                    return json_error_invalid_request()
                else:
                    return abort(400)

            g.question = question
            return f(*args, **kwargs)
Esempio n. 8
0
        def decorated_function(*args, **kwargs):
            data = request.get_json()

            # support both json and normal request
            if not data:
                data = request.args
                # now kiss
                data = dict(request.args.items() + kwargs.items())

            article_id = data.get('id')
            article_id = data.get('article_id', article_id)

            article = Article.find_by_pk(article_id)
            if not article:
                if json:
                    return json_error_invalid_request()
                else:
                    return abort(400)

            g.article = article
            return f(*args, **kwargs)
Esempio n. 9
0
        def decorated_function(*args, **kwargs):
            data = request.get_json()

            # support both json and normal request
            if not data:
                data = request.args
                # now kiss
                data = dict(request.args.items() + kwargs.items())

            topic_id = data.get('id')
            topic_id = data.get('topic_id', topic_id)

            topic = Topic.find_by_pk(topic_id)
            if not topic:
                if json:
                    return json_error_invalid_request()
                else:
                    return abort(400)

            g.topic = topic
            return f(*args, **kwargs)
Esempio n. 10
0
def comment_delete():
    data = request.get_json()

    id = data.get('id')

    comment = Comment.find_by_pk(id)

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

            cache.update_article(comment.article.id, comment.article)
            cache.update_sorted_articles(comment.article, 'comment_count')
            return json_data(data)
        except Exception:
            db_session.rollback()
            return json_error_database()
    else:
        return json_error_invalid_request()
Esempio n. 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()
Esempio n. 12
0
            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()
    else:
        return json_error_invalid_request()


@api_topics.route('/topics/search', methods=["GET"])
def search():
    query = request.args.get('query')
    limit = int(request.args.get('limit', 10))
    offset = int(request.args.get('offset', 0))

    result = Topic.search(query, offset=offset, limit=limit)
    topics = result.get('data')

    pagination = dict(limit=limit,
                      offset=offset,
                      total=Topic.count_search(query))
Esempio n. 13
0
                user_points = 10
            article.user.update_points(up= not vote.up, points=user_points)

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

            cache.update_article(article.id, article)
            cache.update_sorted_articles(article, 'points')
            return jsonify({"data": data})
        except Exception, e:
            logging.error(e)
            return json_error_database()
    else:
        return json_error_invalid_request()

@api_articles.route('/articles/search', methods=["GET"])
def search():
    query = request.args.get('query')
    limit = int(request.args.get('limit', 10))
    offset = int(request.args.get('offset', 0))

    result = Article.search(query, offset=offset, limit=limit)
    articles = result.get('data')

    pagination = dict(
        limit=limit,
        offset=offset,
        total=Article.count_search(query)
    )