Skip to content

Commit

Permalink
Fix #94 (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWillner committed May 21, 2023
1 parent 9ace142 commit 66bb16f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
10 changes: 9 additions & 1 deletion tests/test_things.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ def test_tags(self):
tag = things.tags(title="Errand")
self.assertEqual("Errand", tag["title"]) # type: ignore

# test tagged items, in particular within headings
tags = things.tasks(tag="Home")
self.assertEqual(1, len(tags))
tasks = things.tasks(project="3x1QqJqfvZyhtw8NSdnZqG")
self.assertEqual(5, len(tasks))
tasks = things.tasks(tag="Home", project="3x1QqJqfvZyhtw8NSdnZqG")
self.assertEqual(1, len(tasks))

def test_get_link(self):
link = things.link("uuid")
self.assertEqual("things:///show?id=uuid", link)
Expand All @@ -228,7 +236,7 @@ def test_projects(self):
projects = things.projects()
self.assertEqual(3, len(projects))
projects = things.projects(include_items=True)
self.assertEqual(4, len(projects[0]["items"]))
self.assertEqual(5, len(projects[0]["items"]))

def test_areas(self):
areas = things.areas()
Expand Down
26 changes: 24 additions & 2 deletions things/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import sqlite3
from textwrap import dedent
from typing import Optional


# --------------------------------------------------
Expand Down Expand Up @@ -246,6 +247,14 @@ def get_tasks( # pylint: disable=R0914
"PROJECT_OF_HEADING.trashed", context_trashed
)

# As a task assigned to a heading is not directly assigned to a project anymore,
# we need to check if the heading is assigned to a project.
# See, e.g. https://github.com/thingsapi/things.py/issues/94
project_filter = make_or_filter(
make_filter("TASK.project", project),
make_filter("PROJECT_OF_HEADING.uuid", project),
)

where_predicate = f"""
TASK.{IS_NOT_RECURRING}
{trashed_filter and f"AND TASK.{trashed_filter}"}
Expand All @@ -256,7 +265,7 @@ def get_tasks( # pylint: disable=R0914
{status_filter and f"AND TASK.{status_filter}"}
{make_filter('TASK.uuid', uuid)}
{make_filter("TASK.area", area)}
{make_filter("TASK.project", project)}
{project_filter}
{make_filter("TASK.heading", heading)}
{make_filter("TASK.deadlineSuppressionDate", deadline_suppressed)}
{make_filter("TAG.title", tag)}
Expand Down Expand Up @@ -622,6 +631,19 @@ def list_factory(_cursor, row):
return row[0]


def remove_prefix(text, prefix):
"""Remove prefix from text (as removeprefix() is 3.9+ only)."""
return text[text.startswith(prefix) and len(prefix) :]


def make_or_filter(*filters):
"""Join filters with OR."""
filters = filter(None, filters)
filters = [remove_prefix(filter, "AND ") for filter in filters]
filters = " OR ".join(filters)
return f"AND ({filters})" if filters else ""


def make_filter(column, value):
"""
Return SQL filter 'AND {column} = "{value}"'.
Expand Down Expand Up @@ -795,7 +817,7 @@ def make_truthy_filter(column: str, value) -> str:
return f"AND NOT IFNULL({column}, 0)"


def make_search_filter(query) -> str:
def make_search_filter(query: Optional[str]) -> str:
"""
Return a SQL filter to search tasks by a string query.
Expand Down

0 comments on commit 66bb16f

Please sign in to comment.