def post(self): """Create a new user. Required in Payload: userame: Username of the new user to be created. password: Passowrd of the user to be created. Optional in Payload: bio: Bio of the user to be created. """ args = post_parser.parse_args(strict=True) LOGGER.info({"Args": args}) user = User.get_user(args.username) if user is None: hashed = bcrypt.hashpw(args.password.encode("utf8"), bcrypt.gensalt()) record = User( username=args.username, pw_hash=hashed, bio=args.bio, displayName=args.displayName, ) record.save() data = {"message": f"user {args.username} created"} return Success(data).to_json(), 201 return Fail(f"user {args.username} exists").to_json(), 400
def put(self, username, jwt_payload=None): """Update user info. Args: username: The user to be updated. jwt_payload: The payload data of the JWT passed in the request """ args = put_parser.parse_args(strict=True) user = User.get_user(username) if user is not None: if args.is_admin is not None and jwt_payload.is_admin: user.is_admin = args.is_admin if args.is_mod is not None and jwt_payload.is_mod: user.is_mod = args.is_mod if not user.is_admin else True if args.displayName is not None and username == jwt_payload.username: user.displayName = args.displayName if args.bio is not None and username == jwt_payload.username: user.bio = args.bio if args.password is not None and username == jwt_payload.username: user.pw_hash = bcrypt.hashpw(args.password.encode("utf8"), bcrypt.gensalt()) db.session.commit() data = {"message": f"{username} updated"} return Success(data).to_json(), 200 return Fail(f"user {username} does not exist").to_json(), 404
def post(self): """Create a new reply. Required Args: post_id: ID of the post to reply to author: Username of post author body: Body text of 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 Post.get_post(args.post_id) is None: return Fail(f"topic {args.post_id} does not exist").to_json(), 404 reply = Reply(body=args.body, author=args.author, post_id=args.post_id) db.session.add(reply) db.session.flush() reply_uuid = reply.id db.session.commit() return Success(reply.to_json()).to_json(), 201
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
def delete(self, username): """Delete a user. Args: username: The user to be deleted. """ user = User.get_user(username) if user is not None: user.delete() return Success(None).to_json(), 204 return Fail(f"user {username} does not exist").to_json(), 404
def get(self, username, jwt_payload=None): """Get info on a user. Args: username: Username to lookup. """ LOGGER.debug({"Requested user": username}) user = User.get_user(username) if user is not None: user_json = user.to_json() return Success(user_json).to_json(), 200 return Fail(f"user {username} not found").to_json(), 404
def init_forum(): db.create_all() if User.get_user(username=FORUM_ADMIN.get("username")) is None: hashed = bcrypt.hashpw( FORUM_ADMIN.get("password").encode("utf8"), bcrypt.gensalt()) admin = User( username=FORUM_ADMIN.get("username"), pw_hash=hashed, is_admin=True, is_mod=True, ) admin.save()
def tearDownClass(self): with app.app_context(): db.drop_all() db.create_all() if User.get_user(username=FORUM_ADMIN.get("username")) is None: hashed = bcrypt.hashpw( FORUM_ADMIN.get("password").encode("utf8"), bcrypt.gensalt()) admin = User( username=FORUM_ADMIN.get("username"), pw_hash=hashed, is_admin=True, is_mod=True, ) admin.save()
def delete(self, username, jwt_payload=None): """Delete a user. Args: username: The user to be deleted. """ user = User.get_user(username) if user is None: return Fail(f"user {username} does not exist").to_json(), 404 if user.username == jwt_payload.username or jwt_payload.is_admin: user.delete() return Success(None).to_json(), 204 return ( Fail(f"Invalid permissions, cannot delete user {username}"). to_json(), 403, )
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