Skip to content

Commit

Permalink
Resolve circular imports and remove union for markdown models
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Jan 3, 2024
1 parent 6b1b17b commit 432d4f4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 62 deletions.
11 changes: 7 additions & 4 deletions funnel/cli/refresh/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
import rich.progress

from ... import models
from ...models import MarkdownModelUnion, db, sa_orm
from ...models import db, sa_orm
from . import refresh

_M = TypeVar('_M', bound=MarkdownModelUnion)
_M = TypeVar('_M', bound=models.ModelIdProtocol)


class MarkdownModel(Generic[_M]):
"""Holding class for a model that has markdown fields with custom configuration."""

#: Dict of ``{MarkdownModel().name: MarkdownModel()}``
registry: ClassVar[dict[str, MarkdownModel]] = {}
#: Dict of ``{config_name: MarkdownModel()}``, where the fields on the model using
#: that config are enumerated in :attr:`config_fields`
config_registry: ClassVar[dict[str, set[MarkdownModel]]] = {}

def __init__(self, model: type[_M], fields: set[str]) -> None:
Expand Down Expand Up @@ -54,12 +57,12 @@ def reparse(self, config: str | None = None, obj: _M | None = None) -> None:
iter_total = 1
else:
load_columns = (
[self.model.id]
[self.model.id_]
+ [getattr(self.model, f'{field}_text'.lstrip('_')) for field in fields]
+ [getattr(self.model, f'{field}_html'.lstrip('_')) for field in fields]
)
iter_list = (
self.model.query.order_by(self.model.id)
self.model.query.order_by(self.model.id_)
.options(sa_orm.load_only(*load_columns))
.yield_per(10)
)
Expand Down
6 changes: 3 additions & 3 deletions funnel/models/commentset_membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
from . import Mapped, Model, Query, relationship, sa, sa_orm
from .account import Account
from .membership_mixin import ImmutableMembershipMixin
from .project import Project
from .proposal import Proposal
from .update import Update

__all__ = ['CommentsetMembership']

Expand Down Expand Up @@ -100,6 +97,9 @@ def for_user(cls, account: Account) -> Query[Self]:

# Tail imports
from .comment import Comment, Commentset
from .project import Project
from .proposal import Proposal
from .update import Update

CommentsetMembership.new_comment_count = sa_orm.column_property(
sa.select(sa.func.count(Comment.id))
Expand Down
5 changes: 4 additions & 1 deletion funnel/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
types,
)
from .account import Account
from .comment import SET_TYPE, Commentset
from .helpers import (
RESERVED_NAMES,
ImgeeType,
Expand Down Expand Up @@ -1641,3 +1640,7 @@ def __repr__(self) -> str:
# joined model, not the first
grants_via={Rsvp.participant: {'participant', 'project_participant'}},
)


# Tail imports
from .comment import SET_TYPE, Commentset
2 changes: 1 addition & 1 deletion funnel/models/proposal.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
sa_orm,
)
from .account import Account
from .comment import SET_TYPE, Commentset
from .helpers import (
MarkdownCompositeDocument,
add_search_trigger,
Expand Down Expand Up @@ -582,6 +581,7 @@ class ProposalSuuidRedirect(BaseMixin[int, Account], Model):


# Tail imports
from .comment import SET_TYPE, Commentset
from .proposal_membership import ProposalMembership
from .sponsor_membership import ProposalSponsorMembership

Expand Down
64 changes: 11 additions & 53 deletions funnel/models/typing.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
"""Union types for models with shared functionality."""

from __future__ import annotations

from collections.abc import Iterable, Iterator, Sequence
from datetime import datetime
from typing import (
Any,
ClassVar,
Literal,
Protocol,
TypeAlias,
Union,
overload,
runtime_checkable,
)
from typing import Any, ClassVar, Literal, Protocol, overload, runtime_checkable
from uuid import UUID

from sqlalchemy import Table
Expand All @@ -20,55 +13,16 @@
from coaster.sqlalchemy import LazyRoleSet, QueryProperty
from coaster.utils import InspectableSet

from .account import Account, AccountOldId, Team
from .auth_client import AuthClient
from .comment import Comment, Commentset
from .login_session import LoginSession
from .membership_mixin import ImmutableMembershipMixin
from .moderation import CommentModeratorReport
from .project import Project
from .proposal import Proposal
from .rsvp import Rsvp
from .session import Session
from .sync_ticket import TicketParticipant
from .update import Update
from .venue import Venue, VenueRoom

__all__ = [
'UuidModelUnion',
'MarkdownModelUnion',
'ModelProtocol',
'ModelTimestampProtocol',
'ModelUrlProtocol',
'ModelRoleProtocol',
'ModelIdProtocol',
'ModelUuidProtocol',
'ModelSearchProtocol',
]

# All models with a `uuid` attr
UuidModelUnion: TypeAlias = Union[
Account,
AccountOldId,
AuthClient,
Comment,
CommentModeratorReport,
Commentset,
ImmutableMembershipMixin,
LoginSession,
Project,
Proposal,
Rsvp,
Session,
Team,
TicketParticipant,
Update,
Venue,
VenueRoom,
]


# All models with one or more markdown composite columns
MarkdownModelUnion: TypeAlias = Union[
Account, Comment, Project, Proposal, Session, Update, Venue, VenueRoom
]


class ModelProtocol(Protocol):
__tablename__: str
Expand Down Expand Up @@ -135,3 +89,7 @@ class ModelSearchProtocol(ModelUuidProtocol, Protocol):
@property
def title(self) -> Mapped[str] | declared_attr[str]:
...


# Tail imports
from .account import Account

0 comments on commit 432d4f4

Please sign in to comment.