-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
While we'll only get this information from some Canvas installs the new table and service code are LMS agnostic. We'll store the Canvas data for a while before exploring how to integrate other LMSes
- Loading branch information
Showing
7 changed files
with
186 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from lms.models import LMSTerm, LTIParams | ||
from lms.services.upsert import bulk_upsert | ||
|
||
|
||
class LMSTermService: | ||
def __init__(self, db): | ||
self._db = db | ||
|
||
def get_term(self, lti_params: LTIParams) -> LMSTerm | None: | ||
term_starts_at = lti_params.get_datetime("custom_term_start") | ||
term_ends_at = lti_params.get_datetime("custom_term_end") | ||
term_id = lti_params.get("custom_term_id") | ||
term_name = lti_params.get("custom_term_name") | ||
guid = lti_params["tool_consumer_instance_guid"] | ||
|
||
if not any([term_starts_at, term_ends_at]): | ||
# We need to have at least one date to consider a term. | ||
return None | ||
|
||
if term_id: | ||
# If we get an ID from the LMS we'll use it as the key. | ||
# We'll scope it the installs GUID | ||
key = f"{guid}:{term_id}" | ||
else: | ||
# Otherwise we'll use the name and dates as part of the key | ||
key = f"{guid}:{term_name if term_name else '-'}:{term_starts_at if term_starts_at else '-'}:{term_ends_at if term_ends_at else '-'}" | ||
|
||
values = [ | ||
{ | ||
"name": term_name, | ||
"tool_consumer_instance_guid": guid, | ||
"starts_at": term_starts_at, | ||
"ends_at": term_ends_at, | ||
"key": key, | ||
"lms_id": term_id, | ||
} | ||
] | ||
return bulk_upsert( | ||
self._db, | ||
model_class=LMSTerm, | ||
values=values, | ||
index_elements=["key"], | ||
update_columns=["updated", "name", "starts_at", "ends_at"], | ||
).first() | ||
|
||
|
||
def factory(_context, request): | ||
return LMSTermService(db=request.db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from datetime import datetime | ||
from unittest.mock import sentinel | ||
|
||
import pytest | ||
|
||
from lms.services.lms_term import LMSTermService, factory | ||
|
||
|
||
class TestLMSTermService: | ||
def test_get_term_not_enought_data(self, svc, pyramid_request): | ||
assert not svc.get_term(pyramid_request.lti_params) | ||
|
||
def test_get_term(self, svc, pyramid_request): | ||
term_starts = datetime(2020, 1, 1) | ||
term_ends = datetime(2020, 6, 1) | ||
term_name = "NICE TERM" | ||
lti_params = pyramid_request.lti_params | ||
lti_params["custom_term_start"] = term_starts.isoformat() | ||
lti_params["custom_term_end"] = term_ends.isoformat() | ||
lti_params["custom_term_name"] = term_name | ||
|
||
term = svc.get_term(pyramid_request.lti_params) | ||
|
||
assert term.starts_at == term_starts | ||
assert term.ends_at == term_ends | ||
assert term.name == term_name | ||
assert ( | ||
term.tool_consumer_instance_guid | ||
== lti_params["tool_consumer_instance_guid"] | ||
) | ||
|
||
@pytest.mark.parametrize( | ||
"name,start,end,term_id,expected", | ||
[ | ||
( | ||
"NICE TERM", | ||
"2020-01-01 00:00:00", | ||
"2020-06-01 00:00:00", | ||
None, | ||
"TEST_TOOL_CONSUMER_INSTANCE_GUID:NICE TERM:2020-01-01 00:00:00:2020-06-01 00:00:00", | ||
), | ||
( | ||
"NICE TERM", | ||
None, | ||
"2020-06-01 00:00:00", | ||
None, | ||
"TEST_TOOL_CONSUMER_INSTANCE_GUID:NICE TERM:-:2020-06-01 00:00:00", | ||
), | ||
( | ||
"NICE TERM", | ||
"2020-01-01 00:00:00", | ||
None, | ||
None, | ||
"TEST_TOOL_CONSUMER_INSTANCE_GUID:NICE TERM:2020-01-01 00:00:00:-", | ||
), | ||
( | ||
"NICE TERM", | ||
"2020-01-01 00:00:00", | ||
"2020-06-01 00:00:00", | ||
"TERM_ID", | ||
"TEST_TOOL_CONSUMER_INSTANCE_GUID:TERM_ID", | ||
), | ||
], | ||
) | ||
def test_get_term_key( | ||
self, svc, pyramid_request, name, start, end, term_id, expected | ||
): | ||
lti_params = pyramid_request.lti_params | ||
lti_params["custom_term_start"] = start | ||
lti_params["custom_term_end"] = end | ||
lti_params["custom_term_name"] = name | ||
lti_params["custom_term_id"] = term_id | ||
|
||
term = svc.get_term(pyramid_request.lti_params) | ||
|
||
assert term.key == expected | ||
|
||
@pytest.fixture() | ||
def svc(self, pyramid_request): | ||
return LMSTermService(db=pyramid_request.db) | ||
|
||
|
||
class TestFactory: | ||
def test_it(self, pyramid_request, LMSTermService): | ||
service = factory(sentinel.context, pyramid_request) | ||
|
||
LMSTermService.assert_called_once_with(db=pyramid_request.db) | ||
assert service == LMSTermService.return_value | ||
|
||
@pytest.fixture | ||
def LMSTermService(self, patch): | ||
return patch("lms.services.lms_term.LMSTermService") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters