-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PADV-1191: Add DeepLinkingForm to DeepLinkingView. #35
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""Django Forms.""" | ||
from typing import List, Optional, Set, Tuple | ||
|
||
from django import forms | ||
from django.http.request import HttpRequest | ||
from django.urls import reverse | ||
from django.utils.translation import gettext as _ | ||
from pylti1p3.deep_link_resource import DeepLinkResource | ||
|
||
from openedx_lti_tool_plugin.apps import OpenEdxLtiToolPluginConfig as app_config | ||
from openedx_lti_tool_plugin.edxapp_wrapper.learning_sequences import course_context | ||
|
||
|
||
class DeepLinkingForm(forms.Form): | ||
"""Deep Linking Form.""" | ||
|
||
def __init__(self, *args: tuple, request=None, **kwargs: dict): | ||
"""Class __init__ method. | ||
|
||
Initialize class instance attributes and add `content_items` field. | ||
|
||
Args: | ||
*args: Variable length argument list. | ||
request: HttpRequest object. | ||
**kwargs: Arbitrary keyword arguments. | ||
|
||
""" | ||
super().__init__(*args, **kwargs) | ||
self.fields['content_items'] = forms.MultipleChoiceField( | ||
choices=self.get_content_items_choices(request), | ||
required=False, | ||
widget=forms.CheckboxSelectMultiple, | ||
label=_('Courses'), | ||
) | ||
|
||
def get_content_items_choices(self, request: HttpRequest) -> List[Tuple[str, str]]: | ||
"""Get `content_items` field choices. | ||
|
||
Args: | ||
request: HttpRequest object. | ||
|
||
Returns: | ||
List of tuples with choices for the `content_items` field. | ||
|
||
""" | ||
return [ | ||
self.get_content_items_choice(course, request) | ||
for course in course_context().objects.all() | ||
] | ||
|
||
def get_content_items_choice(self, course, request: HttpRequest) -> Tuple[str, str]: | ||
"""Get `content_items` field choice. | ||
|
||
Args: | ||
course (CourseContext): Course object. | ||
request: HttpRequest object. | ||
|
||
Returns: | ||
Tuple containing the choice value and name. | ||
|
||
.. _LTI Deep Linking Specification - LTI Resource Link: | ||
https://www.imsglobal.org/spec/lti-dl/v2p0#lti-resource-link | ||
|
||
""" | ||
relative_url = reverse( | ||
f'{app_config.name}:1.3:resource-link:launch-course', | ||
kwargs={'course_id': course.learning_context.context_key}, | ||
) | ||
|
||
return ( | ||
request.build_absolute_uri(relative_url), | ||
course.learning_context.title, | ||
) | ||
|
||
def get_deep_link_resources(self) -> Set[Optional[DeepLinkResource]]: | ||
"""Get DeepLinkResource objects from this form `cleaned_data` attribute. | ||
|
||
Returns: | ||
Set of DeepLinkResource objects or an empty set | ||
|
||
.. _LTI 1.3 Advantage Tool implementation in Python - LTI Message Launches: | ||
https://github.com/dmitry-viskov/pylti1.3?tab=readme-ov-file#deep-linking-responses | ||
|
||
""" | ||
return { | ||
DeepLinkResource().set_url(content_item) | ||
for content_item in self.cleaned_data.get('content_items', []) | ||
} |
106 changes: 106 additions & 0 deletions
106
openedx_lti_tool_plugin/deep_linking/tests/test_forms.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,106 @@ | ||
"""Tests forms module.""" | ||
from unittest.mock import MagicMock, patch | ||
|
||
from django import forms | ||
from django.test import TestCase | ||
|
||
from openedx_lti_tool_plugin.apps import OpenEdxLtiToolPluginConfig as app_config | ||
from openedx_lti_tool_plugin.deep_linking.forms import DeepLinkingForm | ||
from openedx_lti_tool_plugin.deep_linking.tests import MODULE_PATH | ||
|
||
MODULE_PATH = f'{MODULE_PATH}.forms' | ||
|
||
|
||
class TestDeepLinkingForm(TestCase): | ||
"""Test DeepLinkingForm class.""" | ||
|
||
def setUp(self): | ||
"""Set up test fixtures.""" | ||
super().setUp() | ||
self.form_class = DeepLinkingForm | ||
self.request = MagicMock() | ||
self.learning_context = MagicMock(context_key='random-course-key', title='Test') | ||
self.course = MagicMock(learning_context=self.learning_context) | ||
|
||
@patch(f'{MODULE_PATH}.forms.MultipleChoiceField') | ||
@patch(f'{MODULE_PATH}._') | ||
@patch.object(DeepLinkingForm, 'get_content_items_choices') | ||
def test_init( | ||
self, | ||
get_content_items_choices_mock: MagicMock, | ||
gettext_mock: MagicMock, | ||
multiple_choice_field_mock: MagicMock, | ||
): | ||
"""Test `__init__` method.""" | ||
self.assertEqual( | ||
self.form_class(request=self.request).fields, | ||
{'content_items': multiple_choice_field_mock.return_value}, | ||
) | ||
get_content_items_choices_mock.assert_called_once_with(self.request) | ||
gettext_mock.assert_called_once_with('Courses') | ||
multiple_choice_field_mock.assert_called_once_with( | ||
choices=get_content_items_choices_mock(), | ||
required=False, | ||
widget=forms.CheckboxSelectMultiple, | ||
label=gettext_mock(), | ||
) | ||
|
||
@patch(f'{MODULE_PATH}.course_context') | ||
@patch.object(DeepLinkingForm, 'get_content_items_choice') | ||
@patch.object(DeepLinkingForm, '__init__', return_value=None) | ||
def test_get_content_items_choices( | ||
self, | ||
deep_linking_form_init: MagicMock, # pylint: disable=unused-argument | ||
get_content_items_choice_mock: MagicMock, | ||
course_context_mock: MagicMock, | ||
): | ||
"""Test `get_content_items_choices` method.""" | ||
course_context_mock.return_value.objects.all.return_value = [self.course] | ||
|
||
self.assertEqual( | ||
self.form_class().get_content_items_choices(self.request), | ||
[get_content_items_choice_mock.return_value], | ||
) | ||
course_context_mock.assert_called_once_with() | ||
course_context_mock().objects.all.assert_called_once_with() | ||
get_content_items_choice_mock.assert_called_once_with(self.course, self.request) | ||
|
||
@patch(f'{MODULE_PATH}.reverse') | ||
@patch.object(DeepLinkingForm, '__init__', return_value=None) | ||
def test_get_content_items_choice( | ||
self, | ||
deep_linking_form_init: MagicMock, # pylint: disable=unused-argument | ||
reverse_mock: MagicMock, | ||
): | ||
"""Test `get_content_items_choice` method.""" | ||
self.assertEqual( | ||
self.form_class().get_content_items_choice(self.course, self.request), | ||
( | ||
self.request.build_absolute_uri.return_value, | ||
self.course.learning_context.title, | ||
), | ||
) | ||
reverse_mock.assert_called_once_with( | ||
f'{app_config.name}:1.3:resource-link:launch-course', | ||
kwargs={'course_id': self.course.learning_context.context_key}, | ||
) | ||
self.request.build_absolute_uri.assert_called_once_with(reverse_mock()) | ||
|
||
@patch(f'{MODULE_PATH}.DeepLinkResource') | ||
@patch.object(DeepLinkingForm, '__init__', return_value=None) | ||
def test_get_deep_link_resources( | ||
self, | ||
deep_linking_form_init: MagicMock, # pylint: disable=unused-argument | ||
deep_link_resource_mock: MagicMock, | ||
): | ||
"""Test `get_deep_link_resources` method.""" | ||
content_item = 'https://example.com' | ||
form = self.form_class() | ||
form.cleaned_data = {'content_items': [content_item]} | ||
|
||
self.assertEqual( | ||
form.get_deep_link_resources(), | ||
{deep_link_resource_mock.return_value.set_url.return_value}, | ||
) | ||
deep_link_resource_mock.assert_called_once_with() | ||
deep_link_resource_mock().set_url.assert_called_once_with(content_item) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query can become pretty big, do you think it is worth to implement a more robust field? One that has a search option and progressive loading? or maybe only retrieve the courses which are specified in a site configuration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexjmpb Right now there is this other PR that will separate the queryset into another method of this form: #36
I do think we need to come up with a more robust way to handle this form since there seems to be the future requirement to filter per institutions, licenses or even select unit or components, I'm thinking on using a more dynamic form using a JSONField and a custom form template, I'm still not sure of what will be the UI requirements so for now I think is OK to handle it has we are doing on #36.
What do you think?, do you have any resources that could be useful to create a more dynamic form?.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I was worried of not applying a filter on this since it would query all the courses in prod. But #36 does the job, maybe it's worth discussing later if a select field with a search bar can be implemented.