Przeglądaj źródła

feat: refactor assert_project_exists

clovis 1 tydzień temu
rodzic
commit
40dd657808

+ 2 - 2
app/api/endpoints/project.py

@@ -7,7 +7,7 @@ from sqlalchemy.exc import IntegrityError
 from sqlalchemy.orm import Session
 
 from app.api import deps
-from app.api.utils import assert_project_exists, get_project_or_404
+from app.api.utils import assert_project_exists_or_404, get_project_or_404
 from app.importData.gsheet import ParsingError, extract_doc_uid, parseGsheet
 from app.models import (
     GlobalRole,
@@ -233,7 +233,7 @@ async def create_sms_batch(
     session: Session = Depends(deps.get_session),
 ):
     """(docstring unchanged)"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     slots = session.execute(select(Slot).where(Slot.project_id == project_id))
     sms_list = []
     now = datetime.now(UTC)

+ 3 - 3
app/api/endpoints/slots.py

@@ -5,7 +5,7 @@ from sqlalchemy import delete, select
 from sqlalchemy.orm import Session
 
 from app.api import deps
-from app.api.utils import assert_project_exists, update_object_from_payload, verify_id_list
+from app.api.utils import assert_project_exists_or_404, update_object_from_payload, verify_id_list
 from app.models import (
     Slot,
     SlotTemplate,
@@ -42,7 +42,7 @@ async def list_project_slots(
     session: Session = Depends(deps.get_session),
 ):
     """List slots from project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     results = session.execute(select(Slot).where(Slot.project_id == project_id))
     return results.scalars().all()
 
@@ -55,7 +55,7 @@ async def create_slot(
     session: Session = Depends(deps.get_session),
 ):
     """Create a new slot to the project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     if new_slot.template_id:
         assert_template_from_commission(session, current_user, project_id, new_slot.template_id)
 

+ 3 - 3
app/api/endpoints/sms.py

@@ -5,7 +5,7 @@ from sqlalchemy import delete, select
 from sqlalchemy.orm import Session
 
 from app.api import deps
-from app.api.utils import assert_project_exists, update_object_from_payload
+from app.api.utils import assert_project_exists_or_404, update_object_from_payload
 from app.models import (
     Sms,
     User,
@@ -23,7 +23,7 @@ async def list_project_sms(
     session: Session = Depends(deps.get_session),
 ):
     """List sms from project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     results = session.execute(select(Sms).where(Sms.project_id == project_id))
     return results.scalars().all()
 
@@ -36,7 +36,7 @@ async def create_sms(
     session: Session = Depends(deps.get_session),
 ):
     """Create a new to the project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     sms = Sms(project_id=project_id, **new_sms.model_dump())
     session.add(sms)
     session.commit()

+ 2 - 2
app/api/endpoints/templates.py

@@ -5,7 +5,7 @@ from sqlalchemy import delete, select
 from sqlalchemy.orm import Session
 
 from app.api import deps
-from app.api.utils import assert_project_exists, update_object_from_payload, verify_id_list
+from app.api.utils import assert_project_exists_or_404, update_object_from_payload, verify_id_list
 from app.models import (
     Project,
     SlotTag,
@@ -40,7 +40,7 @@ async def create_template(
     session: Session = Depends(deps.get_session),
 ):
     """Create a new template to the project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     if payload.commission_id is not None:
         deps.assert_commission_ownership(session, current_user, project_id, payload.commission_id)
     template = SlotTemplate(project_id=project_id, title=payload.title)

+ 6 - 3
app/api/endpoints/volunteer_groups.py

@@ -5,7 +5,10 @@ from sqlalchemy import select
 from sqlalchemy.orm import Session
 
 from app.api import deps
-from app.api.utils import assert_project_exists, verify_id_list  # adjust import path if different
+from app.api.utils import (  # adjust import path if different
+    assert_project_exists_or_404,
+    verify_id_list,
+)
 from app.models import OrgRole, Slot, Sms, User, Volunteer, VolunteerGroup
 from app.schemas.requests import (
     GroupMembershipRequest,
@@ -34,7 +37,7 @@ async def list_project_groups(
     session: Session = Depends(deps.get_session),
 ):
     """List volunteer groups from project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     results = session.execute(select(VolunteerGroup).where(VolunteerGroup.project_id == project_id))
     return results.scalars().all()
 
@@ -47,7 +50,7 @@ async def create_group(
     session: Session = Depends(deps.get_session),
 ):
     """Create a new volunteer group in the project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     group = VolunteerGroup(project_id=project_id, **new_group.model_dump())
     session.add(group)
     session.commit()

+ 3 - 3
app/api/endpoints/volunteers.py

@@ -5,7 +5,7 @@ from sqlalchemy import delete, select
 from sqlalchemy.orm import Session
 
 from app.api import deps
-from app.api.utils import assert_project_exists, update_object_from_payload, verify_id_list
+from app.api.utils import assert_project_exists_or_404, update_object_from_payload, verify_id_list
 from app.models import Slot, User, Volunteer, association_table_volunteer_slot
 from app.schemas.requests import VolunteerCreateRequest, VolunteerUpdateRequest
 from app.schemas.responses import VolunteerResponse
@@ -20,7 +20,7 @@ async def list_project_volunteers(
     session: Session = Depends(deps.get_session),
 ):
     """List volunteers from project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     results = session.execute(select(Volunteer).where(Volunteer.project_id == project_id))
     return results.scalars().all()
 
@@ -33,7 +33,7 @@ async def create_volunteer(
     session: Session = Depends(deps.get_session),
 ):
     """Create a new volunteer to the project"""
-    assert_project_exists(session, project_id)
+    assert_project_exists_or_404(session, project_id)
     input_dict = new_volunteer.model_dump()
 
     # Extract slots list from input dict

+ 12 - 12
app/api/utils.py

@@ -8,18 +8,7 @@ from sqlalchemy.sql import func
 from app.models import Project
 
 
-def get_project_organization_id(session: Session, project_id: UUID) -> str:
-    """Fetches only Project.organization_id -- avoids loading the full
-    Project row (and its relationships) just to check existence/ownership."""
-    org_id = session.execute(
-        select(Project.organization_id).where(Project.id == project_id)
-    ).scalar_one_or_none()
-    if org_id is None:
-        raise HTTPException(status_code=404, detail="Project not found")
-    return org_id
-
-
-def assert_project_exists(session: Session, project_id: UUID) -> None:
+def assert_project_exists_or_404(session: Session, project_id: UUID) -> None:
     if not session.execute(select(exists().where(Project.id == project_id))).scalar():
         raise HTTPException(status_code=404, detail="Project not found")
 
@@ -31,6 +20,17 @@ def get_project_or_404(session: Session, project_id: UUID) -> Project:
     return p
 
 
+def get_project_organization_id(session: Session, project_id: UUID) -> str:
+    """Fetches only Project.organization_id -- avoids loading the full
+    Project row (and its relationships) just to check existence/ownership."""
+    org_id = session.execute(
+        select(Project.organization_id).where(Project.id == project_id)
+    ).scalar_one_or_none()
+    if org_id is None:
+        raise HTTPException(status_code=404, detail="Project not found")
+    return org_id
+
+
 async def verify_id_list(
     session: Session,
     id_list: list[UUID],