示例#1
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()},
        }
    )
示例#2
0
def set_blog(post, json, user):
    if json is not None:
        if "blog" in json:
            blog = Blog.get_or_none(Blog.id == json["blog"])
            if blog is not None:
                # admin can save to any blog
                if user.is_admin:
                    post.blog = blog
                    return

                role = Blog.get_user_role(blog, user)

                # if user not in blog - then he can't save post here
                if role is None:
                    return BlogError.NoAccess
                # if blog is open - anyone can post here
                if blog.blog_type == 1:
                    post.blog = blog
                # if blog is closed or hidden - only writers and admins can
                # save posts here
                elif blog.blog_type == 2 or blog.blog_type == 3:
                    if role < 3:
                        post.blog = blog
                    else:
                        return BlogError.NoAccess
            else:
                return BlogError.NoBlog
示例#3
0
def invites(url):
    """Пригласить пользователя или принять инвайт"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_found()

    user = get_user_from_request()

    json = request.get_json()

    if "invite" in json:
        invite = BlogInvite.get_or_none(BlogInvite.id == json["invite"])
        if invite is None:
            return errors.invite_not_found()

        if invite.user_to.id != user.id:
            return errors.no_access()

        invite.is_accepted = True
        invite.save()

        BlogParticipiation.create(blog=invite.blog, user=user, role=invite.role)

        return jsonify({"success": 1})
    elif "user" in json and "role" in json:
        user_to = User.get_or_none(User.id == json["user"])
        if user_to is None:
            return errors.not_found()

        role = Blog.get_user_role(blog, user)

        if role is None:
            return errors.no_access()

        role_to = json["role"]
        roles = {"owner": 1, "writer": 2, "reader": 3}

        if role_to not in roles:
            return errors.invite_wrong_role()

        role_to = roles[role_to]
        if role > role_to:
            return errors.no_access()

        invite = BlogInvite.create(
            blog=blog, user_from=user, user_to=user_to, role=role_to
        )

        Notification.create(
            user=user,
            created_date=datetime.datetime.now(),
            text='Вас пригласили в блог "{0}"'.format(blog.title),
            object_type="invite",
            object_id=invite.id,
        )

        return jsonify({"success": 1, "invite": invite.id})
示例#4
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})
示例#5
0
def join(url):
    """Присоеденится к блогу. Работает только с открытми блогами"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_found()
    if blog.blog_type != 1:
        return errors.no_access()

    user = get_user_from_request()
    if user is None:
        return errors.not_authorized()
    if BlogParticipiation.get_or_none(blog=blog, user=user) is None:
        BlogParticipiation.create(blog=blog, user=user, role=3)

    return jsonify({"success": 1})
示例#6
0
def delete_blog(url):
    """Удалить блог"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_found()

    user = get_user_from_request()

    role = Blog.get_user_role(blog, user)
    if role != 1:
        return errors.no_access()

    blog.delete_instance()

    return jsonify({"success": 1})
示例#7
0
def edit_blog(url):
    """Изменить блог"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_found()

    user = get_user_from_request()

    role = Blog.get_user_role(blog, user)
    if role != 1:
        return errors.no_access()

    fill_blog_from_json(blog, request.get_json())

    if not validate_url(blog):
        return errors.blog_url_already_taken()

    blog.save()

    return jsonify({"success": 1, "blog": blog.to_json()})
示例#8
0
def create_blog():
    """Создать блог"""
    user = get_user_from_request()

    url = request.get_json()["url"]
    if Blog.get_or_none(Blog.url == url) is not None:
        return errors.blog_url_already_taken()

    blog = Blog.create(
        created_date=datetime.datetime.now(),
        updated_date=datetime.datetime.now(),
        creator=user,
    )

    BlogParticipiation.create(blog=blog, user=user, role=1)

    fill_blog_from_json(blog, request.get_json())
    blog.save()

    return jsonify({"success": 1, "blog": blog.to_json()})
示例#9
0
def readers(url):
    """Получить список читателей блога"""
    blog = Blog.get_or_none(Blog.url == url)
    if blog is None:
        return errors.not_foun
    user = get_user_from_request()
    has_access = Blog.has_access(blog, user)
    if not has_access:
        return errors.no_access()

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

    return jsonify(
        {
            "success": 1,
            "readers": [u.to_json() for u in paginated_query.get_object_list()],
            "meta": {"page_count": paginated_query.get_page_count()},
        }
    )
示例#10
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()})