def post(): """ Method saves a new admin user. :return: a success message / error message """ data = _user_parser.parse_args() if data['secret_key'] == ADMIN_SECRET_KEY: if UserModel.find_user_by_username(data["username"]): logging.info("User already exists") return {"message": "User already exists"}, 400 if data['team'] != 'Support': logging.info( "User team not correct. This command is to create Support members." ) return { "message": "User team not correct. This command is to create Support members." }, 400 user = UserModel( data["username"], hashlib.sha256(data["password"].encode("utf-8")).hexdigest(), 'Support') user.save_to_db() logging.info("User {} created".format(data["username"])) return {"message": "User {} created".format(data["username"])} else: logging.info( "Unloged users cannot created users without a correct secret_key" ) return { "message": "Unloged users cannot created users without a correct secret_key" }, 400
def delete(model): """ Static method that fetches and deletes the camera entry with a specific model. Can only be done if the user logged in is a support team user. :param model: camera model :return: a success message / an error message """ user_team = UserModel.find_user_by_id(get_current_user()).team if user_team == 'Support': camera = CameraModel.find_camera_by_model(model) if camera: camera.remove_from_db() logging.info("Camera deleted!") return { "message": "Camera deleted!" } logging.info("Camera not found!") return { "message": "Camera not found!" }, 404 else: logging.info("Non authorized user!") return { "message": "Non authorized user!" }, 400
def post(): """ Method saves a new camera. Can only be done if the user logged in is a support team user. :return: a success message / error message """ user_team = UserModel.find_user_by_id(get_current_user()).team if user_team == 'Support': data = _drone_parser.parse_args() if CameraModel.find_camera_by_model(data["model"]): logging.info("The camera already exists") return { "message": "The camera already exists!" }, 400 drone = CameraModel(data["model"], data["megapixels"], data["brand"]) drone.save_to_db() logging.info("Camera {} created!".format(data["model"])) return { "message": "Camera {} created!".format(data["model"]) } else: logging.info("Non authorized user!") return { "message": "Non authorized user!" }, 400
def delete(user_id): """ Static method that fetches and deletes the user entry with a specific id. Can only be done if the user logged in is a support team user. :param user_id: user id :return: a success message / an error message """ user_team = UserModel.find_user_by_id(get_current_user()).team if user_team == 'Support': user = UserModel.find_user_by_id(user_id) if user: user.remove_from_db() logging.info("User deleted") return {"message": "User deleted"} logging.info("User not found") return {"message": "User not found"}, 404 else: logging.error("Unauthorized user") return {"message": "Non authorized user"}, 400
def get(user_id): """ Static method that fetches and returns the user entry with a specific id :param user_id: user id :return: a dict with the camera data / an error message """ user = UserModel.find_user_by_id(user_id) if user: logging.info(user.json()) return user.json() logging.info("User not found") return {"message": "User not found"}, 404
def get(): """ Static method that fetches and returns all users, sorted by name :return: a list of dicts with the users data / an error message """ users = UserModel.find_users_by_name() if users: output = {} for ind, user in enumerate(users): output[str(ind)] = user.json() logging.info(output) return output logging.info("Users not found") return {"message": "Users not found"}, 404
def delete(serial_number): """ Static method that fetches and deletes the drone entry with a specific serial number. Can only be done if the user logged in is a support team user. :param serial_number: drone serial number :return: a success message / an error message """ user_team = UserModel.find_user_by_id(get_current_user()).team if user_team == 'Support': drone = DroneModel.find_drone_by_serial(serial_number) if drone: drone.remove_from_db() logging.info("Drone deleted") return {"message": "Drone deleted"} logging.info("No drone found") return {"message": "Drone not found"}, 404 else: logging.info("Non authorized user") return {"message": "Non authorized user"}, 400
def post(): """ This method checks if the user that is trying to login is a registered user, and returns authentication data :return: a dict with authentication tokens / an error message """ data = _user_parser.parse_args() user = UserModel.find_user_by_username(data["username"]) if user and user.password == hashlib.sha256( data["password"].encode("utf-8")).hexdigest(): access_token = create_access_token( identity=user.id, fresh=True) # Puts User ID as Identity in JWT refresh_token = create_refresh_token( identity=user.id) # Puts User ID as Identity in JWT logging.info("Login credentials obtained") return { "access_token": access_token, "refresh_token": refresh_token } logging.info("Invalid credentials") return {"message": "Invalid credentials"}, 401
def post(): """ Method saves a new drone. Can only be done if the user logged in is a support team user. :return: a success message / error message """ user_team = UserModel.find_user_by_id(get_current_user()).team if user_team == 'Support': data = _drone_parser.parse_args() for camera in data['cameras'].split(','): found_camera = CameraModel.find_camera_by_model(camera.strip()) if not found_camera: logging.info("Camera not correct: {}".format( camera.strip())) return { "message": "Camera not correct: {}".format(camera.strip()) }, 400 if DroneModel.find_drone_by_serial(data["serial_number"]): logging.info("Drone {} already exists".format( data["serial_number"])) return { "message": "Drone {} already exists".format(data["serial_number"]) }, 400 drone = DroneModel(data["serial_number"], data["name"], data["brand"], data['cameras']) drone.save_to_db() logging.info("Drone {} created".format(data["serial_number"])) return { "message": "Drone {} created".format(data["serial_number"]) } else: logging.info("Non authorized user") return {"message": "Non authorized user"}, 400
def post(): """ Method saves a new user. Can only be done if the user logged in is a support team user. :return: a success message / error message """ data = _user_parser.parse_args() user = get_current_user() if user: user_team = UserModel.find_user_by_id(user).team if user_team == 'Support': if UserModel.find_user_by_username(data["username"]): logging.info("User already exists") return {"message": "User already exists"}, 400 if data['team'] not in POSSIBLE_TEAMS: logging.info( "User team not correct. It has to be one of the following ones:" + ', '.join(POSSIBLE_TEAMS)) return { "message": "User team not correct. It has to be one of the following ones:" + ', '.join(POSSIBLE_TEAMS) }, 400 user = UserModel( data["username"], hashlib.sha256( data["password"].encode("utf-8")).hexdigest(), data['team']) user.save_to_db() logging.info("User {} created".format(data["username"])) return {"message": "User {} created".format(data["username"])} else: logging.info("Non authorized user") return {"message": "Non authorized user"}, 400 else: logging.info("Unlogged users cannot create other users") return {"message": "Unlogged users cannot create other users"}, 400