def reduce(key, stringified_values): values = [ast.literal_eval(v) for v in stringified_values] for item in values: if item['type'] == 'feedback': subscription_services.subscribe_to_thread(key, item['id']) elif item['type'] == 'exploration': subscription_services.subscribe_to_activity(key, item['id'])
def reduce(key, stringified_values): values = [ast.literal_eval(v) for v in stringified_values] for item in values: if item["type"] == "feedback": subscription_services.subscribe_to_thread(key, item["id"]) elif item["type"] == "exploration": subscription_services.subscribe_to_activity(key, item["id"])
def test_thread_and_activity_subscriptions_are_tracked_individually(self): USER_ID = 'user_id' FEEDBACK_THREAD_ID = 'fthread_id' ACTIVITY_ID = 'activity_id' self.assertEqual(self._get_thread_ids_subscribed_to(USER_ID), []) subscription_services.subscribe_to_thread(USER_ID, FEEDBACK_THREAD_ID) subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_ID) self.assertEqual( self._get_thread_ids_subscribed_to(USER_ID), [FEEDBACK_THREAD_ID]) self.assertEqual( self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID])
def test_thread_and_activity_subscriptions_are_tracked_individually(self): USER_ID = 'user_id' FEEDBACK_THREAD_ID = 'fthread_id' ACTIVITY_ID = 'activity_id' self.assertEqual(self._get_thread_ids_subscribed_to(USER_ID), []) subscription_services.subscribe_to_thread(USER_ID, FEEDBACK_THREAD_ID) subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_ID) self.assertEqual(self._get_thread_ids_subscribed_to(USER_ID), [FEEDBACK_THREAD_ID]) self.assertEqual(self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID])
def create_new_exploration_rights(exploration_id, committer_id): exploration_rights = ExplorationRights(exploration_id, [committer_id], [], []) commit_cmds = [{'cmd': CMD_CREATE_NEW}] exp_models.ExplorationRightsModel( id=exploration_rights.id, owner_ids=exploration_rights.owner_ids, editor_ids=exploration_rights.editor_ids, viewer_ids=exploration_rights.viewer_ids, community_owned=exploration_rights.community_owned, status=exploration_rights.status, viewable_if_private=exploration_rights.viewable_if_private, ).commit(committer_id, 'Created new exploration', commit_cmds) subscription_services.subscribe_to_activity(committer_id, exploration_id)
def test_get_activity_ids_subscribed_to(self): USER_ID = 'user_id' self.assertEqual( subscription_services.get_activity_ids_subscribed_to(USER_ID), []) ACTIVITY_ID = 'activity_id' subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_ID) self.assertEqual( subscription_services.get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID]) ACTIVITY_2_ID = 'activity_id_2' subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_2_ID) self.assertEqual( subscription_services.get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID, ACTIVITY_2_ID])
def create_new_exploration_rights(exploration_id, committer_id): exploration_rights = ExplorationRights( exploration_id, [committer_id], [], []) commit_cmds = [{'cmd': CMD_CREATE_NEW}] exp_models.ExplorationRightsModel( id=exploration_rights.id, owner_ids=exploration_rights.owner_ids, editor_ids=exploration_rights.editor_ids, viewer_ids=exploration_rights.viewer_ids, community_owned=exploration_rights.community_owned, status=exploration_rights.status, viewable_if_private=exploration_rights.viewable_if_private, ).commit(committer_id, 'Created new exploration', commit_cmds) subscription_services.subscribe_to_activity( committer_id, exploration_id)
def test_subscribe_to_activity(self): USER_ID = 'user_id' self.assertEqual(self._get_activity_ids_subscribed_to(USER_ID), []) ACTIVITY_ID = 'activity_id' subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_ID) self.assertEqual(self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID]) # Repeated subscriptions to the same activity have no effect. subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_ID) self.assertEqual(self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID]) ACTIVITY_2_ID = 'activity_id_2' subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_2_ID) self.assertEqual(self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID, ACTIVITY_2_ID])
def test_subscribe_to_activity(self): USER_ID = 'user_id' self.assertEqual(self._get_activity_ids_subscribed_to(USER_ID), []) ACTIVITY_ID = 'activity_id' subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_ID) self.assertEqual( self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID]) # Repeated subscriptions to the same activity have no effect. subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_ID) self.assertEqual( self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID]) ACTIVITY_2_ID = 'activity_id_2' subscription_services.subscribe_to_activity(USER_ID, ACTIVITY_2_ID) self.assertEqual( self._get_activity_ids_subscribed_to(USER_ID), [ACTIVITY_ID, ACTIVITY_2_ID])
def assign_role(committer_id, exploration_id, assignee_id, new_role): """Assign `assignee_id` to the given role and subscribes the assignee to future exploration updates. The caller should ensure that assignee_id corresponds to a valid user in the system. Args: - committer_id: str. The user_id of the user who is performing the action. - exploration_id: str. The exploration id. - assignee_id: str. The user_id of the user whose role is being changed. - new_role: str. The name of the new role: either 'owner', 'editor' or 'viewer'. """ if not Actor(committer_id).can_modify_roles(exploration_id): logging.error( 'User %s tried to allow user %s to be a(n) %s of exploration %s ' 'but was refused permission.' % (committer_id, assignee_id, new_role, exploration_id)) raise Exception( 'UnauthorizedUserException: Could not assign new role.') assignee_username = user_services.get_username(assignee_id) old_role = ROLE_NONE if new_role == ROLE_OWNER: if Actor(assignee_id).is_owner(exploration_id): raise Exception('This user already owns this exploration.') exp_rights = get_exploration_rights(exploration_id) exp_rights.owner_ids.append(assignee_id) if assignee_id in exp_rights.viewer_ids: exp_rights.viewer_ids.remove(assignee_id) old_role = ROLE_VIEWER if assignee_id in exp_rights.editor_ids: exp_rights.editor_ids.remove(assignee_id) old_role = ROLE_EDITOR elif new_role == ROLE_EDITOR: if Actor(assignee_id).has_explicit_editing_rights(exploration_id): raise Exception('This user already can edit this exploration.') exp_rights = get_exploration_rights(exploration_id) if exp_rights.community_owned: raise Exception( 'Community-owned explorations can be edited by anyone.') exp_rights.editor_ids.append(assignee_id) if assignee_id in exp_rights.viewer_ids: exp_rights.viewer_ids.remove(assignee_id) old_role = ROLE_VIEWER elif new_role == ROLE_VIEWER: if Actor(assignee_id).has_explicit_viewing_rights(exploration_id): raise Exception('This user already can view this exploration.') exp_rights = get_exploration_rights(exploration_id) if exp_rights.status != EXPLORATION_STATUS_PRIVATE: raise Exception('Public explorations can be viewed by anyone.') exp_rights.viewer_ids.append(assignee_id) else: raise Exception('Invalid role: %s' % new_role) commit_message = 'Changed role of %s from %s to %s' % (assignee_username, old_role, new_role) commit_cmds = [{ 'cmd': CMD_CHANGE_ROLE, 'assignee_id': assignee_id, 'old_role': old_role, 'new_role': new_role }] _save_exploration_rights(committer_id, exp_rights, commit_message, commit_cmds) if new_role in [ROLE_OWNER, ROLE_EDITOR]: subscription_services.subscribe_to_activity(assignee_id, exploration_id)
def assign_role(committer_id, exploration_id, assignee_id, new_role): """Assign `assignee_id` to the given role and subscribes the assignee to future exploration updates. The caller should ensure that assignee_id corresponds to a valid user in the system. Args: - committer_id: str. The user_id of the user who is performing the action. - exploration_id: str. The exploration id. - assignee_id: str. The user_id of the user whose role is being changed. - new_role: str. The name of the new role: either 'owner', 'editor' or 'viewer'. """ if not Actor(committer_id).can_modify_roles(exploration_id): logging.error( 'User %s tried to allow user %s to be a(n) %s of exploration %s ' 'but was refused permission.' % ( committer_id, assignee_id, new_role, exploration_id)) raise Exception( 'UnauthorizedUserException: Could not assign new role.') assignee_username = user_services.get_username(assignee_id) old_role = ROLE_NONE if new_role == ROLE_OWNER: if Actor(assignee_id).is_owner(exploration_id): raise Exception('This user already owns this exploration.') exp_rights = get_exploration_rights(exploration_id) exp_rights.owner_ids.append(assignee_id) if assignee_id in exp_rights.viewer_ids: exp_rights.viewer_ids.remove(assignee_id) old_role = ROLE_VIEWER if assignee_id in exp_rights.editor_ids: exp_rights.editor_ids.remove(assignee_id) old_role = ROLE_EDITOR elif new_role == ROLE_EDITOR: if Actor(assignee_id).has_explicit_editing_rights(exploration_id): raise Exception('This user already can edit this exploration.') exp_rights = get_exploration_rights(exploration_id) if exp_rights.community_owned: raise Exception( 'Community-owned explorations can be edited by anyone.') exp_rights.editor_ids.append(assignee_id) if assignee_id in exp_rights.viewer_ids: exp_rights.viewer_ids.remove(assignee_id) old_role = ROLE_VIEWER elif new_role == ROLE_VIEWER: if Actor(assignee_id).has_explicit_viewing_rights(exploration_id): raise Exception('This user already can view this exploration.') exp_rights = get_exploration_rights(exploration_id) if exp_rights.status != EXPLORATION_STATUS_PRIVATE: raise Exception( 'Public explorations can be viewed by anyone.') exp_rights.viewer_ids.append(assignee_id) else: raise Exception('Invalid role: %s' % new_role) commit_message = 'Changed role of %s from %s to %s' % ( assignee_username, old_role, new_role) commit_cmds = [{ 'cmd': CMD_CHANGE_ROLE, 'assignee_id': assignee_id, 'old_role': old_role, 'new_role': new_role }] _save_exploration_rights( committer_id, exp_rights, commit_message, commit_cmds) if new_role in [ROLE_OWNER, ROLE_EDITOR]: subscription_services.subscribe_to_activity( assignee_id, exploration_id)