예제 #1
0
def publish_topic(topic_id, committer_id):
    """Marks the given topic as published.

    Args:
        topic_id: str. The id of the given topic.
        committer_id: str. ID of the committer.

    Raises:
        Exception. The given topic does not exist.
        Exception. The topic is already published.
        Exception. The user does not have enough rights to publish the topic.
    """
    topic_rights = get_topic_rights(topic_id, strict=False)
    if topic_rights is None:
        raise Exception('The given topic does not exist')
    user = user_services.UserActionsInfo(committer_id)
    if role_services.ACTION_CHANGE_TOPIC_STATUS not in user.actions:
        raise Exception(
            'The user does not have enough rights to publish the topic.')

    if topic_rights.topic_is_published:
        raise Exception('The topic is already published.')
    topic_rights.topic_is_published = True
    commit_cmds = [
        topic_domain.TopicRightsChange({'cmd': topic_domain.CMD_PUBLISH_TOPIC})
    ]
    save_topic_rights(topic_rights, committer_id, 'Published the topic',
                      commit_cmds)
예제 #2
0
def publish_topic(topic_id, committer_id):
    """Marks the given topic as published.

    Args:
        topic_id: str. The id of the given topic.
        committer_id: str. ID of the committer.

    Raises:
        Exception. The given topic does not exist.
        Exception. The topic is already published.
        Exception. The user does not have enough rights to publish the topic.
    """
    topic_rights = topic_fetchers.get_topic_rights(topic_id, strict=False)
    if topic_rights is None:
        raise Exception('The given topic does not exist')
    topic = topic_fetchers.get_topic_by_id(topic_id)
    topic.validate(strict=True)
    user = user_services.get_user_actions_info(committer_id)
    if role_services.ACTION_CHANGE_TOPIC_STATUS not in user.actions:
        raise Exception(
            'The user does not have enough rights to publish the topic.')

    if topic_rights.topic_is_published:
        raise Exception('The topic is already published.')
    topic_rights.topic_is_published = True
    commit_cmds = [
        topic_domain.TopicRightsChange({'cmd': topic_domain.CMD_PUBLISH_TOPIC})
    ]
    save_topic_rights(topic_rights, committer_id, 'Published the topic',
                      commit_cmds)
    opportunity_services.create_exploration_opportunities_for_topic(topic.id)
예제 #3
0
def assign_role(committer, assignee, new_role, topic_id):
    """Assigns a new role to the user.

    Args:
        committer: UserActionsInfo. UserActionsInfo object for the user
            who is performing the action.
        assignee: UserActionsInfo. UserActionsInfo object for the user
            whose role is being changed.
        new_role: str. The name of the new role. Possible values are:
            ROLE_MANAGER.
        topic_id: str. ID of the topic.

    Raises:
        Exception. The committer does not have rights to modify a role.
        Exception. The assignee is already a manager for the topic.
        Exception. The assignee doesn't have enough rights to become a manager.
        Exception. The role is invalid.
    """
    committer_id = committer.user_id
    topic_rights = topic_fetchers.get_topic_rights(topic_id)
    if (role_services.ACTION_MODIFY_CORE_ROLES_FOR_ANY_ACTIVITY not in
            committer.actions):
        logging.error(
            'User %s tried to allow user %s to be a %s of topic %s '
            'but was refused permission.' % (
                committer_id, assignee.user_id, new_role, topic_id))
        raise Exception(
            'UnauthorizedUserException: Could not assign new role.')

    assignee_username = user_services.get_username(assignee.user_id)
    if role_services.ACTION_EDIT_OWNED_TOPIC not in assignee.actions:
        raise Exception(
            'The assignee doesn\'t have enough rights to become a manager.')

    old_role = topic_domain.ROLE_NONE
    if topic_rights.is_manager(assignee.user_id):
        old_role = topic_domain.ROLE_MANAGER

    if new_role == topic_domain.ROLE_MANAGER:
        if topic_rights.is_manager(assignee.user_id):
            raise Exception('This user already is a manager for this topic')
        topic_rights.manager_ids.append(assignee.user_id)
    elif new_role == topic_domain.ROLE_NONE:
        if topic_rights.is_manager(assignee.user_id):
            topic_rights.manager_ids.remove(assignee.user_id)
        else:
            old_role = topic_domain.ROLE_NONE
    else:
        raise Exception('Invalid role: %s' % new_role)

    commit_message = rights_domain.ASSIGN_ROLE_COMMIT_MESSAGE_TEMPLATE % (
        assignee_username, old_role, new_role)
    commit_cmds = [topic_domain.TopicRightsChange({
        'cmd': topic_domain.CMD_CHANGE_ROLE,
        'assignee_id': assignee.user_id,
        'old_role': old_role,
        'new_role': new_role
    })]

    save_topic_rights(topic_rights, committer_id, commit_message, commit_cmds)
예제 #4
0
def deassign_manager_role_from_topic(committer, user_id, topic_id):
    """Deassigns given user from all topics assigned to them.

    Args:
        committer: UserActionsInfo. UserActionsInfo object for the user
            who is performing the action.
        user_id: str. The ID of the user.
        topic_id: str. The ID of the topic.

    Raises:
        Exception. The committer does not have rights to modify a role.
    """
    topic_rights = topic_fetchers.get_topic_rights(topic_id)
    if user_id not in topic_rights.manager_ids:
        raise Exception('User does not have manager rights in topic.')

    topic_rights.manager_ids.remove(user_id)
    commit_cmds = [
        topic_domain.TopicRightsChange({
            'cmd': topic_domain.CMD_REMOVE_MANAGER_ROLE,
            'removed_user_id': user_id
        })
    ]
    save_topic_rights(topic_rights, committer.user_id,
                      'Removed all assigned topics from %s' % (user_id),
                      commit_cmds)
예제 #5
0
 def test_topic_rights_change_object_with_extra_attribute_in_cmd(self):
     with self.assertRaisesRegexp(
             utils.ValidationError,
         ('The following extra attributes are present: invalid')):
         topic_domain.TopicRightsChange({
             'cmd': 'publish_topic',
             'invalid': 'invalid'
         })
예제 #6
0
 def test_topic_rights_change_object_with_missing_attribute_in_cmd(self):
     with self.assertRaisesRegexp(
             utils.ValidationError,
         ('The following required attributes are missing: '
          'new_role, old_role')):
         topic_domain.TopicRightsChange({
             'cmd': 'change_role',
             'assignee_id': 'assignee_id',
         })
예제 #7
0
 def test_topic_rights_change_object_with_invalid_role(self):
     with self.assertRaisesRegexp(
         utils.ValidationError, (
             'Value for old_role in cmd change_role: '
             'invalid is not allowed')):
         topic_domain.TopicRightsChange({
             'cmd': 'change_role',
             'assignee_id': 'assignee_id',
             'old_role': 'invalid',
             'new_role': topic_domain.ROLE_MANAGER
         })
예제 #8
0
 def test_to_dict(self):
     topic_rights_change_dict = {
         'cmd': 'change_role',
         'assignee_id': 'assignee_id',
         'old_role': topic_domain.ROLE_NONE,
         'new_role': topic_domain.ROLE_MANAGER
     }
     topic_rights_change_object = topic_domain.TopicRightsChange(
         topic_rights_change_dict)
     self.assertEqual(topic_rights_change_object.to_dict(),
                      topic_rights_change_dict)
예제 #9
0
    def test_topic_rights_change_object_with_change_role(self):
        topic_rights_change_object = topic_domain.TopicRightsChange({
            'cmd': 'change_role',
            'assignee_id': 'assignee_id',
            'old_role': topic_domain.ROLE_NONE,
            'new_role': topic_domain.ROLE_MANAGER
        })

        self.assertEqual(topic_rights_change_object.cmd, 'change_role')
        self.assertEqual(topic_rights_change_object.assignee_id, 'assignee_id')
        self.assertEqual(
            topic_rights_change_object.old_role, topic_domain.ROLE_NONE)
        self.assertEqual(
            topic_rights_change_object.new_role, topic_domain.ROLE_MANAGER)
예제 #10
0
def deassign_user_from_all_topics(committer, user_id):
    """Deassigns given user from all topics assigned to them.

    Args:
        committer: UserActionsInfo. UserActionsInfo object for the user
            who is performing the action.
        user_id: str. The ID of the user.

    Raises:
        Exception. The committer does not have rights to modify a role.
    """
    topic_rights_list = get_topic_rights_with_user(user_id)
    for topic_rights in topic_rights_list:
        topic_rights.manager_ids.remove(user_id)
        commit_cmds = [topic_domain.TopicRightsChange({
            'cmd': topic_domain.CMD_REMOVE_MANAGER_ROLE,
            'removed_user_id': user_id
        })]
        save_topic_rights(
            topic_rights, committer.user_id,
            'Removed all assigned topics from %s' % (user_id), commit_cmds)
예제 #11
0
def unpublish_topic(topic_id, committer_id):
    """Marks the given topic as unpublished.

    Args:
        topic_id: str. The id of the given topic.
        committer_id: str. ID of the committer.

    Raises:
        Exception. The given topic does not exist.
        Exception. The topic is already unpublished.
        Exception. The user does not have enough rights to unpublish the topic.
    """
    topic_rights = topic_fetchers.get_topic_rights(topic_id, strict=False)
    if topic_rights is None:
        raise Exception('The given topic does not exist')
    user = user_services.get_user_actions_info(committer_id)
    if role_services.ACTION_CHANGE_TOPIC_STATUS not in user.actions:
        raise Exception(
            'The user does not have enough rights to unpublish the topic.')

    if not topic_rights.topic_is_published:
        raise Exception('The topic is already unpublished.')
    topic_rights.topic_is_published = False
    commit_cmds = [
        topic_domain.TopicRightsChange(
            {'cmd': topic_domain.CMD_UNPUBLISH_TOPIC})
    ]
    save_topic_rights(topic_rights, committer_id, 'Unpublished the topic',
                      commit_cmds)

    # Delete the exploration opportunities associated with the topic and reject
    # the corresponding translation suggestions.
    exp_ids = (
        opportunity_services.
        get_exploration_opportunity_ids_corresponding_to_topic(topic_id))
    opportunity_services.delete_exploration_opportunities(exp_ids)
    suggestion_services.auto_reject_translation_suggestions_for_exp_ids(
        exp_ids)
예제 #12
0
 def test_cannot_create_topic_rights_change_class_with_invalid_cmd(self):
     with self.assertRaisesRegexp(Exception,
                                  'Command invalid cmd is not allowed'):
         topic_domain.TopicRightsChange({'cmd': 'invalid cmd'})
예제 #13
0
 def test_topic_rights_change_object_with_missing_cmd(self):
     with self.assertRaisesRegexp(utils.ValidationError,
                                  'Missing cmd key in change dict'):
         topic_domain.TopicRightsChange({'invalid': 'data'})
예제 #14
0
 def test_cannot_create_topic_rights_change_class_with_invalid_changelist(
         self):
     with self.assertRaisesRegexp(Exception,
                                  'Missing cmd key in change dict'):
         topic_domain.TopicRightsChange({})
예제 #15
0
    def test_topic_rights_change_object_with_unpublish_topic(self):
        topic_rights_change_object = topic_domain.TopicRightsChange(
            {'cmd': 'unpublish_topic'})

        self.assertEqual(topic_rights_change_object.cmd, 'unpublish_topic')
예제 #16
0
    def test_topic_rights_change_object_with_create_new(self):
        topic_rights_change_object = topic_domain.TopicRightsChange(
            {'cmd': 'create_new'})

        self.assertEqual(topic_rights_change_object.cmd, 'create_new')
예제 #17
0
    def test_create_new_topic_rights_change_class(self):
        topic_rights = topic_domain.TopicRightsChange({'cmd': 'create_new'})

        self.assertEqual(topic_rights.to_dict(), {'cmd': 'create_new'})
예제 #18
0
 def test_topic_change_rights_object_with_invalid_cmd(self):
     with self.assertRaisesRegexp(utils.ValidationError,
                                  'Command invalid is not allowed'):
         topic_domain.TopicRightsChange({'cmd': 'invalid'})