Skip to content

Commit

Permalink
chore: optimize endpoint for event group .ics file update
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Oct 18, 2023
1 parent c4ebcde commit 871113c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/app/event_groups/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ async def list_event_groups(
**NoCredentialsException.responses,
},
status_code=201,
tags=["ICS"],
)
async def set_event_group_ics(
event_group_id: int,
Expand All @@ -190,12 +191,9 @@ async def set_event_group_ics(
content={"detail": f"File content type is {ics_file.content_type}, but should be 'text/calendar'"},
)

event_group = await event_group_repository.read(event_group_id)

if event_group is None:
raise EventGroupNotFoundException()
event_group_path = await event_group_repository.get_only_path(event_group_id)

if event_group.path is None:
if event_group_path is None:
raise EventGroupWithMissingPath()

# owners_and_moderators = {ownership.user_id for ownership in event_group.ownerships}
Expand All @@ -213,7 +211,7 @@ async def set_event_group_ics(

content = calendar.to_ical()

ics_path = PredefinedRepository.locate_ics_by_path(event_group.path)
ics_path = PredefinedRepository.locate_ics_by_path(event_group_path)

async with aiofiles.open(ics_path, "rb") as f:
old_content = await f.read()
Expand Down
6 changes: 5 additions & 1 deletion src/repositories/event_groups/abc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__all__ = ["AbstractEventGroupRepository"]

from abc import ABCMeta, abstractmethod
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

if TYPE_CHECKING:
from src.schemas import CreateEventGroup, ViewEventGroup, OwnershipEnum, UpdateEventGroup
Expand Down Expand Up @@ -47,6 +47,10 @@ async def batch_update(self, event_groups: dict[int, "UpdateEventGroup"]) -> lis

# ^^^^^^^^^^^^^^^^^ CRUD ^^^^^^^^^^^^^^^^^^^^^^^^ #

@abstractmethod
async def get_only_path(self, group_id: int) -> Optional[str]:
...

@abstractmethod
async def setup_ownership(self, group_id: int, user_id: int, role_alias: "OwnershipEnum") -> None:
...
Expand Down
17 changes: 15 additions & 2 deletions src/repositories/event_groups/repository.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
__all__ = ["SqlEventGroupRepository"]

from typing import Optional

from sqlalchemy import select
from sqlalchemy.dialects.postgresql import insert as postgres_insert
from sqlalchemy.ext.asyncio import AsyncSession

from src.repositories.crud import CRUDFactory
from src.repositories.crud import CRUDFactory, AbstractCRUDRepository
from src.repositories.event_groups.abc import AbstractEventGroupRepository
from src.repositories.ownership import setup_ownership_method
from src.schemas import ViewEventGroup, CreateEventGroup, UpdateEventGroup, OwnershipEnum
from src.storages.sql import AbstractSQLAlchemyStorage
from src.storages.sql.models import UserXFavoriteEventGroup, EventGroup

CRUD = CRUDFactory(
CRUD: AbstractCRUDRepository[
CreateEventGroup,
ViewEventGroup,
UpdateEventGroup,
] = CRUDFactory(
EventGroup,
CreateEventGroup,
ViewEventGroup,
Expand Down Expand Up @@ -41,6 +48,12 @@ async def read(self, group_id: int) -> ViewEventGroup:
async with self._create_session() as session:
return await CRUD.read(session, id=group_id)

async def get_only_path(self, group_id: int) -> Optional[str]:
async with self._create_session() as session:
q = select(EventGroup.path).where(EventGroup.id == group_id).where(EventGroup.path.isnot(None))
path: Optional[str] = await session.scalar(q)
return path

async def read_all(self) -> list[ViewEventGroup]:
async with self._create_session() as session:
return await CRUD.read_all(session)
Expand Down

1 comment on commit 871113c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src
   exceptions.py35877%11, 28, 42, 56, 70, 84, 98, 112
   main.py272122%9–67
   utils.py70700%1–125
src/app
   dependencies.py47883%29, 33, 37, 41, 45, 49, 53, 57
   routers.py8362%10–14
src/app/auth
   common.py281739%12–21, 25–32, 36–44, 49–50
   dependencies.py19953%33–38, 44–47
   jwt.py513433%26–28, 32–36, 40–44, 48–53, 57–70, 74–81
src/app/event_groups
   routes.py704634%39–45, 65–76, 94–98, 116–120, 137–142, 157–158, 188–224
src/app/ics
   routes.py877316%19–187
src/app/root
   __init__.py440%1–8
   routes.py13130%1–48
src/app/tags
   routes.py10280%24–25
src/app/users
   routes.py251348%32–34, 54–59, 78–80, 102–107
src/app/workshops
   __init__.py550%1–9
   routes.py16160%1–63
src/repositories
   crud.py946234%82, 87–92, 95–101, 104–111, 116–121, 124–137, 140–144, 149–157, 161–166, 171–180, 183–185
   ownership.py13746%13–29
src/repositories/event_groups
   repository.py653645%32, 35, 40–41, 44–45, 48–49, 52–55, 58–59, 62–63, 66–67, 70–71, 74–75, 78–81, 88–90, 93–103, 106–123
src/repositories/events
   __init__.py330%1–7
   abc.py440%1–10
   repository.py50500%1–87
src/repositories/predefined
   repository.py1438739%24–29, 48–70, 83–89, 105–109, 113–124, 127, 130, 133, 136, 140, 144–155, 159–168, 172–202
src/repositories/tags
   repository.py764837%22, 25, 29–33, 36–52, 55–56, 59–60, 63–64, 67–68, 71–72, 75–76, 81–83, 86–94, 97–118, 121–125
src/repositories/users
   repository.py956433%20, 36–44, 51, 54, 58–63, 66–80, 83–95, 98–99, 102–104, 107–108, 113–133, 136–150, 153–177
src/repositories/workshops
   __init__.py330%1–4
   abc.py440%1–10
   repository.py34340%1–68
src/schemas
   event_groups.py45491%27, 45–46, 86
   events.py711677%47–57, 77–79, 98–100
   ownership.py16194%21
   tags.py39977%19–21, 35–37, 51–53
   users.py24388%30–32
   workshops.py58580%1–85
src/storages/sql
   storage.py231057%26–27, 31–34, 37, 40–43, 46
TOTAL166284549% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 2.671s ⏱️

Please sign in to comment.