Esempio n. 1
0
def create_task():
    if len(sources) == 0:
        log.error_print(
            "No sources found. Please add a source before creating a task")
        return

    if len(notif_agents) == 0:
        log.error_print(
            "No notification agents found. Please add a notification agent before creating a task"
        )
        return

    creator.print_title("Add Task")
    task_creator(Task())
Esempio n. 2
0
def create_task_add_notif_agents(default=None):
    from lib.core.state import State
    notif_agents_dict = State.get_notif_agents()

    default_str = ""
    if default is None and len(notif_agents_dict) == 1:
        default = [list(notif_agents_dict)[0]]

    if default is not None:
        first = True
        for s in default:
            if not s in notif_agents_dict:
                continue

            if first:
                default_str = f"[{notif_agents_dict[s].name}"
                first = False
            else:
                default_str = f"{default_str}, {notif_agents_dict[s].name}"

        default_str = f" {default_str}]"

    add_notif_agents = []

    if len(notif_agents_dict) == 0:
        log.error_print(f"No notif_agents found. Please add a notif_agent ")
        return

    notif_agents_list = list(notif_agents_dict.values())
    remaining_notif_agents = notif_agents_list.copy()
    while len(remaining_notif_agents) > 0:
        i = 0
        for s in remaining_notif_agents:
            print(f"{i} - {s.name}")
            i = i + 1

        choices = "0"
        if len(remaining_notif_agents) > 1:
            choices = f"0-{len(remaining_notif_agents) - 1}"

        if len(add_notif_agents) > 0:
            print("r - reset")
            print("d - done")

        if default is None or len(add_notif_agents) > 0:
            notif_agent_index_str = input(f"notif_agent [{choices}]: ")
        else:
            notif_agent_index_str = input(
                f"notif_agent [{choices}]:{default_str} ")

        if default is not None and notif_agent_index_str == "" and len(
                add_notif_agents) == 0:
            return default

        if len(remaining_notif_agents) == 0 and notif_agent_index_str == "":
            add_notif_agents.append(remaining_notif_agents[notif_agent_index])
            break

        if len(add_notif_agents):
            if notif_agent_index_str == "d":
                break
            elif notif_agent_index_str == "r":
                print("Resetting...")
                remaining_notif_agents = notif_agents_list.copy()
                add_notif_agents = []

        if re.match("[0-9]+$", notif_agent_index_str):
            notif_agent_index = int(notif_agent_index_str)

            if notif_agent_index >= 0 and notif_agent_index < len(
                    notif_agents_list):
                add_notif_agents.append(
                    remaining_notif_agents[notif_agent_index])
                del (remaining_notif_agents[notif_agent_index])
                if len(remaining_notif_agents) == 0:
                    break

                confirm = creator.yes_no("Add another?", "y")
                if confirm == "n":
                    break

    result = []
    for s in add_notif_agents:
        result.append(s.id)

    return result