Exemplo n.º 1
0
async def social_api_post_flipLikeStatus(request, postId=None, likeId=None):
    """
    反转点赞的状态。当 likeId 为空时,根据 postId 和 用户 cookie 创建新的已经点赞的信息;likeId 不为空,根据相关的对象反转点赞的信息
    :param request: 请求
    :param postId: 帖子Id
    :param likeId: 点赞的Id
    :return:
    """
    username = get_loggined_username_or_denied(request)
    dbm = request.__dbManager__
    if not likeId:
        # 新建新的信息
        if not postId:
            raise ValueError('请指定 postId')
        likedObj = models.Like(likId=next_id(),
                               username=username,
                               postId=postId,
                               liked=False)
        await dbm.insert(likedObj)
    else:
        # 查询已经存在的(建议加上用户名验证等信息)
        likedObj = await dbm.query(models.Like, likeId=likeId)
    # 反转
    if not likedObj:
        raise ValueError('没有指定的帖子点赞对象')
    likedObj.liked = not likedObj.liked
    # 保存到数据库
    await dbm.update(likedObj)
    return likedObj
Exemplo n.º 2
0
async def social_api_post_createPost(request, imgId, imgSrcIndex, title):
    """
    创建一个帖子(需要登陆)
    :param request: 请求
    :param imgId: 图像Id
    :param imgSrcIndex: 图像索引
    :param title: 标题
    :return: 返回帖子对象
    """
    # 获取用户 ID
    username = get_loggined_username_or_denied(request)
    # 创建 Post 对象
    postObj = models.Post(title=title,
                          imageId=imgId,
                          imageSourceIndex=imgSrcIndex,
                          username=username)
    # 保存指定的 ORM 对象
    dbm = request.__dbManager__
    await dbm.insert(postObj)
    # 这里有个问题,如果没任何的评论和点赞信息,那么无法床获得对应的帖子。所以添加一个对应的默认点赞信息和评论信息
    postId = postObj.postId
    defaultliked = models.Like(postId=postId, username='******', liked=True)
    defaultComment = models.Comment(postId=postId,
                                    comment="添加第一条评论吧",
                                    username="******",
                                    to_username=None)
    await dbm.insert(defaultComment)
    await dbm.insert(defaultliked)
    # 返回指定的对象
    return {'postInfo': postObj}
Exemplo n.º 3
0
def db_start():
    engine = create_engine('sqlite:///tmp/likes.db', convert_unicode=True)
    db.create_all()
    db.session.commit()

    like = models.Like()
    like.article ="Dlaczego Kotlin jest przyszłością Androida"
    like.username = "******"
    db.session.add(like)
    db.session.commit()
Exemplo n.º 4
0
def like_post(post_id):
    user_id = current_identity.id
    timestamp = datetime.now()

    if models.Post.query.get(post_id) is None:
        return jsonify({"status": "error", "error": "post not found"}), 404

    like = models.Like(user_id, post_id, timestamp)
    db.session.add(like)
    db.session.commit()

    return jsonify({"status": "ok"})
Exemplo n.º 5
0
def toggle_like(data):
    """Toggles a like for a user, emits updated likecount/status"""
    # Like or unlike?
    print(data)
    post_id = data["postID"]
    like_user = data["username"]
    new_status = not data["status"]

    already_liked = like_exists(post_id, like_user)
    if new_status == True and not already_liked:
        # Add Like to DB
        new_like = models.Like(post_id, like_user)
        DB.session.add(new_like)
        DB.session.commit()
    elif new_status == False and already_liked:
        # Remove like from DB
        DB.session.query(models.Like).filter_by(username=like_user).filter_by(
            post_id=post_id).delete()
        DB.session.commit()
    else:
        print("status/liked mismatch.")
    emit_likes(post_id, like_user, request.sid)
Exemplo n.º 6
0
def add_like(id):
    """
    Adds like to the application, gets id as a parameter in the url, and expects "username" in the request body.
    :param id: the tweet id to like
    :return: the like_id in the system of case of success or in case of re-entering existing like.
    404 page if the original tweet does not exists.
    """
    username = request.json['username']
    duplicate_likes_query = models.Like.query.filter(
        models.Like.username == username, models.Like.post_id == id)
    # if like from this user to this tweet already exist
    if duplicate_likes_query.count() > 0:
        return {'like_id': duplicate_likes_query.first().like_id}

    # if original tweet does not exist -> 404
    models.Tweet.query.get_or_404(id)

    like = models.Like(post_id=id,
                       username=username,
                       timestamp=datetime.datetime.now())
    db.session.add(like)
    db.session.commit()

    return {'like_id': like.like_id}
Exemplo n.º 7
0
    def post(self, id):
        blog = models.BlogPost.by_id(int(id))
        if blog:
            author_id = blog.author_id
            user_id = int(self.read_secure_cookie("id"))
            blog_id = int(id)

            if user_id:
                if user_id != author_id:
                    has_already_liked = models.Like.all().filter(
                        "blog_id =", blog_id).filter("user_id =",
                                                     user_id).get()
                    if not has_already_liked:
                        like = models.Like(user_id=user_id, blog_id=blog_id)
                        like.put()
                        self.redirect('/post/' + id)
                    else:
                        self.render_404()
                else:
                    self.redirect('/post/' + id)
            else:
                self.redirect('/login')
        else:
            self.render_404()
Exemplo n.º 8
0
    def post(self, number, voted):
        self.redirect_if_not_logged_in()
        current_user = models.User.get_by("username", self.get_current_user())
        post = models.Blog.get_by_id(int(number))
        if not post:
            self.render("error.html")

        comments = post.comments
        votes = models.Like.count_likes(post.key().id())
        vote_error = ""
        has_voted_up = ""
        has_voted_down = ""

        if post.author.username == current_user.username:
            vote_error = "You cannot vote on your own post."
            self.render("permalink.html",
                        username=current_user.username,
                        post=post,
                        comments=comments,
                        vote_error=vote_error,
                        user_is_author=True,
                        votes=votes,
                        has_voted_up=has_voted_up,
                        has_voted_down=has_voted_down)
        else:
            if voted == "like":
                if models.Like.vote_of_post(post, current_user):
                    vote_error = "Already voted up. Cannot vote twice."
                    has_voted_up = "voted"
                elif models.Like.vote_of_post(post, current_user) is False:
                    vote = models.Like.gql("WHERE post=:1 AND user=:2", post,
                                           current_user).get()
                    vote.delete()
                    time.sleep(0.2)
                    self.redirect("/blog/%s" % post.key().id())
                else:
                    like = models.Like(user=current_user,
                                       post=post,
                                       status=True)
                    like.put()
                    time.sleep(0.2)
                    has_voted_up = "voted"
                    self.redirect("/blog/%s" % post.key().id())

            elif voted == "dislike":
                if models.Like.vote_of_post(post, current_user):
                    vote = models.Like.gql("WHERE post=:1 AND user=:2", post,
                                           current_user).get()
                    vote.delete()
                    time.sleep(0.2)
                    self.redirect("/blog/%s" % post.key().id())
                elif models.Like.vote_of_post(post, current_user) is False:
                    vote_error = "Already voted down. Cannot vote twice."
                    has_voted_down = "voted"
                else:
                    like = models.Like(user=current_user,
                                       post=post,
                                       status=False)
                    like.put()
                    time.sleep(0.2)
                    has_voted_down = "voted"
                    self.redirect("/blog/%s" % post.key().id())

            self.render("permalink.html",
                        username=current_user.username,
                        post=post,
                        comments=comments,
                        vote_error=vote_error,
                        user_is_author=False,
                        votes=votes,
                        has_voted_up=has_voted_up,
                        has_voted_down=has_voted_down)