Ejemplo n.º 1
0
    def delete(self, post_id):
        """ REST delete method

        Note:
            This also updates the corresponding Post by subtracting 1 to its like property

        Args:
            post_id: The post's id that will be unliked

        Returns:
            A JSON object with a simple deleted key value true

        Raises:
            LoginException: if user is not logged in
            OwnerException: if like was not found
        """

        if not is_login():
            raise LoginException(message='not logged in')

        like = Like.get_by_post_user(long(post_id), current_user_id())

        if like:
            post = Post.get_by_id(long(post_id))
            post.likes = post.likes - 1
            post.put()
            like.key.delete()
            return jsonify(delete=True)
        else:
            raise OwnerException(message='not auth')
Ejemplo n.º 2
0
    def is_valid(self):
        """ Check if Like is valid

        Returns:
            True if user and post exist and the user is not the owner of the post
        """

        user = User.get_by_id(self.user.id())
        post = Post.get_by_id(self.post.id())
        if user and post and not (post.user.id() == user.key.id()):
            return True
        return False
Ejemplo n.º 3
0
    def create(self, post_id):
        """ REST create method

        Note:
            This also updates the corresponding Post by adding 1 to its like property

        Args:
            post_id: The post's id that will be liked

        Returns:
            A JSON object with the like information

        Raises:
            LoginException: if user is not logged in
            FormException: if the like is invalid || like already exist
        """

        if not is_login():
            raise LoginException(message='not logged in')

        like = Like(
            post=ndb.Key('Post', long(post_id)),
            user=ndb.Key('User', long(current_user_id())),
        )

        if like.is_valid() and not Like.already_exist(like.post.id(),
                                                      like.user.id()):
            post = Post.get_by_id(long(post_id))
            post.likes = post.likes + 1
            post.put()

            key = like.put()
            ld = like.to_dict()
            ld['id'] = key.id()
            ld['post'] = like.post.id()
            ld['user'] = like.user.id()
            return jsonify(**ld)
        else:
            raise FormException(message='not auth')
Ejemplo n.º 4
0
    def get(self, post_id):
        """ REST get method

        Args:
            post_id: The id the post to get

        Returns:
            A JSON Post object

        Raises:
            OwnerException: if post is not found
        """

        post = Post.get_by_id(long(post_id))
        if post:
            post_dict = post.to_dict()
            post_dict['id'] = post.key.id()
            user_key = post_dict['user']
            post_dict['isOwner'] = current_user_id() == user_key.id()
            post_dict.pop('user', None)
            return jsonify(**post_dict)
        else:
            raise OwnerException(message='unauth data')
Ejemplo n.º 5
0
    def delete(self, post_id):
        """ REST delete method

        Args:
            post_id: The post's id that will be deleted

        Returns:
            A JSON object key deleted set to true

        Raises:
            LoginException: if user is not logged in
            OwnerException: if Post does not belong to current user
        """

        if not is_login():
            raise LoginException(message='not logged in')

        post = Post.get_by_id(long(post_id))
        if post and post.user.id() == current_user_id():
            post.key.delete()
            return jsonify(deleted=True)
        else:
            raise OwnerException(message='unauth data')