-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplan_pages.py
More file actions
96 lines (85 loc) · 3.65 KB
/
Copy pathplan_pages.py
File metadata and controls
96 lines (85 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from discord import Interaction
from discord.ext import pages
import discord
from utils import get_due_datetime, get_task_info, get_subtasks_recursive
from datetime import datetime
from todoist_api_python.models import Task, Project
from initialization import label_cache, task_cache
from views import AddTaskOptions
class TaskSelector(discord.ui.Select):
def __init__(self, tasks: list[Task]):
self.tasks = {t.id: t for t in tasks}
options = [
discord.SelectOption(
label=t.content[: min(len(t.content), 100)],
value=t.id,
description=t.description[: min(len(t.description), 100)],
)
for t in tasks
]
super().__init__(placeholder="Select A Task For More Info", options=options)
async def callback(self, interaction: Interaction):
task = self.tasks[self.values[0]]
labels = await label_cache.get_labels(interaction.user.id)
await interaction.respond(
embed=await get_task_info(task, labels),
view=AddTaskOptions(task, labels, subtasks=await get_subtasks_recursive(task, await task_cache.get_tasks(
))),
ephemeral=True,
)
async def create_pages(
tasks: list[Task], project_obj: list[Project]
) -> pages.Paginator:
async def create_embed(section_tasks: list[Task]) -> discord.Embed:
embed = discord.Embed(title="Your Tasks")
embed.set_footer(text="Last Updated")
embed.timestamp = datetime.now()
for task in section_tasks:
if task.parent_id:
continue
v = f"`{task.id}`"
if due := await get_due_datetime(task):
v += f" | Due {discord.utils.format_dt(due, 'R')}"
v += f"\n{task.description}" if task.description else ""
if subtasks := [st for st in section_tasks if st.parent_id == task.id]:
v += "\n**Sub-Tasks:**"
for subtask in subtasks:
v += f"\n- {subtask.content} | [{subtask.id}]({subtask.url})"
if due := await get_due_datetime(subtask):
v += f" | Due {discord.utils.format_dt(due, 'R')}"
embed.add_field(
name=(
(task.content[:253] + "...")
if len(task.content) > 256
else task.content
),
value=v,
inline=False,
)
return embed
# TODO: Make The Sorting And Filtering Of Tasks More Efficient
annotated_tasks = [
(await get_due_datetime(t) or datetime.max, t.id, t) for t in tasks
]
annotated_tasks.sort()
tasks = [t for _, _, t in annotated_tasks]
# Group The Tasks Into Different Projects
project_name_map = {project.id: project.name for project in project_obj}
projects: [str, list[Task]] = {"All": tasks, "Inbox": []}
for task in tasks:
if task.project_id:
projects.setdefault(project_name_map[task.project_id], []).append(task)
else:
projects["Inbox"].append(task)
pgs = []
for category, tasks in projects.items():
split_pages = [tasks[i: i + 10] for i in range(0, len(tasks), 10)]
complete_split_pages = []
for group in split_pages:
view = discord.ui.View()
view.add_item(TaskSelector(group))
complete_split_pages.append(
pages.Page(embeds=[await create_embed(group)], custom_view=view)
)
pgs.append(pages.PageGroup(label=category, pages=complete_split_pages))
return pages.Paginator(pages=pgs, show_menu=True, menu_placeholder="Project")