-
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.
Basic API view and config to create canvas assignments
- Loading branch information
Showing
10 changed files
with
229 additions
and
1 deletion.
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
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,50 @@ | ||
from pyramid.view import view_config, view_defaults | ||
|
||
from lms.security import Permissions | ||
from lms.validation import APICanvasCreateAssignment | ||
|
||
|
||
@view_defaults(request_method="POST", renderer="json", permission=Permissions.API) | ||
class AssignmentsAPIViews: | ||
def __init__(self, request): | ||
self.request = request | ||
self.assignment_service = request.find_service(name="assignment") | ||
self.application_instance = request.find_service( | ||
name="application_instance" | ||
).get() | ||
|
||
@view_config( | ||
route_name="canvas_api.assignments.create", | ||
request_method="POST", | ||
schema=APICanvasCreateAssignment, | ||
) | ||
def create(self): | ||
""" | ||
Create an assignment in the DB. | ||
Note that at this point the assignment.resource_link_id == None, it will be filled on first launch. | ||
""" | ||
params = self.request.parsed_params | ||
content = params["content"] | ||
content_type = content["type"] | ||
|
||
url = None | ||
extra = {} | ||
if content_type == "url": | ||
url = content["url"] | ||
elif content_type == "file": | ||
url = f"canvas://file/course/{params['course_id']}/file_id/{params['content']['file']['id']}" | ||
extra = {"canvas_file": params["content"]["file"]} | ||
else: | ||
raise ValueError("Unhandled content type on assignment") | ||
|
||
if groupset := params.get("groupset"): | ||
extra["canvas_groupset"] = groupset | ||
|
||
assignment = self.assignment_service.set_document_url( | ||
self.application_instance.tool_consumer_instance_guid, | ||
url, | ||
ext_lti_assignment_id=params["ext_lti_assignment_id"], | ||
extra=extra, | ||
) | ||
return {"ext_lti_assignment_id": assignment.ext_lti_assignment_id} |
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,99 @@ | ||
import pytest | ||
|
||
from lms.views.api.canvas.assignments import AssignmentsAPIViews | ||
|
||
|
||
class TestAssignmentsAPIViews: | ||
@pytest.mark.parametrize( | ||
"request_body,expected_url,expected_extra", | ||
[ | ||
( | ||
{ | ||
"content": {"type": "url", "url": "https://example.com"}, | ||
"ext_lti_assignment_id": "EXT_LTI_ASSIGNMENT_ID", | ||
}, | ||
"https://example.com", | ||
{}, | ||
), | ||
( | ||
{ | ||
"content": {"type": "url", "url": "https://example.com"}, | ||
"ext_lti_assignment_id": "EXT_LTI_ASSIGNMENT_ID", | ||
"groupset": 125, | ||
}, | ||
"https://example.com", | ||
{"canvas_groupset": 125}, | ||
), | ||
( | ||
{ | ||
"content": { | ||
"type": "url", | ||
"url": "https://drive.google.com/uc?id=DRIVE_ID&export=download", | ||
}, | ||
"ext_lti_assignment_id": "EXT_LTI_ASSIGNMENT_ID", | ||
}, | ||
"https://drive.google.com/uc?id=DRIVE_ID&export=download", | ||
{}, | ||
), | ||
( | ||
{ | ||
"content": { | ||
"type": "file", | ||
"file": { | ||
"size": 205792, | ||
"display_name": "A Third File.pdf", | ||
"id": 652, | ||
"updated_at": "2021-05-17T15:15:47Z", | ||
}, | ||
}, | ||
"ext_lti_assignment_id": "EXT_LTI_ASSIGNMENT_ID", | ||
"course_id": "COURSE_ID", | ||
}, | ||
"canvas://file/course/COURSE_ID/file_id/652", | ||
{ | ||
"canvas_file": { | ||
"size": 205792, | ||
"display_name": "A Third File.pdf", | ||
"id": 652, | ||
"updated_at": "2021-05-17T15:15:47Z", | ||
} | ||
}, | ||
), | ||
], | ||
) | ||
def test_create( | ||
self, | ||
application_instance_service, | ||
assignment_service, | ||
pyramid_request, | ||
request_body, | ||
expected_url, | ||
expected_extra, | ||
): | ||
pyramid_request.parsed_params = request_body | ||
|
||
result = AssignmentsAPIViews(pyramid_request).create() | ||
|
||
assignment_service.set_document_url.assert_called_once_with( | ||
application_instance_service.get.return_value.tool_consumer_instance_guid, | ||
expected_url, | ||
ext_lti_assignment_id="EXT_LTI_ASSIGNMENT_ID", | ||
extra=expected_extra, | ||
) | ||
assert result == { | ||
"ext_lti_assignment_id": assignment_service.set_document_url.return_value.ext_lti_assignment_id | ||
} | ||
|
||
@pytest.mark.usefixtures("application_instance_service", "assignment_service") | ||
def test_create_unknown_type( | ||
self, | ||
pyramid_request, | ||
): | ||
pyramid_request.parsed_params = { | ||
"content": {"type": "nothing"}, | ||
"ext_lti_assignment_id": "EXT_LTI_ASSIGNMENT_ID", | ||
"course_id": "COURSE_ID", | ||
} | ||
|
||
with pytest.raises(ValueError): | ||
AssignmentsAPIViews(pyramid_request).create() |
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