Skip to content

Commit

Permalink
Fix the mixed-use of UUID and str for task IDs.
Browse files Browse the repository at this point in the history
The use of str instead of UUID caused problems during lookup
(e.g., not detecting existing subtasks correctly).

This fixes GitHub issue #1077
  • Loading branch information
gycsaba96 authored and diegogangl committed May 20, 2024
1 parent b32ec93 commit 28c472f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion GTG/core/base_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def new(self) -> Any:
raise NotImplemented


def get(self, key: str) -> Any:
def get(self, key: UUID) -> Any:
"""Get an item by id."""

return self.lookup[key]
Expand Down
10 changes: 5 additions & 5 deletions GTG/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def duplicate_for_recurrent(self, task: Task) -> Task:
return new_task


def new(self, title: str = None, parent: UUID = None) -> Task:
def new(self, title: str = None, parent: Optional[UUID] = None) -> Task:
"""Create a new task and add it to the store."""

tid = uuid4()
Expand All @@ -777,7 +777,7 @@ def from_xml(self, xml: Element, tag_store: TagStore) -> None:
elements = list(xml.iter(self.XML_TAG))

for element in elements:
tid = element.get('id')
tid = UUID(element.get('id'))
title = element.find('title').text
status = element.get('status')

Expand Down Expand Up @@ -841,11 +841,11 @@ def from_xml(self, xml: Element, tag_store: TagStore) -> None:

# All tasks have been added, now we parent them
for element in elements:
parent_tid = element.get('id')
parent_tid = UUID(element.get('id'))
subtasks = element.find('subtasks')

for sub in subtasks.findall('sub'):
self.parent(sub.text, parent_tid)
self.parent(UUID(sub.text), parent_tid)


def to_xml(self) -> Element:
Expand Down Expand Up @@ -934,7 +934,7 @@ def _append_to_parent_model(self,task_id: UUID) -> None:
if not pos[0]: model.append(item)


def add(self, item: Any, parent_id: UUID = None) -> None:
def add(self, item: Any, parent_id: Optional[UUID] = None) -> None:
"""Add a task to the taskstore."""

super().add(item, parent_id)
Expand Down
8 changes: 4 additions & 4 deletions GTG/gtk/editor/taskview.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import re
from time import time
from uuid import uuid4
from uuid import uuid4, UUID
import logging

from gi.repository import Gtk, GLib, Gdk, GObject, GtkSource
Expand Down Expand Up @@ -417,7 +417,7 @@ def on_checkbox_toggle(self, tid: uuid4) -> None:
self.process()


def add_checkbox(self, tid: int, start: Gtk.TextIter) -> None:
def add_checkbox(self, tid: UUID, start: Gtk.TextIter) -> None:
"""Add a checkbox for a subtask."""

task = self.ds.tasks.lookup[tid]
Expand Down Expand Up @@ -483,7 +483,7 @@ def detect_internal_link(self, text: str, start: Gtk.TextIter) -> None:
url_start.forward_chars(match.start())
url_end.forward_chars(match.end())

tid = match.group(0).replace('gtg://', '')
tid = UUID(match.group(0).replace('gtg://', ''))
task = self.ds.tasks.lookup[tid]

if task:
Expand Down Expand Up @@ -769,7 +769,7 @@ def insert(self, text: str) -> None:
# Find the subtasks and store their lines
if line.lstrip().startswith('{!'):
# Get the Task ID
tid = line.replace('{! ', '').replace(' !}', '').strip()
tid = UUID(line.replace('{! ', '').replace(' !}', '').strip())

# Remember there's a line for the title at the top
real_index = index + 1
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def test_xml_load_tree(self):
task_store.from_xml(parsed_xml, None)
self.assertEqual(task_store.count(), 4)
self.assertEqual(task_store.count(root_only=True), 1)
self.assertEqual(len(task_store.get(str(TASK_ID_1)).children), 2)
self.assertEqual(len(task_store.get(TASK_ID_1).children), 2)


def test_xml_load_bad(self):
Expand Down Expand Up @@ -478,7 +478,7 @@ def test_xml_load_bad(self):
</tasklist>
''')

with self.assertRaises(KeyError):
with self.assertRaises(ValueError):
task_store.from_xml(parsed_xml, None)


Expand Down

0 comments on commit 28c472f

Please sign in to comment.