Пример #1
0
def jobs(
    show: bool = Option(True,
                        help="display records for day/week/month/date_key"),
    add: str = Option(
        None, help="Add a new job, job names are stored lowercase only"),
    switch: str = Option(
        None, help="Switch to a different job and clock out current"),
):
    """Show and manage different jobs"""
    if add or switch:
        show = False
    if show:
        print(Job.print_header())
        current_job = State.get().job
        for j in Job.query().all():
            if j.id == current_job.id:
                print(f"{j} <- Current")
            else:
                print(j)
    elif add is not None:
        try:
            j = Job.query().filter(Job.name == add).one()
            print(f"Job '{add.lower()}' already exists.")
        except NoResultFound:
            print(f"Creating job '{add.lower()}'")
            j = Job(name=add)
            j.save()
    elif switch is not None:
        try:
            s = State.get()
            c = Clok.get_last_record()
            if c is not None:
                if c.time_out is None:
                    print(
                        f"Clocking you out of '{s.job.name}' at {get_date()}")
                    Clok.clock_out()
            print(f"Switching to job '{switch.lower()}'")
            j = Job.query().filter(Job.name == switch.lower()).one()
            State.set_job(j)
        except NoResultFound:
            print(f"Job '{switch}' not found")