Skip to content

Commit

Permalink
Add links to read-only explorations in the Contribute gallery.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlip committed May 28, 2014
1 parent b136eba commit 18c8fe1
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 148 deletions.
3 changes: 1 addition & 2 deletions core/controllers/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def test_editor(self, exploration_id, escaped_state_name=None, **kwargs):
except:
raise self.PageNotFoundException

if not (rights_manager.Actor(self.user_id).can_edit(exploration_id) or
self.is_super_admin):
if not rights_manager.Actor(self.user_id).can_edit(exploration_id):
raise self.UnauthorizedUserException(
'You do not have the credentials to edit this exploration.',
self.user_id)
Expand Down
17 changes: 4 additions & 13 deletions core/controllers/galleries.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,26 +168,15 @@ def get(self):
# TODO(sll): Implement paging.

explorations_dict = (
exp_services.get_editable_explorations_summary_dict(
exp_services.get_viewable_explorations_summary_dict(
self.user_id))
if (rights_manager.Actor(self.user_id).is_moderator() or
self.is_super_admin):
explorations_dict.update(
exp_services.get_public_explorations_summary_dict())
explorations_dict.update(
exp_services.get_publicized_explorations_summary_dict())

categories = collections.defaultdict(list)
for (eid, exploration_data) in explorations_dict.iteritems():
categories[exploration_data['category']].append({
'id': eid,
'title': exploration_data['title'],
'can_clone': (
rights_manager.Actor(self.user_id).can_clone(eid) or
self.is_super_admin),
'can_edit': (
rights_manager.Actor(self.user_id).can_edit(eid) or
self.is_super_admin),
'can_edit': rights_manager.Actor(self.user_id).can_edit(eid),
'is_private': (
exploration_data['rights']['status'] ==
rights_manager.EXPLORATION_STATUS_PRIVATE),
Expand All @@ -198,6 +187,8 @@ def get(self):
exploration_data['rights']['status'] ==
rights_manager.EXPLORATION_STATUS_PUBLICIZED),
'is_cloned': bool(exploration_data['rights']['cloned_from']),
'is_community_owned': (
exploration_data['rights']['community_owned']),
})

self.values.update({
Expand Down
67 changes: 35 additions & 32 deletions core/controllers/galleries_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import test_utils

CAN_EDIT_STR = 'can_edit'
CAN_CLONE_STR = 'can_clone'


class LearnGalleryTest(test_utils.GenericTestBase):
Expand All @@ -35,7 +34,7 @@ def test_learn_gallery_page(self):
# Test that no edit/copy links are shown (at least in the HTML
# template; a full test should check what happens after the JS is
# loaded and data is fetched from the backend).
no=[CAN_EDIT_STR, CAN_CLONE_STR, 'Create New Exploration']
no=[CAN_EDIT_STR, 'Create New Exploration']
)

# Test that the correct navbar tab is active.
Expand Down Expand Up @@ -104,8 +103,7 @@ def test_playtest_queue_page(self):
# Test that no edit/copy links are shown (at least in the HTML
# template; a full test should check what happens after the JS is
# loaded and data is fetched from the backend).
no=[CAN_EDIT_STR, CAN_CLONE_STR, 'Create New Exploration',
'Categories']
no=[CAN_EDIT_STR, 'Create New Exploration', 'Categories']
)

# Test that the correct navbar tab is active.
Expand Down Expand Up @@ -249,9 +247,7 @@ def test_contribute_gallery_page(self):
self.login('editor@example.com')
response = self.testapp.get(feconf.CONTRIBUTE_GALLERY_URL)
self.assertEqual(response.status_int, 200)
response.mustcontain(
'Categories', 'Create New Exploration', CAN_EDIT_STR,
CAN_CLONE_STR)
response.mustcontain('Create New Exploration', CAN_EDIT_STR)
# Test that the correct navbar tab is active.
self.assertRegexpMatches(
response.body,
Expand Down Expand Up @@ -497,8 +493,8 @@ def attempt_to_delete(self, exploration_id, expect_errors=False):
def test_user_rights(self):
"""Test user rights for explorations in the Contribute gallery."""

# user@example.com, a regular user, can see and edit only exploration
# C, and cannot delete any of the explorations.
# user@example.com, a regular user, can see exploration B, can see and
# edit exploration C, and cannot delete any of the explorations.
EMAIL_USER = 'user@example.com'
self.register_editor(EMAIL_USER)

Expand All @@ -508,19 +504,26 @@ def test_user_rights(self):
self.assertDictContainsSubset({
'is_admin': False,
'is_super_admin': False,
'categories': {
'Test Explorations': [{
'can_edit': True,
'title': 'C',
'can_clone': True,
'id': self.exp_c_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
}]
}
}, response_dict)
self.assertEqual(sorted([{
'can_edit': False,
'title': 'B',
'id': self.exp_b_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': False,
}, {
'can_edit': True,
'title': 'C',
'id': self.exp_c_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': True,
}]), sorted(response_dict['categories']['Test Explorations']))

self.attempt_to_edit(self.exp_a_id, expect_errors=True, version=2)
self.attempt_to_edit(self.exp_b_id, expect_errors=True, version=2)
Expand All @@ -545,21 +548,21 @@ def test_moderator_rights(self):
self.assertEqual(sorted([{
'can_edit': True,
'title': 'B',
'can_clone': True,
'id': self.exp_b_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': False,
}, {
'can_edit': True,
'title': 'C',
'can_clone': True,
'id': self.exp_c_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': True,
}]), sorted(response_dict['categories']['Test Explorations']))

self.attempt_to_edit(self.exp_a_id, expect_errors=True)
Expand All @@ -585,21 +588,21 @@ def test_admin_rights(self):
self.assertEqual(sorted([{
'can_edit': True,
'title': 'B',
'can_clone': True,
'id': self.exp_b_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': False,
}, {
'can_edit': True,
'title': 'C',
'can_clone': True,
'id': self.exp_c_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': True,
}]), sorted(response_dict['categories']['Test Explorations']))

self.attempt_to_edit(self.exp_a_id, expect_errors=True)
Expand All @@ -615,8 +618,8 @@ def test_admin_rights(self):
def test_superadmin_rights(self):
"""Test super-admin rights in the Contribute gallery."""

# superadmin@example.com, a super admin, can edit all explorations,
# but cannot delete any of them.
# superadmin@example.com, a super admin, has no special rights and is
# equivalent to a user.
EMAIL_SUPERADMIN = 'superadmin@example.com'
self.register_editor(EMAIL_SUPERADMIN)

Expand All @@ -627,27 +630,27 @@ def test_superadmin_rights(self):
'is_super_admin': True,
}, response_dict)
self.assertEqual(sorted([{
'can_edit': True,
'can_edit': False,
'title': 'B',
'can_clone': True,
'id': self.exp_b_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': False,
}, {
'can_edit': True,
'title': 'C',
'can_clone': True,
'id': self.exp_c_id,
'is_private': False,
'is_cloned': False,
'is_public': True,
'is_publicized': False,
'is_community_owned': True,
}]), sorted(response_dict['categories']['Test Explorations']))

self.attempt_to_edit(self.exp_a_id)
self.attempt_to_edit(self.exp_b_id, version=2)
self.attempt_to_edit(self.exp_a_id, expect_errors=True)
self.attempt_to_edit(self.exp_b_id, expect_errors=True, version=2)
self.attempt_to_edit(self.exp_c_id, version=2)

self.attempt_to_delete(self.exp_a_id, expect_errors=True)
Expand Down
8 changes: 4 additions & 4 deletions core/controllers/resources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_image_upload_and_download(self):

self._initialize()

self.login(self.EDITOR_EMAIL, is_super_admin=True)
self.login(self.EDITOR_EMAIL)
response = self.testapp.get('/create/0')
csrf_token = self.get_csrf_token_from_response(response)

Expand All @@ -66,7 +66,7 @@ def test_upload_empty_image(self):

self._initialize()

self.login(self.EDITOR_EMAIL, is_super_admin=True)
self.login(self.EDITOR_EMAIL)
response = self.testapp.get('/create/0')
csrf_token = self.get_csrf_token_from_response(response)

Expand All @@ -89,7 +89,7 @@ def test_upload_bad_image(self):

self._initialize()

self.login(self.EDITOR_EMAIL, is_super_admin=True)
self.login(self.EDITOR_EMAIL)
response = self.testapp.get('/create/0')
csrf_token = self.get_csrf_token_from_response(response)

Expand Down Expand Up @@ -121,7 +121,7 @@ def test_bad_filenames_are_detected(self):
# TODO(sll): Add more tests here.
self._initialize()

self.login(self.EDITOR_EMAIL, is_super_admin=True)
self.login(self.EDITOR_EMAIL)
response = self.testapp.get('/create/0')
csrf_token = self.get_csrf_token_from_response(response)

Expand Down
24 changes: 19 additions & 5 deletions core/domain/exp_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ def get_publicized_explorations_summary_dict():
rights_manager.get_publicized_exploration_rights())


def get_non_private_explorations_summary_dict():
"""Returns a summary of non-private explorations."""
return _get_explorations_summary_dict(
rights_manager.get_non_private_exploration_rights())


def get_community_owned_explorations_summary_dict():
"""Returns a summary of community-owned explorations."""
return _get_explorations_summary_dict(
Expand Down Expand Up @@ -238,11 +244,19 @@ def get_owned_explorations_summary_dict(user_id):
rights_manager.get_owned_exploration_rights(user_id))


def get_editable_explorations_summary_dict(user_id):
"""Returns a summary of all explorations editable by this user."""
result = get_community_owned_explorations_summary_dict()
result.update(get_explicit_editor_explorations_summary_dict(user_id))
result.update(get_owned_explorations_summary_dict(user_id))
def get_private_at_least_viewable_summary_dict(user_id):
"""Returns a summary of private explorations that are at least viewable by
this user.
"""
return _get_explorations_summary_dict(
rights_manager.get_private_at_least_viewable_exploration_rights(
user_id))


def get_viewable_explorations_summary_dict(user_id):
"""Returns a summary of all explorations viewable by this user."""
result = get_private_at_least_viewable_summary_dict(user_id)
result.update(get_non_private_explorations_summary_dict())
return result


Expand Down
20 changes: 14 additions & 6 deletions core/domain/exp_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,35 +199,43 @@ def test_get_explicit_viewer_explorations_summary_dict(self):
exp_services.get_explicit_viewer_explorations_summary_dict(
self.OWNER_ID), {})

def test_get_editable_explorations_summary_dict(self):
def test_get_private_at_least_viewable_summary_dict(self):
self.save_new_default_exploration(self.EXP_ID, self.OWNER_ID)
rights_manager.assign_role(
self.OWNER_ID, self.EXP_ID, self.EDITOR_ID,
rights_manager.ROLE_EDITOR)
rights_manager.assign_role(
self.OWNER_ID, self.EXP_ID, self.VIEWER_ID,
rights_manager.ROLE_VIEWER)

exp_dict = {
'title': 'A title',
'category': 'A category',
'rights': {
'owner_names': [self.OWNER_NAME],
'editor_names': [self.EDITOR_NAME],
'viewer_names': [],
'viewer_names': [self.VIEWER_NAME],
'community_owned': False,
'cloned_from': None,
'status': rights_manager.EXPLORATION_STATUS_PRIVATE
}
}

self.assertEqual(
exp_services.get_editable_explorations_summary_dict(self.OWNER_ID),
exp_services.get_private_at_least_viewable_summary_dict(
self.OWNER_ID),
{self.EXP_ID: exp_dict})
self.assertEqual(
exp_services.get_editable_explorations_summary_dict(
exp_services.get_private_at_least_viewable_summary_dict(
self.EDITOR_ID),
{self.EXP_ID: exp_dict})
self.assertEqual(
exp_services.get_editable_explorations_summary_dict(
self.VIEWER_ID), {})
exp_services.get_private_at_least_viewable_summary_dict(
self.VIEWER_ID),
{self.EXP_ID: exp_dict})
self.assertEqual(
exp_services.get_private_at_least_viewable_summary_dict(
'random_user_id'), {})

def test_count_explorations(self):
"""Test count_explorations()."""
Expand Down
14 changes: 14 additions & 0 deletions core/domain/rights_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ def get_publicized_exploration_rights():
exp_models.ExplorationRightsModel.get_publicized()]


def get_non_private_exploration_rights():
"""Returns a list of rights domain objects for non-private explorations."""
return [_get_exploration_rights_from_model(model) for model in
exp_models.ExplorationRightsModel.get_non_private()]


def get_community_owned_exploration_rights():
"""Returns a list of rights objects for community-owned explorations."""
return [_get_exploration_rights_from_model(model) for model in
Expand Down Expand Up @@ -228,6 +234,14 @@ def get_editable_exploration_rights(user_id):
exp_models.ExplorationRightsModel.get_editable(user_id)]


def get_private_at_least_viewable_exploration_rights(user_id):
"""Returns rights objects for all private explorations that this user can
view, edit or own."""
return [_get_exploration_rights_from_model(model) for model in
exp_models.ExplorationRightsModel.get_private_at_least_viewable(
user_id)]


def get_owned_exploration_rights(user_id):
"""Returns rights objects for explorations owned by this user.
Expand Down
Loading

0 comments on commit 18c8fe1

Please sign in to comment.