-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial version of LineItem Implementation
Signed-off-by: Giovanni Cimolin da Silva <[email protected]>
- Loading branch information
1 parent
955d229
commit 11fd22d
Showing
24 changed files
with
969 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
LTI Advantage Assignments and Grades service implementation | ||
""" | ||
|
||
|
||
class LtiAgs: | ||
""" | ||
LTI Advantage Consumer | ||
Implements LTI Advantage Services and ties them in | ||
with the LTI Consumer. This only handles the LTI | ||
message claim inclusion and token handling. | ||
Available services: | ||
* Assignments and Grades services (partial support) | ||
Reference: https://www.imsglobal.org/lti-advantage-overview | ||
""" | ||
def __init__( | ||
self, | ||
lineitems_url, | ||
allow_creating_lineitems=True, | ||
results_service_enabled=True, | ||
scores_service_enabled=True | ||
): | ||
""" | ||
Instance class with LTI AGS Global settings. | ||
""" | ||
# If the platform allows creating lineitems, set this | ||
# to True. | ||
self.allow_creating_lineitems = allow_creating_lineitems | ||
|
||
# Result and scores services | ||
self.results_service_enabled = results_service_enabled | ||
self.scores_service_enabled = scores_service_enabled | ||
|
||
# Lineitems urls | ||
self.lineitems_url = lineitems_url | ||
|
||
def get_available_scopes(self): | ||
""" | ||
Retrieves list of available token scopes in this instance. | ||
""" | ||
scopes = [] | ||
|
||
if self.allow_creating_lineitems: | ||
scopes.append('https://purl.imsglobal.org/spec/lti-ags/scope/lineitem') | ||
else: | ||
scopes.append('https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly') | ||
|
||
if self.results_service_enabled: | ||
scopes.append('https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly') | ||
|
||
if self.scores_service_enabled: | ||
scopes.append('https://purl.imsglobal.org/spec/lti-ags/scope/score') | ||
|
||
return scopes | ||
|
||
def get_lti_ags_launch_claim(self): | ||
""" | ||
Returns LTI AGS Claim to be injected in the LTI launch message. | ||
""" | ||
ags_claim = { | ||
"https://purl.imsglobal.org/spec/lti-ags/claim/endpoint": { | ||
"scope": self.get_available_scopes(), | ||
"lineitems": self.lineitems_url, | ||
} | ||
} | ||
|
||
return ags_claim |
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,16 @@ | ||
""" | ||
LTI 1.3 Django extensions - Content parsers | ||
Used by DRF views to render content in LTI APIs. | ||
""" | ||
|
||
from rest_framework import parsers | ||
|
||
|
||
class LineItemParser(parsers.JSONParser): | ||
""" | ||
Line Item Parser. | ||
It's the same as JSON parser, but uses a custom media_type. | ||
""" | ||
media_type = 'application/vnd.ims.lis.v2.lineitem+json' |
44 changes: 44 additions & 0 deletions
44
lti_consumer/lti_1p3/extensions/rest_framework/permissions.py
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,44 @@ | ||
""" | ||
Django REST Framework extensions for LTI 1.3 & LTI Advantage implementation. | ||
Implements a custom authorization classes to be used by any of the | ||
LTI Advantage extensions. | ||
""" | ||
from rest_framework import permissions | ||
|
||
|
||
class LtiAgsPermissions(permissions.BasePermission): | ||
""" | ||
LTI AGS Permissions. | ||
This checks if the token included in the request | ||
has the allowed scopes to read/write LTI AGS items | ||
(LineItems, Results, Score). | ||
LineItem scopes: https://www.imsglobal.org/spec/lti-ags/v2p0#scope-and-allowed-http-methods | ||
Results: Not implemented yet. | ||
Score: Not implemented yet. | ||
""" | ||
def has_permission(self, request, view): | ||
# Retrieves token from request, which was already checked by | ||
# the Authentication class, so we assume it's a sane value. | ||
auth_token = request.headers.get('Authorization', '').split()[1] | ||
|
||
if view.action in ['list', 'retrieve']: | ||
# We don't need to wrap this around a try-catch because | ||
# the token was already tested by the Authentication class. | ||
has_perm = request.lti_consumer.check_token( | ||
auth_token, | ||
[ | ||
'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly', | ||
'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem', | ||
], | ||
) | ||
return has_perm | ||
elif view.action in ['create', 'update', 'partial_update', 'delete']: | ||
has_perm = request.lti_consumer.check_token( | ||
auth_token, | ||
'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem' | ||
) | ||
return has_perm | ||
return False |
28 changes: 28 additions & 0 deletions
28
lti_consumer/lti_1p3/extensions/rest_framework/renderers.py
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,28 @@ | ||
""" | ||
LTI 1.3 Django extensions - Content renderers | ||
Used by DRF views to render content in LTI APIs. | ||
""" | ||
from rest_framework import renderers | ||
|
||
|
||
class LineItemsRenderer(renderers.JSONRenderer): | ||
""" | ||
Line Items Renderer. | ||
It's a JSON parser, but uses a custom media_type. | ||
Reference: https://www.imsglobal.org/spec/lti-ags/v2p0#media-types-and-schemas | ||
""" | ||
media_type = 'application/vnd.ims.lis.v2.lineitemcontainer+json' | ||
format = 'json' | ||
|
||
|
||
class LineItemRenderer(renderers.JSONRenderer): | ||
""" | ||
Line Item Renderer. | ||
It's a JSON parser, but uses a custom media_type. | ||
Reference: https://www.imsglobal.org/spec/lti-ags/v2p0#media-types-and-schemas | ||
""" | ||
media_type = 'application/vnd.ims.lis.v2.lineitem+json' | ||
format = 'json' |
Oops, something went wrong.