/
storage.py
75 lines (45 loc) · 1.62 KB
/
storage.py
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
import datetime
from typing import List, Optional, Protocol
from beaverhabits.app.db import User
class CheckedRecord(Protocol):
@property
def day(self) -> datetime.date: ...
@property
def done(self) -> bool: ...
@done.setter
def done(self, value: bool) -> None: ...
def __str__(self):
return f"{self.day} {'[x]' if self.done else '[ ]'}"
__repr__ = __str__
class Habit[R: CheckedRecord](Protocol):
@property
def id(self) -> str | int: ...
@property
def name(self) -> str: ...
@name.setter
def name(self, value: str) -> None: ...
@property
def star(self) -> bool: ...
@star.setter
def star(self, value: int) -> None: ...
@property
def records(self) -> List[R]: ...
@property
def ticked_days(self) -> list[datetime.date]:
return [r.day for r in self.records if r.done]
async def tick(self, day: datetime.date, done: bool) -> None: ...
def __str__(self):
return self.name
__repr__ = __str__
class HabitList[H: Habit](Protocol):
@property
def habits(self) -> List[H]: ...
async def add(self, name: str) -> None: ...
async def remove(self, item: H) -> None: ...
async def get_habit_by(self, habit_id: str) -> Optional[H]: ...
class SessionStorage[L: HabitList](Protocol):
def get_user_habit_list(self) -> Optional[L]: ...
def save_user_habit_list(self, habit_list: L) -> None: ...
class UserStorage[L: HabitList](Protocol):
async def get_user_habit_list(self, user: User) -> Optional[L]: ...
async def save_user_habit_list(self, user: User, habit_list: L) -> None: ...