Example #1
0
def create_post():
    """Создать пост"""
    user = get_user_from_request()

    post = Post(
        created_date=datetime.datetime.now(),
        updated_date=datetime.datetime.now(),
        creator=user,
    )

    json = request.get_json()

    url = json["url"]
    if Post.get_or_none(Post.url == url) is not None:
        return errors.post_url_already_taken()

    error = set_blog(post, json, user)
    if error is not None:
        error_response = {
            BlogError.NoBlog: errors.blog_not_found(),
            BlogError.NoAccess: errors.blog_no_access(),
        }[error]
        return error_response

    fill_post_from_json(post, json)

    post.save()

    set_tags_for_post(post, json)

    manage_jam_entries(post, json)

    return jsonify({"success": 1, "post": post.to_json()})
Example #2
0
def convert():
    create_app()

    for c in TuComment.select():
        if c.target_type != "topic":
            continue

        creator = User.get_or_none(User.id == c.user)
        if not creator:
            print(
                "Skipped comment. Owner:" + TuUser.get(TuUser.user == c.user).user_login
            )
            continue

        updated = c.comment_date
        if not updated:
            updated = c.comment_date_edit

        text = content.replace_uploads_in_text(creator, c.comment_text)

        post = Post.get_or_none(Post.id == c.target)
        if post is None:
            print("Skipped comment " + str(c.comment) + ". No post" + str(c.target))
            continue

        parent = None
        if c.comment_pid:
            parent = Comment.get_or_none(Comment.id == c.comment_pid)
            if parent is None:
                print("Skipped comment " + str(c.comment) + ". No parent")

        Comment.create(
            id=c.comment,
            post=post,
            creator=creator,
            parent=parent,
            level=c.comment_level,
            created_date=c.comment_date,
            updated_date=updated,
            text=text,
            rating=0,
        )
Example #3
0
def edit_comment(url, comment_id):
    """Редактировать комментарий"""
    post = Post.get_or_none(Post.url == url)
    if post is None:
        return errors.not_found()

    user = get_user_from_request()
    if user is None:
        return errors.not_authorized()

    json = request.get_json()

    text = None
    if "text" in json:
        text = sanitize(json.get("text"))
    else:
        return errors.wrong_payload("text")

    comment = _edit_comment(comment_id, user, text)

    return jsonify({"success": 1, "comment": comment.to_json()})
Example #4
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 #5
0
def get_post_by_id(id):
    """Получить пост по id"""
    post = Post.get_or_none(Post.id == id)
    return _get_post(post)
Example #6
0
def get_post(url):
    """Получить пост по url"""
    post = Post.get_or_none(Post.url == url)
    return _get_post(post)
Example #7
0
def comments(url):
    """Получить список комментариев для поста или добавить новый комментарий"""
    post = Post.get_or_none(Post.url == url)
    if post is None:
        return errors.not_found()

    if request.method == "GET":
        user = get_user_from_request()
        if post.is_draft:

            if user is None:
                return errors.no_access()

            if post.creator != user:
                return errors.no_access()
        return _get_comments("post", post.id, user)
    elif request.method == "POST":
        user = get_user_from_request()
        if user is None:
            return errors.not_authorized()

        json = request.get_json()

        if "text" in json:
            text = sanitize(json.get("text"))
        else:
            return errors.wrong_payload("text")

        parent_id = None
        if "parent" in json:
            parent_id = json["parent"]
        parent = None
        if parent_id:
            parent = Comment.get_or_none(Comment.id == parent_id)

        comment = _add_comment("post", post.id, user, text, parent_id)

        if user.id != post.creator.id:
            t = "Пользователь {0} оставил комментарий к вашему посту {1}: {2}"
            notification_text = t.format(user.visible_name, post.title, text)

            Notification.create(
                user=post.creator,
                created_date=datetime.datetime.now(),
                text=notification_text,
                object_type="comment",
                object_id=comment.id,
            )

        if parent is not None:
            if user.id != parent.creator.id:
                t = "Пользователь {0} ответил на ваш комментарий {1}: {2}"
                notification_text = t.format(user.visible_name, parent.text,
                                             text)

                Notification.create(
                    user=parent.creator,
                    created_date=datetime.datetime.now(),
                    text=notification_text,
                    object_type="comment",
                    object_id=comment.id,
                )

        return jsonify({"success": 1, "comment": comment.to_json()})
Example #8
0
def delete_post_by_id(id):
    """Удалить пост"""
    post = Post.get_or_none(Post.id == id)
    return _delete_post(post)
Example #9
0
def delete_post(url):
    """Удалить пост"""
    post = Post.get_or_none(Post.url == url)
    return _delete_post(post)
Example #10
0
def edit_post_by_id(id):
    """Изменить пост"""
    post = Post.get_or_none(Post.id == id)
    return _edit_post(post)
Example #11
0
def edit_post(url):
    """Изменить пост"""
    post = Post.get_or_none(Post.url == url)
    return _edit_post(post)