Skip to content

Commit

Permalink
Refactor presentation layer with entities properties
Browse files Browse the repository at this point in the history
* Use `project_name`, `workspace_name` on task object
* template messages are now operate only on entities itself
* console printer simplified to use entities properties as well
  • Loading branch information
AndreyMarkinPPC committed Jan 31, 2024
1 parent 364af2c commit 1b90fba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
20 changes: 3 additions & 17 deletions terka/presentations/console/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,26 +303,12 @@ def print_task(self, entities, print_options):
table.add_column(column)

for task in sorted(entities, key=lambda x: x.id, reverse=True):
if tags := task.tags:
tags_text = ",".join([tag.base_tag.text for tag in list(tags)])
else:
tags_text = ""
if collaborators := task.collaborators:
collaborators_texts = sorted([
collaborator.users.name
for collaborator in list(collaborators)
if collaborator.users
])
collaborator_string = ",".join(collaborators_texts)
else:
collaborator_string = ""
if task.is_overdue:
task_id = f"[red]{task.id}[/red]"
elif task.is_stale:
task_id = f"[yellow]{task.id}[/yellow]"
else:
task_id = str(task.id)
project = str(task.project_.name) if task.project_ else ""
printable_row = {
"id":
task_id,
Expand All @@ -335,13 +321,13 @@ def print_task(self, entities, print_options):
"priority":
task.priority.name,
"project":
project,
task.project_name,
"due_date":
str(task.due_date),
"tags":
tags_text,
task.tags_string,
"collaborators":
collaborator_string,
task.collaborators_string,
"time_spent":
formatter.Formatter.format_time_spent(task.total_time_spent),
}
Expand Down
3 changes: 2 additions & 1 deletion terka/presentations/text_ui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,8 @@ def compose(self) -> ComposeResult:
yield plotext
with TabPane("Overview", id="overview"):
project_split = defaultdict(int)
for task in self.get_tasks(all=True):
for task in self.entity.tasks:
task = task.tasks
project = task.project_name
project_split[project] += task.total_time_spent
collaborators = self.entity.collaborators
Expand Down
47 changes: 19 additions & 28 deletions terka/presentations/vim/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def edit_epic_template(epic: entities.epic.Epic, project) -> str:
name: {epic.name}
description: {epic.description}
status: {epic.status.name}
project: {project}
project: {epic.project_name}
"""


Expand All @@ -96,7 +96,7 @@ def edit_story_template(story: entities.story.Story, project) -> str:
name: {story.name}
description: {story.description}
status: {story.status.name}
project: {project}
project: {story.project_name}
"""


Expand All @@ -107,43 +107,42 @@ def edited_task_template(task: entities.task.Task) -> str:
status: {task.status.name}
name: {task.name}
description: {task.description or ""}
project: {task.project_.name if task.project else ""}
assignee: {task.assigned_to.name if task.project else ""}
project: {task.project_name}
assignee: {task.assignee_name}
sprints: {task.sprints[-1].sprint if task.sprints else ""}
epics: {task.epics[-1].epic if task.epics else ""}
stories: {task.stories[-1].story if task.stories else ""}
tags: {task.tags.pop() if task.tags else ""}
collaborators: {task.collaborators.pop() if task.collaborators else ""}
collaborators: {task.collaborators_string}
time_spent: 0 (task total_time_spent {task.total_time_spent})
comment:
"""


def edited_project_template(task: entities.project.Project,
def edited_project_template(project: entities.project.Project,
workspace: int) -> str:
return f"""
# You are editing project {task.id}, enter below:
# You are editing project {project.id}, enter below:
---
status: {task.status.name}
name: {task.name}
description: {task.description if task.description else ""}
workspace: {workspace}
tags: {task.tags.pop() if task.tags else ""}
collaborators: {task.collaborators.pop() if task.collaborators else ""}
status: {project.status.name}
name: {project.name}
description: {project.description if project.description else ""}
workspace: {project.workspace_name}
tags: {project.tags.pop() if project.tags else ""}
collaborators: {project.collaborators.pop() if project.collaborators else ""}
comment:
"""


def completed_task_template(task: entities.task.Task,
project: str | None = None) -> str:
def completed_task_template(task: entities.task.Task) -> str:
return f"""
# You are closing task {task.id}, enter below:
---
status: DONE
time_spent: 0 (task total_time_spent {task.total_time_spent})
comment:
name: {task.name}
project: {project or ""}
project: {task.project_name}
description: {task.description if task.description else ""}
sprints: {task.sprints[-1].sprint if task.sprints else ""}
epics: {task.epics[-1].epic if task.epics else ""}
Expand All @@ -156,27 +155,19 @@ def completed_task_template(task: entities.task.Task,
def generate_message_template(entity,
kwargs: dict[str, str] | None = None) -> str:
if not isinstance(entity, type):
if isinstance(
entity,
(entities.task.Task, entities.story.Story, entities.epic.Epic)):
# TODO: convert project from int to name
project = entity.project
elif isinstance(entity, entities.project.Project):
project = entity.name
workspace = entity.workspace
if isinstance(entity, entities.sprint.Sprint):
if not entity.id:
message_template = new_sprint_template()
else:
message_template = edit_sprint_template(entity)
elif isinstance(entity, entities.project.Project):
message_template = edited_project_template(entity, workspace)
message_template = edited_project_template(entity)
elif isinstance(entity, entities.task.Task):
message_template = edited_task_template(entity)
elif isinstance(entity, entities.epic.Epic):
message_template = edit_epic_template(entity, project)
message_template = edit_epic_template(entity)
elif isinstance(entity, entities.story.Story):
message_template = edit_story_template(entity, project)
message_template = edit_story_template(entity)
else:
if not kwargs:
if entity == entities.task.Task:
Expand All @@ -188,7 +179,7 @@ def generate_message_template(entity,
if entity in (entities.epic.Epic, entities.story.Story):
message_template = new_composite_template(entity)
elif kwargs and kwargs.get("status"):
message_template = completed_task_template(entity, project)
message_template = completed_task_template(entity)
else:
message_template = edited_task_template(entity)
return re.sub("\n +", "\n", message_template.lstrip())
Expand Down

0 comments on commit 1b90fba

Please sign in to comment.