Skip to content

Commit

Permalink
Showing blocks in schedule + minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arutemu64 committed Sep 17, 2024
1 parent 24f61ec commit 115f922
Show file tree
Hide file tree
Showing 34 changed files with 375 additions and 183 deletions.
112 changes: 0 additions & 112 deletions fanfan/application/events/common.py

This file was deleted.

2 changes: 1 addition & 1 deletion fanfan/application/events/get_current_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def __call__(self) -> FullEventDTO:
event = await self.session.scalar(
select(Event)
.where(Event.current.is_(True))
.options(joinedload(Event.nomination))
.options(joinedload(Event.nomination), joinedload(Event.block))
)
if event:
return event.to_full_dto()
Expand Down
4 changes: 2 additions & 2 deletions fanfan/application/events/get_event_by_id.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import contains_eager, joinedload, undefer
from sqlalchemy.orm import contains_eager, joinedload

from fanfan.application.common.id_provider import IdProvider
from fanfan.core.exceptions.events import EventNotFound
Expand All @@ -20,7 +20,7 @@ async def __call__(
query = (
select(Event)
.where(Event.id == event_id)
.options(joinedload(Event.nomination), undefer(Event.queue))
.options(joinedload(Event.nomination), joinedload(Event.block))
)

if self.id_provider.get_current_user_id():
Expand Down
4 changes: 2 additions & 2 deletions fanfan/application/events/get_schedule_page.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlalchemy import and_, func, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import contains_eager, joinedload, undefer
from sqlalchemy.orm import contains_eager, joinedload

from fanfan.application.common.id_provider import IdProvider
from fanfan.core.models.event import UserFullEventDTO
Expand All @@ -22,7 +22,7 @@ async def __call__(
query = (
select(Event)
.order_by(Event.order)
.options(joinedload(Event.nomination), undefer(Event.queue))
.options(joinedload(Event.nomination), joinedload(Event.block))
)
total_query = select(func.count(Event.id))

Expand Down
Empty file.
2 changes: 2 additions & 0 deletions fanfan/application/schedule_mgmt/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ANNOUNCEMENT_TIMESTAMP = "announcement_timestamp"
ANNOUNCEMENT_LOCK = "announcement_lock"
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
from sqlalchemy.ext.asyncio import AsyncSession

from fanfan.application.common.id_provider import IdProvider
from fanfan.application.events.common import (
from fanfan.application.schedule_mgmt.common import (
ANNOUNCEMENT_LOCK,
ANNOUNCEMENT_TIMESTAMP,
prepare_notifications,
)
from fanfan.application.schedule_mgmt.utils.prepare_notifications import (
EventChangeDTO,
EventChangeType,
PrepareNotifications,
)
from fanfan.core.exceptions.events import (
AnnounceTooFast,
Expand Down Expand Up @@ -39,11 +43,13 @@ def __init__(
session: AsyncSession,
redis: Redis,
id_provider: IdProvider,
prepare_notifications: PrepareNotifications,
notifier: Notifier,
) -> None:
self.session = session
self.redis = redis
self.id_provider = id_provider
self.prepare_notifications = prepare_notifications
self.notifier = notifier

async def __call__(self, event_id: int, after_event_id: int) -> MoveEventResult:
Expand Down Expand Up @@ -85,10 +91,10 @@ async def __call__(self, event_id: int, after_event_id: int) -> MoveEventResult:
await self.session.refresh(event)

# Prepare notifications
notifications = await prepare_notifications(
notifications = await self.prepare_notifications(
session=self.session,
next_event_before=next_event,
changed_events=[event],
event_changes=[EventChangeDTO(event, EventChangeType.MOVE)],
)

# Commit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
from sqlalchemy.ext.asyncio import AsyncSession

from fanfan.application.common.id_provider import IdProvider
from fanfan.application.events.common import (
from fanfan.application.schedule_mgmt.common import (
ANNOUNCEMENT_LOCK,
ANNOUNCEMENT_TIMESTAMP,
prepare_notifications,
)
from fanfan.application.schedule_mgmt.utils.prepare_notifications import (
EventChangeDTO,
EventChangeType,
PrepareNotifications,
)
from fanfan.core.exceptions.events import (
AnnounceTooFast,
Expand Down Expand Up @@ -38,6 +42,7 @@ def __init__(
self,
session: AsyncSession,
redis: Redis,
prepare_notifications: PrepareNotifications,
notifier: Notifier,
id_provider: IdProvider,
) -> None:
Expand Down Expand Up @@ -74,15 +79,18 @@ async def __call__(self, event_id: int | None) -> SetCurrentEventResult:
raise CurrentEventNotAllowed
event.current = True
await self.session.flush([event])
await self.session.refresh(event)
else:
event = None

# Prepare subscriptions
notifications = await prepare_notifications(
notifications = await self.prepare_notifications(
session=self.session,
next_event_before=next_event,
changed_events=[event],
event_changes=[
EventChangeDTO(event, EventChangeType.SET_AS_CURRENT)
if isinstance(event, Event)
else None
],
)

await self.session.commit()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from fanfan.application.schedule_mgmt.set_current_event import (
SetCurrentEvent,
SetCurrentEventResult,
)
from fanfan.core.exceptions.events import EventNotFound, NoNextEvent
from fanfan.infrastructure.db.models import Event
from fanfan.infrastructure.db.queries.events import next_event_query

from .set_current_event import SetCurrentEvent, SetCurrentEventResult


class SetNextEvent:
def __init__(self, session: AsyncSession, set_current_event: SetCurrentEvent):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from fanfan.application.events.common import (
from fanfan.application.schedule_mgmt.common import (
ANNOUNCEMENT_LOCK,
ANNOUNCEMENT_TIMESTAMP,
prepare_notifications,
)
from fanfan.application.schedule_mgmt.utils.prepare_notifications import (
EventChangeDTO,
EventChangeType,
PrepareNotifications,
)
from fanfan.core.exceptions.events import (
AnnounceTooFast,
Expand All @@ -36,11 +40,13 @@ def __init__(
self,
session: AsyncSession,
redis: Redis,
prepare_notifications: PrepareNotifications,
notifier: Notifier,
) -> None:
self.session = session
self.redis = redis
self.notifier = notifier
self.prepare_notifications = prepare_notifications

async def __call__(self, event_id: int) -> SkipEventResult:
async with self.session, self.redis.lock(ANNOUNCEMENT_LOCK, 10):
Expand All @@ -64,15 +70,16 @@ async def __call__(self, event_id: int) -> SkipEventResult:
next_event = await self.session.scalar(next_event_query())

# Toggle event skip
change_type = EventChangeType.UNSKIP if event.skip else EventChangeType.SKIP
event.skip = not event.skip
await self.session.flush([event])
await self.session.refresh(event)

# Prepare subscriptions
notifications = await prepare_notifications(
notifications = await self.prepare_notifications(
session=self.session,
next_event_before=next_event,
changed_events=[event],
event_changes=[EventChangeDTO(event, change_type)],
)

await self.session.commit()
Expand Down
Empty file.
Loading

0 comments on commit 115f922

Please sign in to comment.