示例#1
0
    def put(self, topicName, jwt_payload=None):
        """Update topic info.
        Only available to admins and mods
        Args:
            This topic will be updated
        """

        if not (jwt_payload.is_admin or jwt_payload.is_mod):
            return (
                Fail("User does not have permissions for this request").
                to_json(),
                403,
            )

        else:
            args = put_parser.parse_args(strict=True)
            topic = Topic.get_topic(topicName)
            if topic is not None:
                if args.descript is not None:
                    topic.descript = args.descript
                db.session.commit()
                return Success({
                    "message": f"{topicName} updated"
                }).to_json(), 200
            return Fail(f"topic {topicName} not found").to_json(), 404
示例#2
0
    def post(self):
        """Create a new post.

        Required Args:
            topic: Topic to post to
            author: Username of post author
            body: Body text of post
            title: Title of the post 

        """
        args = post_parser.parse_args(strict=True)
        LOGGER.info({"Args": args})

        if User.get_user(args.author) is None:
            return Fail(f"author {args.author} does not exist").to_json(), 404

        if Topic.get_topic(args.topic_name) is None:
            return Fail(f"topic {args.topic_name} does not exist").to_json(), 404

        post = Post(
            title=args.title,
            body=args.body,
            author=args.author,
            topic_name=args.topic_name,
        )
        db.session.add(post)
        db.session.flush()
        post_uuid = post.id
        db.session.commit()

        return Success(post.to_json()).to_json(), 201
示例#3
0
    def post(self, jwt_payload=None):
        """Create a new Topic."""

        args = post_parser.parse_args(strict=True)
        LOGGER.info({"Args": args})

        topic = Topic.get_topic(args.name)
        if topic is None:
            try:
                record = Topic(name=args.name, descript=args.descript)
                record.save()
                return Success({
                    "message": f"topic {args.name} created"
                }).to_json(), 200
            except Exception as e:
                LOGGER.error({"Exception": e})
                return Error(str(e)).to_json(), 500
        return Fail(f"topic {args.name} exists").to_json(), 400
示例#4
0
    def delete(self, topicName):
        """

        :param topicName:
        :return:
        """
        topic = Topic.get_topic(topicName)
        if topic is not None:
            topic.delete()
            return Success(f"{topicName} deleted").to_json(), 204
        return Fail(f"topic {topicName} not found").to_json(), 404
示例#5
0
    def get(self, topicName):
        """Get info on a topic

         Args:
            topicName: Topic to lookup
        """
        LOGGER.debug({"Requested Topic": topicName})
        topic = Topic.get_topic(topicName)
        if topic is not None:
            topic_json = topic.to_json(posts=True)
            return Success({"topic": topic_json}).to_json(), 200
        return Fail(f"topic {topicName} not found").to_json(), 404
示例#6
0
 def put(self, topicName):
     """Update topic info
     Args:
         This topic will be updated
     """
     args = put_parser.parse_args(strict=True)
     topic = Topic.get_topic(topicName)
     if topic is not None:
         if args.descript is not None:
             topic.descript = args.descript
         db.session.commit()
         return Success(f"{topicName} updated").to_json(), 200
     return Fail(f"topic {topicName} not found").to_json(), 404
示例#7
0
    def delete(self, topicName, jwt_payload=None):
        """ Delete a topic

        Only an admin has permission to delete (others should not be shown the option)
        :param topicName:
        :return:
        """

        if not jwt_payload.is_admin:
            return Fail("User does not have permission").to_json(), 403
        else:
            topic = Topic.get_topic(topicName)
            if topic is not None:
                topic.delete()
                return Success({
                    "message": f"{topicName} deleted"
                }).to_json(), 204
            return Fail(f"topic {topicName} not found").to_json(), 404
示例#8
0
    def post(self, jwt_payload=None):
        """Create a new post.

        Required Args:
            topic: Topic to post to
            body: Body text of post
            title: Title of the post 

        """
        args = post_parser.parse_args(strict=True)
        LOGGER.info({"Args": args})

        user = User.get_user(jwt_payload.username)

        if user is None:
            return Fail(f"author does not exist").to_json(), 404

        if Topic.get_topic(args.topic_name) is None:
            return Fail(
                f"topic {args.topic_name} does not exist").to_json(), 404

        post = Post(
            title=args.title,
            body=args.body,
            author=user.username,
            topic_name=args.topic_name,
        )
        db.session.add(post)
        db.session.flush()
        post_uuid = post.id
        db.session.commit()

        user.post_count += 1
        user.save()

        return Success(post.to_json()).to_json(), 201
示例#9
0
 def get(self, jwt_payload=None):
     """Get list of all topics."""
     topic_filter = {}
     topics = Topic.get_all()
     topics_json = [res.to_json() for res in topics]
     return Success({"topics": topics_json}).to_json(), 200