Skip to content

Commit

Permalink
Merge pull request #34 from lcfd/works
Browse files Browse the repository at this point in the history
Works
  • Loading branch information
lcfd authored Dec 13, 2023
2 parents bc61030 + 039e673 commit 760b7fe
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cli/trakcli/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class Project(NamedTuple):
categories: list[str] = []
tags: list[str] = []
customer: str = ""
fare: int = 1
rate: int = 1
2 changes: 1 addition & 1 deletion cli/trakcli/create/commands/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def create_project(
else [],
tags=[t.strip() for t in tags.split(",")] if tags != "" else [],
customer=customer,
fare=hour_rate,
rate=hour_rate,
)

with open(details_path, "w") as details_file:
Expand Down
22 changes: 3 additions & 19 deletions cli/trakcli/create/commands/session.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from datetime import datetime, timedelta
from typing import Annotated, Optional
from trakcli.projects.utils.print_missing_project import print_missing_project

import typer
from rich import print as rprint
from rich.panel import Panel

from trakcli.config.main import get_config
from trakcli.database.database import add_session
from trakcli.database.models import Record
from trakcli.projects.database import get_projects_from_config
Expand Down Expand Up @@ -79,10 +79,8 @@ def create_session(
),
] = False,
):
CONFIG = get_config()

# Check if the project exists
projects_in_config = get_projects_from_config(CONFIG)
projects_in_config = get_projects_from_config()
if len(projects_in_config):
if project_id in projects_in_config:
# Check if today or when is passed
Expand Down Expand Up @@ -146,21 +144,7 @@ def create_session(
)
)
else:
renderable_projects_list = "\n • ".join(projects_in_config)
rprint(
Panel(
title="[red]Missing project[/red]",
renderable=print_with_padding(
"This project doesn't exists.\n\n"
f"Awailable projects: \n{renderable_projects_list}\n\n\n\n"
"Try to run the `trak create project <project name>` command if you want to create a new project."
),
)
)
print_missing_project(projects_in_config)
return
else:
rprint(projects_in_config)

# Check if hours or minutes is passed
# Get category
# Get tag
146 changes: 144 additions & 2 deletions cli/trakcli/create/commands/work.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,147 @@
from datetime import datetime
from typing import Annotated

import typer
from rich import print as rprint
from rich.panel import Panel

from trakcli.projects.database import get_project_from_config, get_projects_from_config
from trakcli.projects.utils.print_missing_project import print_missing_project
from trakcli.utils.print_with_padding import print_with_padding
from trakcli.works.database import (
get_project_works_from_config,
set_project_works_in_config,
)
from trakcli.works.models import Work


def create_work(
id: Annotated[
str,
typer.Argument(help="The id for the new work."),
],
project_id: Annotated[
str,
typer.Option(
"--project-id",
"-p",
help="The id of the project where the new work will be placed.",
),
],
name: Annotated[
str,
typer.Option(
"--name",
"-n",
help="A readable name for the new work.",
),
],
time: Annotated[
int,
typer.Option(
"--time",
"-t",
help="",
),
],
from_date: Annotated[
datetime,
typer.Option(
"--from",
help="",
formats=["%Y-%m-%d"],
),
],
to_date: Annotated[
datetime,
typer.Option(
"--to",
help="",
formats=["%Y-%m-%d"],
),
],
description: Annotated[
str,
typer.Option(
"--description",
"-d",
help="",
),
] = "",
rate: Annotated[
int,
typer.Option(
"--rate",
"-r",
help="",
),
] = 1,
):
projects_in_config = get_projects_from_config()

if project_id in projects_in_config:
details = get_project_from_config(project_id)

# Check if project esists
if details:
works = get_project_works_from_config(project_id)

# Check if id already exists
if works is not None:
work_ids = [w["id"] for w in works]
if id in work_ids:
rprint("")
rprint(
Panel.fit(
title="[yellow]This work id already exists[/yellow]",
renderable=print_with_padding(
"You should change the id parameter or you can just use the work already in the configuration."
),
)
)

return

new_work = Work(
id=id,
name=name,
time=time,
rate=rate,
from_date=from_date.strftime("%Y-%d-%m"),
to_date=to_date.strftime("%Y-%d-%m"),
description=description,
done=False,
)

if works is not None:
works.append(new_work._asdict())
else:
works = [new_work._asdict()]

set_project_works_in_config(project_id, works)

rprint("")
rprint(
Panel.fit(
title="[green]Work created[/green]",
renderable=print_with_padding(f"Work {id} created."),
)
)

return
else:
rprint("")
rprint(
Panel.fit(
title="[red]Error in config[/red]",
renderable=print_with_padding(
"Error in/with details file in project's configuration."
),
)
)

return
else:
print_missing_project(projects_in_config)

def create_work():
rprint("Create a work")
return
44 changes: 22 additions & 22 deletions cli/trakcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from trakcli.config.commands import app as config_app
from trakcli.config.main import get_config
from trakcli.create import app as create_app
from trakcli.database.database import (
add_session,
get_current_session,
Expand All @@ -27,9 +28,10 @@
from trakcli.initialize import initialize_trak
from trakcli.projects.commands import app as projects_app
from trakcli.projects.database import get_projects_from_config
from trakcli.projects.utils.print_missing_project import print_missing_project
from trakcli.report.commands.main import report
from trakcli.utils.print_with_padding import print_with_padding
from trakcli.create import app as create_app
from trakcli.works import app as works_app

console = Console()

Expand All @@ -46,6 +48,7 @@
app.add_typer(config_app, name="config", help="Interact with your configuration.")
app.add_typer(projects_app, name="projects", help="Interact with your projects.")
app.add_typer(create_app, name="create", help="Create something in trak.")
app.add_typer(works_app, name="works", help="Interact with your works.")


@app.callback()
Expand Down Expand Up @@ -151,25 +154,16 @@ def start_tracker(
Panel.fit(
title="▶️ Start",
renderable=print_with_padding(
f"""[bold green]{project}[/bold green] started.
Have a good session!"""
(
f"[bold green]{project}[/bold green] started.\n\n"
"Have a good session!"
)
),
)
)
else:
renderable_projects_list = "\n • ".join(projects_in_config)
rprint("")
rprint(
Panel(
title="[red]Missing project[/red]",
renderable=print_with_padding(
"This project doesn't exists.\n\n"
f"Awailable projects: \n{renderable_projects_list}\n\n\n\n"
"Try to run the `trak create project <project name>` command if you want to create a new project."
),
)
)
print_missing_project(projects_in_config)

return
else:
formatted_start_time = datetime.fromisoformat(record["start"]).strftime(
Expand All @@ -186,6 +180,8 @@ def start_tracker(
)
)

return


@app.command("stop", help="Stop the current trak session.")
def stop_tracker():
Expand All @@ -197,9 +193,10 @@ def stop_tracker():
if record:
stop_trak_session()
message = print_with_padding(
f"""The [bold green]{record['project']}[/bold green] session is over.
Good job!"""
(
f"The [bold green]{record['project']}[/bold green] session is over.\n\n"
"Good job!"
)
)

rprint(Panel.fit(title="⏹️ Stop", renderable=message))
Expand Down Expand Up @@ -272,9 +269,12 @@ def status(
Panel(
title="💬 No active session",
renderable=print_with_padding(
"""Ther aren't active sessions.
Use the command: trak start <project name> to start a new session of work."""
(
"Ther aren't active sessions.\n\n"
"Use the command: trak start <project name> to start a new session of work."
)
),
)
)

return
14 changes: 14 additions & 0 deletions cli/trakcli/projects/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ def get_projects_from_config():
projects.append(details.get("id", "ERROR: No id!"))

return projects


def get_project_from_config(project_id: str):
"""Get a project in the config by id."""

project_path = pathlib.Path(TRAK_FOLDER / "projects" / project_id)

if project_path.exists() and project_path.is_dir():
details_path = project_path / "details.json"
with open(details_path, "r") as f:
details = json.load(f)
return details
else:
return None
Empty file.
20 changes: 20 additions & 0 deletions cli/trakcli/projects/utils/print_missing_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rich import print as rprint
from rich.panel import Panel
from trakcli.utils.print_with_padding import print_with_padding


def print_missing_project(projects_in_config):
renderable_projects_list = "\n • ".join(projects_in_config)
rprint("")
rprint(
Panel.fit(
title="[red]Missing project[/red]",
renderable=print_with_padding(
"This project doesn't exists.\n\n"
f"Awailable projects: \n\n{renderable_projects_list}\n\n\n\n"
"Try to run the `trak create project <project name>` command if you want to create a new project."
),
)
)

return
12 changes: 12 additions & 0 deletions cli/trakcli/works/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import typer

from trakcli.works.commands.delete import delete_work
from trakcli.works.commands.done import done_work
from trakcli.works.commands.list import list_works

app = typer.Typer()


app.command("list")(list_works)
app.command("delete")(delete_work)
app.command("done")(done_work)
Empty file.
Loading

0 comments on commit 760b7fe

Please sign in to comment.