Skip to content

Commit

Permalink
✨(edx) add entries deletion from student_courseenrollmentallowed
Browse files Browse the repository at this point in the history
Introduces functionality to manually query and delete entries from the
`student_courseenrollmentallowed` table, as it lacks foreign key relationships
with other tables.
  • Loading branch information
wilbrdt committed Oct 16, 2024
1 parent 21b21dd commit 0aac468
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/app/mork/edx/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from logging import getLogger
from typing import Optional

from sqlalchemy import distinct, select, union_all
from sqlalchemy import delete, distinct, select, union_all
from sqlalchemy.orm import Session, load_only
from sqlalchemy.sql.functions import count

Expand All @@ -18,6 +18,7 @@
CourseCreatorsCoursecreator,
)
from mork.edx.models.dark import DarkLangDarklangconfig
from mork.edx.models.student import StudentCourseenrollmentallowed
from mork.edx.models.util import UtilRatelimitconfiguration
from mork.edx.models.verify import VerifyStudentHistoricalverificationdeadline
from mork.exceptions import UserDeleteError, UserProtectedDeleteError
Expand Down Expand Up @@ -136,4 +137,14 @@ def delete_user(session: Session, email: str, username: str) -> None:
"User is linked to a protected table and cannot be deleted"
)

# Delete entries in student_courseenrollmentallowed table containing user email
session.execute(
delete(StudentCourseenrollmentallowed).where(
StudentCourseenrollmentallowed.email == email
)
)

# Delete user from auth_user table and all its children
session.delete(user_to_delete)

logger.info(f"Deleting user {username=} {email=}")
11 changes: 8 additions & 3 deletions src/app/mork/tests/edx/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
EdxCourseCreatorsCoursecreatorFactory,
)
from mork.edx.factories.dark import EdxDarkLangDarklangconfigFactory
from mork.edx.factories.student import (
EdxStudentCourseenrollmentallowedFactory,
)
from mork.edx.factories.util import EdxUtilRatelimitconfigurationFactory
from mork.edx.factories.verify import (
EdxVerifyStudentHistoricalverificationdeadlineFactory,
Expand Down Expand Up @@ -155,9 +158,10 @@ def test_edx_crud_delete_user_missing(edx_db):

def test_edx_crud_delete_user(edx_db):
"""Test the `delete_user` method."""
EdxAuthUserFactory.create_batch(
1, email="[email protected]", username="john_doe"
)
email = "[email protected]"
username = "john_doe"
EdxAuthUserFactory.create_batch(1, email=email, username=username)
EdxStudentCourseenrollmentallowedFactory.create_batch(3, email=email)

# Get all related tables that have foreign key constraints on the parent table
related_tables = [
Expand All @@ -183,6 +187,7 @@ def test_edx_crud_delete_user(edx_db):
"student_anonymoususerid",
"student_courseaccessrole",
"student_courseenrollment",
"student_courseenrollmentallowed",
"student_courseenrollmentattribute",
"student_historicalcourseenrollment",
"student_languageproficiency",
Expand Down

0 comments on commit 0aac468

Please sign in to comment.