Skip to content

Commit

Permalink
Backfill LMSUser.lms_api_user_id from the assignment roster data
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospri committed Dec 3, 2024
1 parent f9e9f0f commit 226d707
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lms/services/roster.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from logging import getLogger

from sqlalchemy import Select, select, update
from sqlalchemy import Select, func, select, text, update

from lms.models import (
ApplicationInstance,
Expand Down Expand Up @@ -251,6 +251,11 @@ def _get_roster_users(self, roster, tool_consumer_instance_guid):
for member in roster:
lti_user_id = member.get("lti11_legacy_user_id") or member["user_id"]
lti_v13_user_id = member["user_id"]
lms_api_user_id = (
member.get("message", [{}])[0]
.get("https://purl.imsglobal.org/spec/lti/claim/custom", {})
.get("canvas_user_id")
)
name = display_name(
given_name=member.get("name", ""),
family_name=member.get("family_name", ""),
Expand All @@ -270,6 +275,7 @@ def _get_roster_users(self, roster, tool_consumer_instance_guid):
"lti_v13_user_id": lti_v13_user_id,
"h_userid": h_userid,
"display_name": name,
"lms_api_user_id": lms_api_user_id,
}
)

Expand All @@ -282,6 +288,14 @@ def _get_roster_users(self, roster, tool_consumer_instance_guid):
"updated",
# lti_v13_user_id is not going to change but we want to backfill it for existing users.
"lti_v13_user_id",
# Same for lms_api_user_id, not going to change for existing user but we are backfilling for now
(
"lms_api_user_id",
func.coalesce(
text('"excluded"."lms_api_user_id"'),
text('"lms_user"."lms_api_user_id"'),
),
),
],
)

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/lms/services/roster_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,39 @@ def test_fetch_assignment_roster(
assert roster[3].lms_user.lti_user_id == "USER_ID_INACTIVE"
assert not roster[3].active

def test_fetch_assignment_roster_with_canvas_user_id(
self, svc, lti_names_roles_service, db_session, lti_role_service, assignment
):
lti_names_roles_service.get_context_memberships.return_value = [
{
"user_id": "USER_ID",
"roles": ["ROLE1"],
"status": "Active",
"message": [
{
"https://purl.imsglobal.org/spec/lti/claim/custom": {
"canvas_user_id": "API_ID"
}
}
],
},
]
lti_role_service.get_roles.return_value = [
factories.LTIRole(value="ROLE1"),
]

svc.fetch_assignment_roster(assignment)

roster = db_session.scalars(
select(AssignmentRoster)
.order_by(AssignmentRoster.lms_user_id)
.where(AssignmentRoster.assignment_id == assignment.id)
).all()

assert roster[0].assignment_id == assignment.id
assert roster[0].lms_user.lti_user_id == "USER_ID"
assert roster[0].lms_user.lms_api_user_id == "API_ID"

@pytest.mark.parametrize(
"known_error",
[
Expand Down

0 comments on commit 226d707

Please sign in to comment.