Skip to content

Commit

Permalink
Fix/raid registering drive (#559)
Browse files Browse the repository at this point in the history
### Description

Please explain the changes you made here.

### Checklist

- [ ] Created tests which fail without the change (if possible)
- [ ] All tests passing
- [ ] Extended the documentation, if necessary

---------

Co-authored-by: Maxime Roucher <[email protected]>
  • Loading branch information
armanddidierjean and maximeroucher authored Sep 8, 2024
1 parent 08b98d0 commit 3c846f4
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 160 deletions.
16 changes: 15 additions & 1 deletion app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
from app.core.groups.groups_type import GroupType
from app.core.log import LogConfig
from app.dependencies import (
get_db,
get_redis_client,
get_websocket_connection_manager,
init_and_get_db_engine,
)
from app.modules.module_list import module_list
from app.types.exceptions import ContentHTTPException
from app.types.exceptions import ContentHTTPException, GoogleAPIInvalidCredentialsError
from app.types.sqlalchemy import Base
from app.utils import initialization
from app.utils.google_api.google_api import GoogleAPI
from app.utils.redis import limiter

if TYPE_CHECKING:
Expand Down Expand Up @@ -298,6 +300,18 @@ def get_application(settings: Settings, drop_db: bool = False) -> FastAPI:
# https://fastapi.tiangolo.com/advanced/events/
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator:
# Init Google API credentials
async for db in app.dependency_overrides.get(
get_db,
get_db,
)():
google_api = GoogleAPI()
try:
await google_api.get_credentials(db, settings)
except GoogleAPIInvalidCredentialsError:
# We expect this error to be raised if the credentials were never set before
pass

ws_manager = app.dependency_overrides.get(
get_websocket_connection_manager,
get_websocket_connection_manager,
Expand Down
6 changes: 2 additions & 4 deletions app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,14 @@ def get_notification_tool(
)


def get_drive_file_manager(
settings: Settings = Depends(get_settings),
) -> DriveFileManager:
def get_drive_file_manager() -> DriveFileManager:
"""
Dependency that returns the drive file manager.
"""
global drive_file_manage

if drive_file_manage is None:
drive_file_manage = DriveFileManager(settings=settings)
drive_file_manage = DriveFileManager()

return drive_file_manage

Expand Down
119 changes: 98 additions & 21 deletions app/modules/raid/endpoints_raid.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)
from app.types.content_type import ContentType
from app.types.module import Module
from app.utils.google_api.google_api import DriveGoogleAPI
from app.utils.tools import (
get_core_data,
get_file_from_data,
Expand Down Expand Up @@ -121,6 +122,7 @@ async def update_participant(
user: models_core.CoreUser = Depends(is_user),
db: AsyncSession = Depends(get_db),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Update a participant
Expand Down Expand Up @@ -213,7 +215,12 @@ async def update_participant(

await cruds_raid.update_participant(participant_id, participant, is_minor, db)
team = await cruds_raid.get_team_by_participant_id(participant_id, db)
await post_update_actions(team, db, drive_file_manager)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)


@module.router.post(
Expand All @@ -226,6 +233,7 @@ async def create_team(
user: models_core.CoreUser = Depends(is_user),
db: AsyncSession = Depends(get_db),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Create a team
Expand All @@ -248,7 +256,12 @@ async def create_team(
await cruds_raid.create_team(db_team, db)
# We need to get the team from the db to have access to relationships
created_team = await cruds_raid.get_team_by_id(team_id=db_team.id, db=db)
await post_update_actions(created_team, db, drive_file_manager)
await post_update_actions(
created_team,
db,
drive_file_manager,
settings=settings,
)
return created_team


Expand Down Expand Up @@ -320,6 +333,7 @@ async def update_team(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Update a team
Expand All @@ -331,7 +345,12 @@ async def update_team(
raise HTTPException(status_code=403, detail="You can only edit your own team.")
await cruds_raid.update_team(team_id, team, db)
updated_team = await cruds_raid.get_team_by_id(team_id, db)
await post_update_actions(updated_team, db, drive_file_manager)
await post_update_actions(
updated_team,
db,
drive_file_manager,
settings=settings,
)


@module.router.delete(
Expand All @@ -342,7 +361,7 @@ async def delete_team(
team_id: str,
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Delete a team
Expand All @@ -354,11 +373,12 @@ async def delete_team(
await cruds_raid.delete_team(team_id, db)
# We will try to delete PDF associated with the team from the Google Drive
if team.file_id:
drive_file_manager.delete_file(team.file_id)
if team.captain.security_file.file_id:
drive_file_manager.delete_file(team.captain.security_file.file_id)
if team.second and team.second.security_file.file_id:
drive_file_manager.delete_file(team.second.security_file.file_id)
async with DriveGoogleAPI(db, settings) as google_api:
google_api.delete_file(team.file_id)
if team.captain.security_file.file_id:
google_api.delete_file(team.captain.security_file.file_id)
if team.second and team.second.security_file.file_id:
google_api.delete_file(team.second.security_file.file_id)


@module.router.delete(
Expand Down Expand Up @@ -493,13 +513,19 @@ async def validate_document(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Validate a document
"""
await cruds_raid.update_document_validation(document_id, validation, db)
team = await cruds_raid.get_team_by_participant_id(user.id, db)
await post_update_actions(team, db, drive_file_manager)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)


@module.router.post(
Expand All @@ -513,6 +539,7 @@ async def set_security_file(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Confirm security file
Expand Down Expand Up @@ -552,8 +579,14 @@ async def set_security_file(
team.number,
db,
drive_file_manager,
settings,
)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)
await post_update_actions(team, db, drive_file_manager)
return created_security_file


Expand All @@ -566,13 +599,22 @@ async def confirm_payment(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Confirm payment manually
"""
await cruds_raid.confirm_payment(participant_id, db)
team = await cruds_raid.get_team_by_participant_id(participant_id, db)
await post_update_actions(team, db, drive_file_manager)
team = await cruds_raid.get_team_by_participant_id(
participant_id,
db,
)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)


@module.router.post(
Expand All @@ -584,6 +626,7 @@ async def confirm_t_shirt_payment(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Confirm T shirt payment
Expand All @@ -597,7 +640,12 @@ async def confirm_t_shirt_payment(
raise HTTPException(status_code=400, detail="T shirt size not set.")
await cruds_raid.confirm_t_shirt_payment(participant_id, db)
team = await cruds_raid.get_team_by_participant_id(participant_id, db)
await post_update_actions(team, db, drive_file_manager)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)


@module.router.post(
Expand All @@ -609,6 +657,7 @@ async def validate_attestation_on_honour(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Validate attestation on honour
Expand All @@ -617,7 +666,12 @@ async def validate_attestation_on_honour(
raise HTTPException(status_code=403, detail="You are not the participant")
await cruds_raid.validate_attestation_on_honour(participant_id, db)
team = await cruds_raid.get_team_by_participant_id(participant_id, db)
await post_update_actions(team, db, drive_file_manager)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)


@module.router.post(
Expand Down Expand Up @@ -664,6 +718,7 @@ async def join_team(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Join a team
Expand All @@ -682,7 +737,8 @@ async def join_team(
raise HTTPException(status_code=403, detail="You are already in a team.")

if user_team.file_id:
drive_file_manager.delete_file(user_team.file_id)
async with DriveGoogleAPI(db, settings) as google_api:
google_api.delete_file(user_team.file_id)
await cruds_raid.delete_team(user_team.id, db)

team = await cruds_raid.get_team_by_id(invite_token.team_id, db)
Expand All @@ -700,7 +756,12 @@ async def join_team(
)

await cruds_raid.update_team_second_id(team.id, user.id, db)
await post_update_actions(team, db, drive_file_manager)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)
await cruds_raid.delete_invite_token(invite_token.id, db)


Expand All @@ -715,6 +776,7 @@ async def kick_team_member(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Leave a team
Expand All @@ -736,7 +798,12 @@ async def kick_team_member(
elif team.second_id != participant_id:
raise HTTPException(status_code=404, detail="Participant not found.")
await cruds_raid.update_team_second_id(team_id, None, db)
await post_update_actions(team, db, drive_file_manager)
await post_update_actions(
team,
db,
drive_file_manager,
settings=settings,
)
return await cruds_raid.get_team_by_id(team_id, db)


Expand All @@ -751,6 +818,7 @@ async def merge_teams(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Merge two teams
Expand Down Expand Up @@ -785,8 +853,14 @@ async def merge_teams(
await cruds_raid.update_team_second_id(team1_id, team2.captain_id, db)
await cruds_raid.delete_team(team2_id, db)
if team2.file_id:
drive_file_manager.delete_file(team2.file_id)
await post_update_actions(team1, db, drive_file_manager)
async with DriveGoogleAPI(db, settings) as google_api:
google_api.delete_file(team2.file_id)
await post_update_actions(
team1,
db,
drive_file_manager,
settings=settings,
)
return await cruds_raid.get_team_by_id(team1_id, db)


Expand Down Expand Up @@ -814,6 +888,7 @@ async def update_raid_information(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Update raid information
Expand Down Expand Up @@ -863,6 +938,7 @@ async def update_raid_information(
team.number,
db,
drive_file_manager,
settings,
)


Expand All @@ -875,6 +951,7 @@ async def update_drive_folders(
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.raid_admin)),
drive_file_manager: DriveFileManager = Depends(get_drive_file_manager),
settings: Settings = Depends(get_settings),
):
"""
Update drive folders
Expand All @@ -886,7 +963,7 @@ async def update_drive_folders(
security_folder_id=None,
)
await set_core_data(schemas_folders, db)
await drive_file_manager.init_folders(db=db)
await drive_file_manager.init_folders(db=db, settings=settings)


@module.router.get(
Expand Down
Loading

0 comments on commit 3c846f4

Please sign in to comment.