forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added story viewer backend handler (oppia#6237)
* First * First * Second * Third * Fourth * Added new file * Added test file and changes * Added test file and running * Fixed linting issues * Fixed linting issues * Fixed linting issues * running tests success * merge conflicts removed * Fixed * Fixed * Changes_Done * Changes_Done
- Loading branch information
1 parent
582c910
commit 0f1a9d7
Showing
7 changed files
with
240 additions
and
0 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,54 @@ | ||
# Copyright 2019 The Oppia Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS-IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Controllers for the story viewer page""" | ||
|
||
from constants import constants | ||
from core.controllers import acl_decorators | ||
from core.controllers import base | ||
from core.domain import story_services | ||
import feconf | ||
|
||
|
||
class StoryPageDataHandler(base.BaseHandler): | ||
"""Manages the data that needs to be displayed to a learner on the | ||
story viewer page. | ||
""" | ||
GET_HANDLER_ERROR_RETURN_TYPE = feconf.HANDLER_TYPE_JSON | ||
|
||
@acl_decorators.can_access_story_viewer_page | ||
def get(self, story_id): | ||
"""Handles GET requests.""" | ||
if not constants.ENABLE_NEW_STRUCTURE_PLAYERS: | ||
raise self.PageNotFoundException | ||
|
||
story = story_services.get_story_by_id(story_id) | ||
|
||
completed_nodes = [completed_node.to_dict() | ||
for completed_node in | ||
story_services.get_completed_nodes_in_story( | ||
self.user_id, story_id)] | ||
|
||
pending_nodes = [pending_node.to_dict() | ||
for pending_node in | ||
story_services.get_pending_nodes_in_story( | ||
self.user_id, story_id)] | ||
|
||
self.values.update({ | ||
'story_title': story.title, | ||
'story_description': story.description, | ||
'completed_nodes': completed_nodes, | ||
'pending_nodes': pending_nodes | ||
}) | ||
self.render_json(self.values) |
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,108 @@ | ||
# Copyright 2019 The Oppia Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS-IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Tests for the story viewer page""" | ||
|
||
from constants import constants | ||
from core.domain import rights_manager | ||
from core.domain import story_domain | ||
from core.domain import story_services | ||
from core.domain import user_services | ||
from core.tests import test_utils | ||
import feconf | ||
|
||
|
||
class BaseStoryViewerControllerTests(test_utils.GenericTestBase): | ||
|
||
def _record_completion(self, user_id, STORY_ID, node_id): | ||
"""Records the completion of a node in the context of a story.""" | ||
story_services.record_completed_node_in_story_context( | ||
user_id, STORY_ID, node_id) | ||
|
||
def setUp(self): | ||
"""Completes the sign up process for the various users.""" | ||
super(BaseStoryViewerControllerTests, self).setUp() | ||
self.VIEWER_EMAIL = '[email protected]' | ||
self.VIEWER_USERNAME = 'viewer' | ||
self.signup(self.ADMIN_EMAIL, self.ADMIN_USERNAME) | ||
self.admin_id = self.get_user_id_from_email(self.ADMIN_EMAIL) | ||
self.set_admins([self.ADMIN_USERNAME]) | ||
self.admin = user_services.UserActionsInfo(self.admin_id) | ||
self.login(self.ADMIN_EMAIL) | ||
self.STORY_ID_1 = 'story_id_1' | ||
self.NODE_ID_1 = 'node_1' | ||
self.NODE_ID_2 = 'node_2' | ||
self.EXP_ID = 'exp_id' | ||
|
||
self.save_new_valid_exploration( | ||
self.EXP_ID, self.admin_id, title='Bridges in England', | ||
category='Architecture', language_code='en') | ||
rights_manager.publish_exploration(self.admin, self.EXP_ID) | ||
story = story_domain.Story.create_default_story( | ||
self.STORY_ID_1, 'Title') | ||
story.description = ('Description') | ||
self.node_1 = { | ||
'id': self.NODE_ID_1, | ||
'title': 'Title 1', | ||
'destination_node_ids': ['node_2'], | ||
'acquired_skill_ids': [], | ||
'prerequisite_skill_ids': [], | ||
'outline': '', | ||
'outline_is_finalized': False, | ||
'exploration_id': self.EXP_ID | ||
} | ||
self.node_2 = { | ||
'id': self.NODE_ID_2, | ||
'title': 'Title 2', | ||
'destination_node_ids': [], | ||
'acquired_skill_ids': [], | ||
'prerequisite_skill_ids': [], | ||
'outline': '', | ||
'outline_is_finalized': False, | ||
'exploration_id': self.EXP_ID | ||
} | ||
story.story_contents.nodes = [ | ||
story_domain.StoryNode.from_dict(self.node_1), | ||
story_domain.StoryNode.from_dict(self.node_2) | ||
] | ||
self.nodes = story.story_contents.nodes | ||
story.story_contents.initial_node_id = 'node_1' | ||
story.story_contents.next_node_id = 'node_3' | ||
story_services.save_new_story(self.admin_id, story) | ||
story_services.publish_story(self.STORY_ID_1, self.admin_id) | ||
self.logout() | ||
self.signup(self.VIEWER_EMAIL, self.VIEWER_USERNAME) | ||
self.viewer_id = self.get_user_id_from_email(self.VIEWER_EMAIL) | ||
self.login(self.VIEWER_EMAIL) | ||
self._record_completion(self.viewer_id, self.STORY_ID_1, self.NODE_ID_1) | ||
|
||
|
||
class StoryPageDataHandlerTests(BaseStoryViewerControllerTests): | ||
def test_get(self): | ||
with self.swap(constants, 'ENABLE_NEW_STRUCTURE_PLAYERS', True): | ||
json_response = self.get_json( | ||
'%s/%s' % (feconf.STORY_DATA_HANDLER, 'story_id_1')) | ||
expected_dict = { | ||
'story_title': 'Title', | ||
'story_description': 'Description', | ||
'completed_nodes': [self.node_1], | ||
'pending_nodes': [self.node_2] | ||
} | ||
self.assertDictContainsSubset(expected_dict, json_response) | ||
|
||
def test_get_fails_when_new_structures_not_enabled(self): | ||
with self.swap(constants, 'ENABLE_NEW_STRUCTURE_PLAYERS', False): | ||
self.get_json( | ||
'%s/%s' % (feconf.STORY_DATA_HANDLER, 'story_id_1'), | ||
expected_status_int=404) |
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