示例#1
0
    def post(self, user):
        # grab the form
        form = LikeForm(self.request.params)
        username = user

        # grab the user (for their key)
        user = User.query(User.username == username).get()

        if user is None:
            self.redirect('/user/login')
            return

        # grab the post via what should be it's key
        try:
            post = Key(urlsafe=form.key.data).get()
        except Exception as e:
            post = None

        if post is None:
            self.redirect('/')
            return

        if user.username == post.author:
            error = 'you can\'t like or unlike you own post'
            self.render("index.html", error=error)
            return

        # is the post liked by this user already?
        try:
            liked = Like.query(Like.owner == user.key,
                               Like.post == Key(urlsafe=form.key.data)).get()
        except Exception as e:
            print e.message
            liked = None

        # let's set the Like entity up and like the post
        try:
            if liked is None:  # hasn't been liked yet
                liked = Like(owner=user.key,
                             post=Key(urlsafe=form.key.data),
                             liked=True)
                liked.put()
            else:
                liked.liked = True if liked.liked is False else False
                liked.put()

            # inc/dec the post likes
            if liked.liked is True:
                post.likes += 1
            else:
                post.likes -= 1

            post.put()
            # go back to the post!
            self.redirect('/post/view?key=%s' % post.key.urlsafe())
            return
        except Exception as e:
            # go back to the post even if we fail to like it
            self.redirect('/post/view?key=%s' % post.key.urlsafe())
            return
示例#2
0
def like(id):
    user = get_JWT_user()
    user.update_last_activity()
    post = Post.query.get(id)
    if not post:
        raise NotFound
    like = Like.query.filter_by(post_id=id, user_id=user.id).first()
    if like is None:
        like = Like(post=post, author=user)
    like.liked = True
    db.session.add(like)
    try:
        db.session.commit()
    except Exception:
        return try_later()
    return {"like": {"id": like.id}}, 201