Improve the publication table / data types to denormalize the values

Returning profile values where necessary will remove the need to compute
heavy joins unless the user actually wants all the data.

Now Publication points at OrderProfiles which don't expose the sequences
Orders->Sequence->EntryProfile avoids including the full `text` for the
sequence.
This commit is contained in:
Campbell Alden 2026-08-21 00:24:14 +09:00
parent 63e51fa017
commit 86b0d88874
4 changed files with 82 additions and 11 deletions

View file

@ -1,5 +1,14 @@
from .data import Publication, PublicationProfile, Order, Sequence, Entry
from .data import Publication, PublicationProfile, Order, Sequence, Entry, OrderProfile
from .service import PublicationService
from .repo import PublicationRepo
__all__ = ['Publication', 'PublicationProfile', 'Order', 'Sequence', 'Entry', 'PublicationService', 'PublicationRepo']
__all__ = [
'Publication',
'PublicationProfile',
'Order',
'OrderProfile',
'Sequence',
'Entry',
'PublicationService',
'PublicationRepo',
]

View file

@ -2,25 +2,44 @@ from datetime import timedelta
from dataclasses import dataclass, field
@dataclass
class EntryProfile:
id: int
title: str
@dataclass
class Entry:
id: int
title: str
text: str
def to_profile(self) -> EntryProfile:
return EntryProfile(self.id, self.title)
@dataclass
class Sequence:
position: int
entry: Entry
entry: EntryProfile
duration: timedelta | None
@dataclass
class OrderProfile:
id: int
title: str
@dataclass
class Order:
id: int
title: str
sequence_entries: list[Sequence]
def to_profile(self) -> OrderProfile:
return OrderProfile(id=self.id, title=self.title)
@dataclass
class PublicationProfile:
@ -35,7 +54,7 @@ class Publication:
title: str
description: str
by_line: str
orders: list[Order] = field(default_factory=list)
orders: list[OrderProfile] = field(default_factory=list)
# Rather than using a subclass I think this makes sense
def to_profile(self) -> PublicationProfile:

View file

@ -1,6 +1,6 @@
import abc
from .data import Publication, PublicationProfile
from .data import Publication, PublicationProfile, Order, Entry
class PublicationRepo(abc.ABC):
@ -15,3 +15,11 @@ class PublicationRepo(abc.ABC):
@abc.abstractmethod
def get_all_publications(self) -> list[PublicationProfile]:
pass
@abc.abstractmethod
def get_full_order(self, order_id: int) -> Order | None:
pass
@abc.abstractmethod
def get_full_entry(self, entry_id: int) -> Entry | None:
pass