Example #1
0
def convert():
    create_app()

    for v in TuVote.select():
        creator = User.get_or_none(User.id == v.user_voter)
        if not creator:
            print("Skipped vote. Owner:" +
                  TuUser.get(TuUser.user == v.user_voter).user_login)
            continue

        t_id = v.target
        t_type = 1
        if v.target_type == "user":
            t_type = 1
        elif v.target_type == "blog":
            t_type = 2
        elif v.target_type == "topic":
            t_type = 3
        elif v.target_type == "comment":
            t_type = 4

        value = 1 if v.vote_direction > 0 else -1

        Vote.create(
            target_id=t_id,
            target_type=t_type,
            voter=creator,
            vote_value=value,
            created_date=v.vote_date,
            updated_date=v.vote_date,
        )
Example #2
0
def _get_post(post):
    if post is None:
        return errors.not_found()

    if post.is_draft:
        user = get_user_from_request()
        if user is None:
            return errors.no_access()

        if post.creator != user:
            return errors.no_access()

    user = get_user_from_request()

    if post.blog is not None:
        # workaround, delete later. Sometime in the past you can save post
        # without blog, so this check will fail.
        has_access = Blog.has_access(post.blog, user)
        if not has_access:
            return errors.no_access()

    post_dict = post.to_json()
    post_dict = Vote.add_votes_info(post_dict, 3, user)

    entries = JamEntry.get_entries_for_post(post)
    post_dict["jam_entries"] = [e.to_json() for e in entries]

    return jsonify({"success": 1, "post": post_dict})
Example #3
0
def posts(url):
    """Получить список постов для блога"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_found()

    user = get_user_from_request()
    has_access = Blog.has_access(blog, user)
    if not has_access:
        return errors.no_access()

    query = Post.get_posts_for_blog(blog)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, user) for p in posts]

    return jsonify(
        {
            "success": 1,
            "posts": posts,
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
Example #4
0
def user(username):
    """Получить подробную информацию о пользователе"""
    user = User.get_or_none(User.username == username)

    if user is None:
        return errors.not_found()

    user_dict = user.to_json()
    user_dict = Vote.add_votes_info(user_dict, 1, get_user_from_request())
    user_dict = Achievement.add_achievements(user_dict)

    return jsonify({"success": 1, "user": user_dict})
Example #5
0
def count_guess_vote(game, player, vote):
    new_vote = Vote(result=vote, player=player)
    game.guessVotes.append(new_vote)

    if len(game.get_correct_guess_votes()) == len(game.players) - 1:
        game.current_player.guessed = True

    if not game.get_guessing_players():
        game.state = GameState.FINISHED

    save_data(game)
    return new_vote
Example #6
0
def get_single_blog(url):
    """Получить блог по указанному url"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_found()

    user = get_user_from_request()
    has_access = Blog.has_access(blog, user)

    if not has_access:
        return errors.no_access()

    blog_dict = blog.to_json()
    blog_dict = Vote.add_votes_info(blog_dict, 2, user)
    return jsonify({"success": 1, "blog": blog_dict})
Example #7
0
def get_posts():
    """Получить список публичных постов"""
    query = Post.get_public_posts()
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, get_user_from_request()) for p in posts]

    return jsonify({
        "success": 1,
        "posts": posts,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Example #8
0
def users():
    """Получить список пользователей"""
    query = User.get_users_sorted_by_active_date()
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    users = [u.to_json() for u in paginated_query.get_object_list()]
    users = [Vote.add_votes_info(u, 1, get_user_from_request()) for u in users]

    return jsonify({
        "success": 1,
        "users": users,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Example #9
0
def get_blogs():
    """Получить список публичных блогов"""
    query = Blog.get_public_blogs()
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    blogs = [b.to_json() for b in paginated_query.get_object_list()]
    blogs = [Vote.add_votes_info(b, 2, get_user_from_request()) for b in blogs]

    return jsonify(
        {
            "success": 1,
            "blogs": blogs,
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
Example #10
0
def tag(title):
    """Получить посты с указанным тегом"""
    tag = Tag.get_or_none(Tag.title == title)
    if tag is None:
        return errors.not_found()

    query = Post.get_public_posts_with_tag(tag)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, get_user_from_request()) for p in posts]

    return jsonify(
        {
            "success": 1,
            "posts": posts,
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
Example #11
0
def get():
    search_type = request.args.get("type")
    search_query = request.args.get("q")
    limit = max(1, min(int(request.args.get("limit") or 20), 100))

    type = 0

    if not search_query or len(search_query) == 0:
        return errors.wrong_payload("q")

    if search_type == "post":
        query = Post.get_public_posts().where(
            Post.title.contains(search_query)
            | Post.text.contains(search_query))
        type = 3
    elif search_type == "blog":
        query = Blog.get_public_blogs().where(
            Blog.title.contains(search_query)
            | Blog.description.contains(search_query))
        type = 2
    elif search_type == "user":
        query = User.select().where(
            User.username.contains(search_query)
            | User.name.contains(search_query))
        type = 1
    else:
        return errors.wrong_payload("type")

    paginated_query = PaginatedQuery(query, paginate_by=limit)
    items = [item.to_json() for item in paginated_query.get_object_list()]
    items = [
        Vote.add_votes_info(i, type, get_user_from_request()) for i in items
    ]

    return jsonify({
        "success": 1,
        "result": items,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Example #12
0
def user_posts(username):
    """Получить список постов пользователя"""
    user = User.get_or_none(User.username == username)

    if user is None:
        return errors.not_found()

    query = Post.get_user_posts(user)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    posts = [p.to_json() for p in paginated_query.get_object_list()]
    posts = [Vote.add_votes_info(p, 3, get_user_from_request()) for p in posts]

    return jsonify({
        "success": 1,
        "posts": posts,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Example #13
0
def user_blogs(username):
    """Получить список блогов пользователя"""
    user = User.get_or_none(User.username == username)

    if user is None:
        return errors.not_found()

    query = Blog.get_blogs_for_user(user)
    limit = max(1, min(int(request.args.get("limit") or 20), 100))
    paginated_query = PaginatedQuery(query, paginate_by=limit)

    blogs = [b.to_json() for b in paginated_query.get_object_list()]
    blogs = [Vote.add_votes_info(b, 2, get_user_from_request()) for b in blogs]

    return jsonify({
        "success": 1,
        "blogs": blogs,
        "meta": {
            "page_count": paginated_query.get_page_count()
        },
    })
Example #14
0
def vote():
    """
    Добавить голос.
    """
    user = get_user_from_request()

    json = request.get_json()

    t_id = json["target_id"]
    t_type = 0

    notification_str_type = ""
    notification_to_whom = None
    notification_object_type = ""

    if json["target_type"] == "user":
        user_to = User.get_or_none(User.id == t_id)
        if user_to is None:
            return errors.vote_no_target()
        t_type = 1

        notification_str_type = "профиль"
        notification_to_whom = user_to
        notification_object_type = "user"

    elif json["target_type"] == "blog":
        blog = Blog.get_or_none(Blog.id == t_id)
        if blog is None:
            return errors.vote_no_target()
        t_type = 2

        notification_str_type = "блог"
        notification_to_whom = blog.creator
        notification_object_type = "blog"

    elif json["target_type"] == "post":
        post = Post.get_or_none(Post.id == t_id)
        if post is None:
            return errors.vote_no_target()
        t_type = 3

        notification_str_type = "пост"
        notification_to_whom = post.creator
        notification_object_type = "post"

    elif json["target_type"] == "comment":
        comment = Comment.get_or_none(Comment.id == t_id)
        if comment is None:
            return errors.vote_no_target()
        t_type = 4

        notification_str_type = "комментарий"
        notification_to_whom = comment.creator
        notification_object_type = "comment"
    else:
        return errors.vote_no_target_type()

    value = json["value"]
    if value > 0:
        value = 1
    elif value < 0:
        value = -1
    else:
        value = 0

    vote = Vote.get_or_none(Vote.voter == user,
                            target_id=t_id,
                            target_type=t_type)
    if vote:
        vote.vote_value = value
        vote.updated_date = datetime.datetime.now()
    else:
        vote = Vote(
            created_date=datetime.datetime.now(),
            updated_date=datetime.datetime.now(),
            voter=user,
            target_id=t_id,
            target_type=t_type,
            vote_value=value,
        )

    notification_str_val = "+1" if value > 0 else "-1"
    notification_text = "{0}: Пользователь {1} оценил ваш {2}".format(
        notification_str_val, user.visible_name, notification_str_type)

    Notification.create(
        user=notification_to_whom,
        created_date=datetime.datetime.now(),
        text=notification_text,
        object_type=notification_object_type,
        object_id=t_id,
    )

    vote.save()

    return jsonify({"success": 1, "vote": vote.to_json()})
Example #15
0
def _get_comments(type, object_id, user):
    query = Comment.get_comments_for(type, object_id)
    comments = [c.to_json() for c in query]
    comments = [Vote.add_votes_info(c, 4, user) for c in comments]

    return jsonify({"success": 1, "comments": comments})
Example #16
0
def increment_next_votes(game, player):
    game.nextVotes.append(Vote(result=True, player=player))
    save_data(game)
    return len(game.get_next_votes()) == len(game.get_connected_players()) - 1