Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes remove user cascading on groups/subjects/... #250

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/src/subject/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
StudentSubject = Table(
"student_subject",
Base.metadata,
Column("uid", ForeignKey("website_user.uid"), primary_key=True),
Column("subject_id", ForeignKey("subject.id"), primary_key=True),
Column("uid", ForeignKey("website_user.uid", ondelete="CASCADE"), primary_key=True),
Column("subject_id", ForeignKey("subject.id", ondelete="CASCADE"), primary_key=True),
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

Is deze cascade nodig als je de gebruiker overal verwijdert voor de gebruiker zelf te verwijderen?

)

InstructorSubject = Table(
Expand Down
15 changes: 12 additions & 3 deletions backend/src/user/service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Sequence

from sqlalchemy import select
from sqlalchemy import select, delete
from sqlalchemy.ext.asyncio import AsyncSession

from .models import User
from .schemas import UserCreate
from ..subject.models import InstructorSubject
from ..subject.models import InstructorSubject, StudentSubject, Subject
from ..group.models import StudentGroup
from ..project.models import InstructorProject


async def get_by_id(db: AsyncSession, user_id: str) -> User:
Expand Down Expand Up @@ -39,7 +41,14 @@ async def set_teacher(db: AsyncSession, user_id: str, value: bool):

async def delete_user(db: AsyncSession, user_id: str):
user = await get_by_id(db, user_id)
await db.delete(user)
if user:
await db.execute(delete(StudentSubject).where(StudentSubject.c.uid == user_id))
await db.execute(delete(InstructorProject).where(InstructorProject.c.uid == user_id))
await db.execute(delete(StudentGroup).where(StudentGroup.c.uid == user_id))
await db.flush()

await db.delete(user)
await db.commit()


async def get_instructors(db: AsyncSession) -> Sequence[User]:
Expand Down
Loading