Skip to content

Commit

Permalink
Specified "sent_at" instead of "sent" for digests
Browse files Browse the repository at this point in the history
  • Loading branch information
G L committed Jul 28, 2024
1 parent df42529 commit fcf53c2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
- POSTMARK_SERVER_API_TOKEN=${POSTMARK_SERVER_API_TOKEN}
- RECIPIENT_EMAIL=${RECIPIENT_EMAIL}
- DELIVERY_SYSTEM=${DELIVERY_SYSTEM}
- DISK_DELIVERY_SYSTEM_DIRECTORY=${DISK_DELIVERY_SYSTEM_DIRECTORY}
- RECIPIENT_EMAIL=${RECIPIENT_EMAIL}
- NUMBER_OF_UNITS_PER_DIGEST=${NUMBER_OF_UNITS_PER_DIGEST}
- DAILY_TIME_OF_DIGEST=${DAILY_TIME_OF_DIGEST}
Expand Down
23 changes: 19 additions & 4 deletions echopages/domain/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import abc
from dataclasses import dataclass
from typing import Callable, List, NewType, Optional
from datetime import datetime
from typing import Any, Callable, Dict, List, NewType, Optional


class Content:
Expand Down Expand Up @@ -34,14 +35,28 @@ def __init__(
self,
id: Optional[int] = None,
content_ids: List[int] = [],
sent: bool = False,
sent_at: Optional[datetime] = None,
) -> None:
self.id = id
self.content_ids = content_ids
self.sent = sent
self.sent_at = sent_at

def mark_as_sent(self) -> None:
self.sent = True
self.sent_at = datetime.now()

def to_dict(self) -> Dict[str, Any]:
sent_at = self.sent_at.isoformat() if self.sent_at else None
return {"id": self.id, "content_ids": self.content_ids, "sent_at": sent_at}

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Digest":
return cls(
id=data["id"],
content_ids=data["content_ids"],
sent_at=datetime.fromisoformat(data["sent_at"])
if data["sent_at"]
else None,
)


class ContentSampler(abc.ABC):
Expand Down
8 changes: 5 additions & 3 deletions echopages/infrastructure/database/file_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def __init__(self, filepath: str):

def _load(self) -> None:
with open(self.filepath, "r") as file:
self.digests = [Digest(**digest_dict) for digest_dict in json.load(file)]
self.digests = [
Digest.from_dict(digest_dict) for digest_dict in json.load(file)
]

def save(self) -> None:
with open(self.filepath, "w") as file:
json.dump([digest.__dict__ for digest in self.digests], file, indent=2)
json.dump([digest.to_dict() for digest in self.digests], file, indent=2)

def get_by_id(self, digest_id: int) -> Optional[Digest]:
self._load()
Expand All @@ -78,7 +80,7 @@ def get_all(self) -> List[Digest]:
def add(self, digest: Digest) -> int:
self._load()
digest.id = (
max([digest.__dict__.get("id", 0) for digest in self.digests], default=0)
max([digest.to_dict().get("id", 0) for digest in self.digests], default=0)
+ 1
)
self.digests.append(digest)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_file_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def test_trigger_digest() -> None:
assert content["author"] in digest_content_str
assert content["location"] in digest_content_str
assert content["text"] in digest_content_str
assert digest.sent
assert digest.sent_at is not None
6 changes: 3 additions & 3 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_generate_digest() -> None:
assert digests[0].content_ids == [
content.id for content in content_objects[:number_of_units]
]
assert digests[0].sent is False
assert digests[0].sent_at is None


def test_deliver_digest() -> None:
Expand All @@ -140,7 +140,7 @@ def test_deliver_digest() -> None:
assert delivery_system.sent_contents == [
("source 1", "content unit 1,content unit 2,content unit 3")
]
assert digest.sent is True
assert digest.sent_at is not None


def test_format_digest_1_content() -> None:
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_trigger_digest() -> None:
assert content["location"] in digest_content_str
assert content["text"] in digest_content_str
digest = uow.digest_repo.get_all()[0]
assert digest.sent
assert digest.sent_at is not None


def test_all_flow() -> None:
Expand Down

0 comments on commit fcf53c2

Please sign in to comment.