def create_comment(body, user_id, post_id):
     """
     Créer un nouveau commentaire
     """
     post = db_session.query(models.Post).get(post_id)
     if post is None:
         raise ResourceNotFound("Resource not found")
     user = db_session.query(models.User).get(user_id)
     if user.is_banned:
         raise UnauthorizedUser("User is not authorized")
     comment = models.Comment.create_new_comment(body, user, post)
     db_session.add(comment)
     db_session.commit()
     return comment
Exemplo n.º 2
0
 def delete_post(id, user_id):
     """
     Supprimer un post à partir de son identifiant
     """
     post = db_session.query(models.Post).get(id)
     if post is not None:
         user = db_session.query(models.User).get(user_id)
         if user.is_admin == False:
             raise UnauthorizedUser("User not authorized")
         for comment in post.comments:
             db_session.delete(comment)
         db_session.delete(post)
         db_session.commit()
         return True
     else:
         raise ResourceNotFound("Resource not found")
Exemplo n.º 3
0
 def update_post_by_id(new_title, new_body, id, user_id):
     """
     Mettre à jour un post du blog
     """
     user = db_session.query(models.User).get(user_id)
     if user.is_admin:
         post = db_session.query(models.Post).get(id)
         if post is not None:
             post.title = new_title
             post.body = new_body
             db_session.commit()
             return post
         else:
             raise ResourceNotFound("Resource not found")
     else:
         raise UnauthorizedUser("User is not authorized")
Exemplo n.º 4
0
 def get_post_by_id(id):
     """
     Obtenir un post à partir de son identifiant
     """
     post = db_session.query(models.Post).get(id)
     if post is None:
         raise ResourceNotFound("Resource not found")
     return post
    def get_all_post_comments(post_id):
        """
        Obtenir tous les commentaires à partir de l'identifiant
        du post.
        """
        comments = db_session.query(
            models.Comment).filter(models.Comment.post_id == post_id).all()

        return comments
Exemplo n.º 6
0
 def get_user_by_id(id):
     """
     Obtenir un utilisateur à partir de son identifiant
     """
     user = db_session.query(models.User).get(id)
     if user is not None:
         return user
     else:
         raise UserNotFound("User not found")
Exemplo n.º 7
0
 def get_user_by_username(username):
     """
     Obtenir un utilisateur à partir de son nom d'utilisateur
     """
     user = db_session.query(
         models.User).filter(models.User.username == username).one()
     if user is not None:
         return user
     else:
         raise UserNotFound("User not found")
Exemplo n.º 8
0
 def create_post(title, body, author_id):
     """
     Créer un post
     """
     user = db_session.query(models.User).get(author_id)
     if user.is_admin:
         post = models.Post.create_new_post(title, body, user)
         db_session.add(post)
         db_session.commit()
         return post
     else:
         raise UnauthorizedUser("User is not admin")
    def update_comment(new_body, post_id, comment_id, user_id):
        """
        Mettre à jour un commentaire à partir de son identifiant.
        """
        try:
            comments = db_session.query(models.Comment).filter(
                models.Comment.post_id == post_id,
                models.Comment.id == comment_id).all()
            comment = comments[0]
        except:
            comment = None

        if comment is not None:
            user = db_session.query(models.User).get(user_id)
            if user.is_banned and user.id == user_id:
                raise UnauthorizedUser("User is not authorized")
            else:
                comment.body = new_body
                db_session.commit()
                return comment
        else:
            raise ResourceNotFound("Resource not found")
    def delete_comment(post_id, comment_id, user_id):
        """
        Supprimer un commentaire à partir de son identifiant.
        """
        try:
            comments = db_session.query(models.Comment).filter(
                models.Comment.post_id == post_id,
                models.Comment.id == comment_id).all()
            comment = comments[0]
        except:
            comment = None

        if comment is not None:
            user = db_session.query(models.User).get(user_id)

            if comment.author.id == user.id or user.is_admin:
                db_session.delete(comment)
                db_session.commit()
                return True
            else:
                raise UnauthorizedUser("User is not authorized")
        else:
            raise ResourceNotFound("Resource not found")
    def get_comment_by_id(post_id, comment_id):
        """
        Obtenir un commentaire à partir de son identifiant.
        """
        try:
            comments = db_session.query(models.Comment).filter(
                models.Comment.post_id == post_id,
                models.Comment.id == comment_id).all()
            comment = comments[0]
        except:
            comment = None

        if comment is None:
            raise ResourceNotFound("Resource not found")

        return comment
Exemplo n.º 12
0
 def get_all_users():
     """
     Obtenir tous les utilisateurs
     """
     return db_session.query(models.User).all()
Exemplo n.º 13
0
 def get_all_post():
     """
     Obtenir tous les posts du blog
     """
     return db_session.query(models.Post).all()