From fcf53c29ed4912f162ac10b7ab7ac9c62673eaa6 Mon Sep 17 00:00:00 2001 From: G L Date: Sun, 28 Jul 2024 19:29:29 +0200 Subject: [PATCH] Specified "sent_at" instead of "sent" for digests --- docker-compose.yml | 1 - echopages/domain/model.py | 23 ++++++++++++++++---- echopages/infrastructure/database/file_db.py | 8 ++++--- tests/integration/test_file_services.py | 2 +- tests/unit/test_services.py | 6 ++--- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 411da03..48e64e3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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} diff --git a/echopages/domain/model.py b/echopages/domain/model.py index b411e4e..604a13b 100644 --- a/echopages/domain/model.py +++ b/echopages/domain/model.py @@ -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: @@ -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): diff --git a/echopages/infrastructure/database/file_db.py b/echopages/infrastructure/database/file_db.py index d94f6a7..30b19e0 100644 --- a/echopages/infrastructure/database/file_db.py +++ b/echopages/infrastructure/database/file_db.py @@ -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() @@ -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) diff --git a/tests/integration/test_file_services.py b/tests/integration/test_file_services.py index f24a5e7..400b186 100644 --- a/tests/integration/test_file_services.py +++ b/tests/integration/test_file_services.py @@ -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 diff --git a/tests/unit/test_services.py b/tests/unit/test_services.py index 968faed..7110be5 100644 --- a/tests/unit/test_services.py +++ b/tests/unit/test_services.py @@ -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: @@ -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: @@ -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: