Skip to content

Commit

Permalink
feat: add properties for is_today and is_evening (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted authored Dec 5, 2024
1 parent 3563935 commit 234eacb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion tests/test_things.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_process_new():
assert todo._instance_creation_paused is False
assert todo._projects == ["ABCd1ee0ykmXYZqT98huxa"]
assert todo._areas == []
assert todo._is_evening == 0
assert todo._is_evening is False
assert todo._tags == []
assert todo._type == Type.TASK
assert todo._due_date_suppression_date is None
Expand Down
14 changes: 12 additions & 2 deletions tests/test_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,14 @@ def test_cancel():

def test_today():
todo = TodoItem("test task")
assert todo.is_today is False
assert todo.is_evening is False
todo.today()
assert todo.is_today is True
assert todo.is_evening is False
assert todo.destination == Destination.ANYTIME
assert todo.scheduled_date == Util.today()
assert todo._is_evening is False
assert todo._today_index_reference_date == Util.today()
assert todo.changes == {
"_destination",
Expand All @@ -287,11 +292,15 @@ def test_today():

def test_evening():
todo = TodoItem("test task")
assert todo.is_today is False
assert todo.is_evening is False
todo.evening()
assert todo.is_today is True
assert todo.is_evening is True
assert todo.destination == Destination.ANYTIME
assert todo.scheduled_date == Util.today()
assert todo._today_index_reference_date == Util.today()
assert todo._is_evening == 1
assert todo._is_evening is True
assert todo.changes == {
"_destination",
"_scheduled_date",
Expand Down Expand Up @@ -353,7 +362,7 @@ def test_deserialize():
assert todo._instance_creation_paused is False
assert todo._projects == ["ABCd1ee0ykmXYZqT98huxa"]
assert todo._areas == []
assert todo._is_evening == 0
assert todo._is_evening is False
assert todo._tags == []
assert todo._type == Type.TASK
assert todo._due_date_suppression_date is None
Expand All @@ -373,6 +382,7 @@ def test_deserialize():
assert todo._recurrence_rule is None
assert todo._note == Note()
assert not todo._changes
assert serialize_dict(todo) == api_object


def test_update():
Expand Down
20 changes: 0 additions & 20 deletions things_cloud/models/converters.py

This file was deleted.

11 changes: 5 additions & 6 deletions things_cloud/models/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ class Serde(ABC):
type_serializers: dict[type, Callable]

@abstractmethod
def serialize(self, v, *, default=None) -> str:
...
def serialize(self, v, *, default=None) -> str: ... # noqa: E704

@abstractmethod
def deserialize(self, v) -> Any:
...
def deserialize(self, v) -> Any: ... # noqa: E704


class JsonSerde(Serde):
Expand All @@ -32,6 +30,7 @@ def dumps(v, *, default=None, indent: int | None = None) -> str:
def prettydumps(v, *, default=None) -> str:
return JsonSerde.dumps(v, default=default, indent=orjson.OPT_INDENT_2)

# @override # TODO
def serialize(self, v, *, default=None) -> str:
for key, value in v.items():
if serializer := self.field_serializers.get(key):
Expand All @@ -41,8 +40,8 @@ def serialize(self, v, *, default=None) -> str:

return JsonSerde.prettydumps(v, default=default)

@staticmethod
def deserialize(v: bytes | bytearray | str) -> Any:
# @override # TODO
def deserialize(self, v: bytes | bytearray | str) -> Any:
return orjson.loads(v)


Expand Down
24 changes: 17 additions & 7 deletions things_cloud/models/todo.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from __future__ import annotations

from collections.abc import Callable
from datetime import datetime, time, timezone
from enum import Enum
from typing import Any, Deque, ParamSpec, TypeVar
from collections.abc import Callable

import cattrs
from attrs import define, field
from cattr.gen import make_dict_structure_fn
from cattrs.gen import make_dict_unstructure_fn, override

from things_cloud.models.converters import bool_int
from things_cloud.models.serde import TodoSerde
from things_cloud.utils import Util

Expand Down Expand Up @@ -81,7 +80,7 @@ class TodoItem:
_instance_creation_paused: bool = field(default=False, kw_only=True)
_projects: list[str] = field(factory=list, kw_only=True)
_areas: list[str] = field(factory=list, kw_only=True)
_is_evening: bool = field(default=False, converter=bool_int, kw_only=True)
_is_evening: bool = field(default=False, kw_only=True)
_tags: list[Any] = field(factory=list, kw_only=True) # TODO: set data type
_type: Type = field(default=Type.TASK, kw_only=True)
_due_date_suppression_date: datetime | None = field(default=None, kw_only=True)
Expand Down Expand Up @@ -265,11 +264,20 @@ def reminder(self) -> time | None:
def reminder(self, reminder: time | None) -> None:
self._reminder = reminder

@property
def is_today(self) -> bool:
return (
self.destination is Destination.ANYTIME
and self.scheduled_date == Util.today()
)

@property
def is_evening(self) -> bool:
return self.is_today and self._is_evening

@mod("_is_evening")
def evening(self) -> None:
today = Util.today()
self.destination = Destination.ANYTIME
self.scheduled_date = today
self.today()
self._is_evening = True

@mod()
Expand Down Expand Up @@ -309,7 +317,9 @@ def update(self, update: TodoItem, keys: set[str]) -> None:
_instance_creation_paused=override(rename="icp"),
_projects=override(rename="pr"),
_areas=override(rename="ar"),
_is_evening=override(rename="sb"),
_is_evening=override(
rename="sb", struct_hook=lambda value, _: bool(value), unstruct_hook=int
),
_tags=override(rename="tg"),
_type=override(rename="tp"),
_due_date_suppression_date=override(rename="dds"),
Expand Down

0 comments on commit 234eacb

Please sign in to comment.