Exemplo n.º 1
0
    def post(self, playsound_name, **options):
        # require JSON so the cooldown can be null
        post_parser = RequestParser()

        post_parser.add_argument("rename", required=False)
        post_parser.add_argument("link", required=True)
        post_parser.add_argument("volume", type=int, required=True)
        post_parser.add_argument("cooldown", type=int, required=False)
        post_parser.add_argument("cost", type=int, required=False)
        post_parser.add_argument("enabled", type=bool, required=False)

        args = post_parser.parse_args()

        rename = args["rename"]

        link = args["link"]
        if not PlaysoundModule.validate_link(link):
            return "Empty or bad link, links must start with https:// and must not contain spaces", 400

        volume = args["volume"]
        if not PlaysoundModule.validate_volume(volume):
            return "Bad volume argument", 400

        cost = args.get("cost", None)
        if not PlaysoundModule.validate_cost(cost):
            return "Bad cost argument", 400

        # cooldown is allowed to be null/None
        cooldown = args.get("cooldown", None)
        if not PlaysoundModule.validate_cooldown(cooldown):
            return "Bad cooldown argument", 400

        enabled = args["enabled"]
        if enabled is None:
            return "Bad enabled argument", 400

        with DBManager.create_session_scope() as db_session:
            playsound = db_session.query(Playsound).filter(
                Playsound.name == playsound_name).one_or_none()

            if playsound is None:
                return "Playsound does not exist", 404
            # TODO admin audit logs
            playsound.link = link
            playsound.volume = volume
            playsound.cost = cost
            playsound.cooldown = cooldown
            playsound.enabled = enabled

            if rename:
                playsound.name = rename

            db_session.add(playsound)

        return "OK", 200
Exemplo n.º 2
0
    def playsound_edit(playsound_name: str, **options) -> ResponseReturnValue:
        # Update playsound
        try:
            json_data = request.get_json()
            if not json_data:
                return {"error": "Missing json body"}, 400
            data: UpdatePlaysound = UpdatePlaysoundSchema().load(json_data)
        except ValidationError as err:
            return {"error": f"Did not match schema: {json.dumps(err.messages)}"}, 400

        # require JSON so the cooldown can be null

        link = data.link
        # TODO: Migrate to validator
        if not PlaysoundModule.validate_link(link):
            return "Empty or bad link, links must start with https:// and must not contain spaces", 400

        volume = data.volume
        # TODO: Migrate to validator
        if not PlaysoundModule.validate_volume(volume):
            return "Bad volume argument", 400

        # cooldown is allowed to be null/None
        cooldown = data.cooldown
        # TODO: Migrate to validator
        if not PlaysoundModule.validate_cooldown(cooldown):
            return "Bad cooldown argument", 400

        enabled = data.enabled
        # TODO: Migrate to validator
        if enabled is None:
            return "Bad enabled argument", 400

        with DBManager.create_session_scope() as db_session:
            playsound = db_session.query(Playsound).filter(Playsound.name == playsound_name).one_or_none()

            if playsound is None:
                return "Playsound does not exist", 404

            raw_edited_data = {
                "link": (playsound.link, link),
                "volume": (playsound.volume, volume),
                "cooldown": (playsound.cooldown, cooldown),
            }
            # make a dictionary with all the changed values (except for enabled, which has a special case below)
            filtered_edited_data = {k: v for k, v in raw_edited_data.items() if v[0] != v[1]}

            log_msg = f"The {playsound_name} playsound has been updated: "
            log_msg_changes = []

            if playsound.enabled != enabled:
                log_msg_changes.append("enabled" if enabled else "disabled")

            # iterate over changed values and push them to the log msg
            for edited_key, values in filtered_edited_data.items():
                log_msg_changes.append(f"{edited_key} {values[0]} to {values[1]}")

            log_msg += ", ".join(log_msg_changes)

            playsound.link = link
            playsound.volume = volume
            playsound.cooldown = cooldown
            playsound.enabled = enabled

            db_session.add(playsound)

            if len(log_msg_changes):
                AdminLogManager.add_entry("Playsound edited", options["user"], log_msg)

        return "OK", 200
Exemplo n.º 3
0
    def post(self, playsound_name, **options):
        # require JSON so the cooldown can be null
        post_parser = RequestParser()
        post_parser.add_argument("link", required=True)
        post_parser.add_argument("volume", type=int, required=True)
        post_parser.add_argument("cooldown", type=int, required=False)
        post_parser.add_argument("enabled", type=bool, required=False)

        args = post_parser.parse_args()

        link = args["link"]
        if not PlaysoundModule.validate_link(link):
            return "Empty or bad link, links must start with https:// and must not contain spaces", 400

        volume = args["volume"]
        if not PlaysoundModule.validate_volume(volume):
            return "Bad volume argument", 400

        # cooldown is allowed to be null/None
        cooldown = args.get("cooldown", None)
        if not PlaysoundModule.validate_cooldown(cooldown):
            return "Bad cooldown argument", 400

        enabled = args["enabled"]
        if enabled is None:
            return "Bad enabled argument", 400

        with DBManager.create_session_scope() as db_session:
            playsound = db_session.query(Playsound).filter(Playsound.name == playsound_name).one_or_none()

            if playsound is None:
                return "Playsound does not exist", 404

            raw_edited_data = {
                "link": (playsound.link, link),
                "volume": (playsound.volume, volume),
                "cooldown": (playsound.cooldown, cooldown),
            }
            # make a dictionary with all the changed values (except for enabled, which has a special case below)
            filtered_edited_data = {k: v for k, v in raw_edited_data.items() if v[0] != v[1]}

            log_msg = f"The {playsound_name} playsound has been updated: "
            log_msg_changes = []

            if playsound.enabled != enabled:
                log_msg_changes.append("enabled" if enabled else "disabled")

            # iterate over changed values and push them to the log msg
            for edited_key, values in filtered_edited_data.items():
                log_msg_changes.append(f"{edited_key} {values[0]} to {values[1]}")

            log_msg += ", ".join(log_msg_changes)

            playsound.link = link
            playsound.volume = volume
            playsound.cooldown = cooldown
            playsound.enabled = enabled

            db_session.add(playsound)

            if len(log_msg_changes):
                AdminLogManager.add_entry("Playsound edited", options["user"], log_msg)

        return "OK", 200