Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion check.sh
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
ruff check --fix && mypy planty && pytest -x && echo "All right, ready to commit!"
(
ruff format --check || (
ruff format && \
echo "Reformatted. You should run this script again." && \
false
)
) && \
ruff check --fix && \
mypy planty && \
pytest -x && \
echo "All right, ready to commit!"
10 changes: 4 additions & 6 deletions planty/application/services/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
convert_to_response,
)
from planty.application.uow import IUnitOfWork
from planty.domain.calendar import multiply_tasks_with_recurrences
from planty.domain.calendar import get_tasks_by_dates
from planty.domain.exceptions import (
ChangingRootSectionError,
MisplaceSectionHierarchyError,
Expand Down Expand Up @@ -77,15 +77,13 @@ async def get_tasks_by_date(
) -> TasksByDatesResponse:
if not_before > not_after:
raise IncorrectDateInterval()
prefiltered_tasks = await self._task_repo.get_prefiltered_tasks_by_due_date(
tasks = await self._task_repo.get_tasks_by_due_to(
not_before=not_before,
not_after=not_after,
user_id=user_id,
)
tasks_by_date = multiply_tasks_with_recurrences(
prefiltered_tasks, not_before, not_after
)
return convert_to_response(tasks_by_date)
tasks_by_dates = get_tasks_by_dates(tasks, not_before, not_after)
return convert_to_response(tasks_by_dates)

async def get_archived_tasks(self, user_id: UUID) -> ArchivedTasksResponse:
tasks = await self._task_repo.get_archived_tasks(user_id)
Expand Down
4 changes: 2 additions & 2 deletions planty/application/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,14 @@ async def test_mark_completed_another_user_task(
(
"2001-01-01",
"2002-12-31",
244,
1,
200,
None,
),
(
"2001-02-01",
"2002-12-31",
233,
0,
200,
None,
),
Expand Down
53 changes: 7 additions & 46 deletions planty/domain/calendar.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,13 @@
from datetime import date

from typing import Generator
import itertools
from dateutil.relativedelta import relativedelta

from planty.application.schemas import TasksByDate, TasksByDates
from planty.domain.task import Task
from planty.domain.types import RecurrencePeriodType


def recurrence_rule(
start_day: date,
period: int,
period_type: RecurrencePeriodType,
not_before: date,
not_after: date,
) -> Generator[date, None, None]:
for i in itertools.count():
d = start_day + relativedelta(**{period_type: period * i}) # type: ignore
if d < not_before:
continue
if d > not_after:
return
yield d


def get_task_recurrences(task: Task, not_before: date, not_after: date) -> list[date]:
if task.due_to is None or task.due_to > not_after:
return []
if task.recurrence is None:
if task.due_to >= not_before:
return [task.due_to]
else:
return []
return list(
recurrence_rule(
start_day=task.due_to,
period=task.recurrence.period,
period_type=task.recurrence.type,
not_before=not_before,
not_after=not_after,
)
)


def multiply_tasks_with_recurrences(
prefiltered_tasks: list[Task],
def get_tasks_by_dates(
tasks: list[Task],
not_before: date,
not_after: date,
) -> TasksByDates:
Expand All @@ -54,10 +16,9 @@ def multiply_tasks_with_recurrences(
TasksByDate(date=not_before + relativedelta(days=i), tasks=[])
for i in range(delta.days + 1)
]
for task in prefiltered_tasks:
for date_ in get_task_recurrences(task, not_before, not_after):
for date_tasks_item in tasks_by_dates:
if date_tasks_item.date == date_:
date_tasks_item.tasks.append(task)
break
for task in tasks:
for date_tasks_item in tasks_by_dates:
if date_tasks_item.date == task.due_to:
date_tasks_item.tasks.append(task)
break
return tasks_by_dates
231 changes: 0 additions & 231 deletions planty/domain/tests/test_calendar.py

This file was deleted.

Loading
Loading