Esempio n. 1
0
    def test_showcase_admin_add_multiple_users(self):
        """
        Calling ckanext_showcase_admin_add for multiple users correctly adds
        them to showcase admin list.
        """
        user_to_add = factories.User()
        second_user_to_add = factories.User()

        assert model.Session.query(ShowcaseAdmin).count() == 0

        helpers.call_action(
            "ckanext_showcase_admin_add",
            context={},
            username=user_to_add["name"],
        )

        helpers.call_action(
            "ckanext_showcase_admin_add",
            context={},
            username=second_user_to_add["name"],
        )

        assert model.Session.query(ShowcaseAdmin).count() == 2
        assert user_to_add["id"] in ShowcaseAdmin.get_showcase_admin_ids()
        assert (second_user_to_add["id"]
                in ShowcaseAdmin.get_showcase_admin_ids())
Esempio n. 2
0
def showcase_admin_add(context, data_dict):
    '''Add a user to the list of showcase admins.

    :param username: name of the user to add to showcase user admin list
    :type username: string
    '''

    toolkit.check_access('ckanext_showcase_admin_add', context, data_dict)

    # validate the incoming data_dict
    validated_data_dict, errors = validate(data_dict,
                                           showcase_admin_add_schema(),
                                           context)

    username = toolkit.get_or_bust(validated_data_dict, 'username')
    try:
        user_id = convert_user_name_or_id_to_id(username, context)
    except toolkit.Invalid:
        raise toolkit.ObjectNotFound

    if errors:
        raise toolkit.ValidationError(errors)

    if ShowcaseAdmin.exists(user_id=user_id):
        raise toolkit.ValidationError(
            "ShowcaseAdmin with user_id '{0}' already exists.".format(user_id),
            error_summary=u"User '{0}' is already a Showcase Admin.".format(
                username))

    # create showcase admin entry
    return ShowcaseAdmin.create(user_id=user_id)
Esempio n. 3
0
def showcase_admin_add(context, data_dict):
    '''Add a user to the list of showcase admins.

    :param username: name of the user to add to showcase user admin list
    :type username: string
    '''

    toolkit.check_access('ckanext_showcase_admin_add', context, data_dict)

    # validate the incoming data_dict
    validated_data_dict, errors = validate(
        data_dict, showcase_admin_add_schema(), context)

    username = toolkit.get_or_bust(validated_data_dict, 'username')
    try:
        user_id = convert_user_name_or_id_to_id(username, context)
    except toolkit.Invalid:
        raise toolkit.ObjectNotFound

    if errors:
        raise toolkit.ValidationError(errors)

    if ShowcaseAdmin.exists(user_id=user_id):
        raise toolkit.ValidationError("ShowcaseAdmin with user_id '{0}' already exists.".format(user_id),
                                      error_summary=u"User '{0}' is already a Showcase Admin.".format(username))

    # create showcase admin entry
    return ShowcaseAdmin.create(user_id=user_id)
Esempio n. 4
0
def _is_showcase_admin(context):
    '''
    Determines whether user in context is in the showcase admin list.
    '''
    user = context.get('user', '')
    userobj = model.User.get(user)
    return ShowcaseAdmin.is_user_showcase_admin(userobj)
Esempio n. 5
0
def showcase_admin_remove(context, data_dict):
    '''Remove a user to the list of showcase admins.

    :param username: name of the user to remove from showcase user admin list
    :type username: string
    '''

    model = context['model']

    toolkit.check_access('ckanext_showcase_admin_remove', context, data_dict)

    # validate the incoming data_dict
    validated_data_dict, errors = validate(data_dict,
                                           showcase_admin_remove_schema(),
                                           context)

    if errors:
        raise toolkit.ValidationError(errors)

    username = toolkit.get_or_bust(validated_data_dict, 'username')
    user_id = convert_user_name_or_id_to_id(username, context)

    showcase_admin_to_remove = ShowcaseAdmin.get(user_id=user_id)

    if showcase_admin_to_remove is None:
        raise toolkit.ObjectNotFound(
            "ShowcaseAdmin with user_id '{0}' doesn't exist.".format(user_id))

    showcase_admin_to_remove.delete()
    model.repo.commit()
Esempio n. 6
0
def set_private_if_not_admin_or_showcase_admin(private):
    userobj = model.User.get(c.user)
    if userobj and not (authz.is_sysadmin(c.user)
                        or ShowcaseAdmin.is_user_showcase_admin(userobj)):
        return True
    else:
        return private
Esempio n. 7
0
def showcase_admin_remove(context, data_dict):
    '''Remove a user to the list of showcase admins.

    :param username: name of the user to remove from showcase user admin list
    :type username: string
    '''

    model = context['model']

    toolkit.check_access('ckanext_showcase_admin_remove', context, data_dict)

    # validate the incoming data_dict
    validated_data_dict, errors = validate(data_dict,
                                           showcase_admin_remove_schema(),
                                           context)

    if errors:
        raise toolkit.ValidationError(errors)

    username = toolkit.get_or_bust(validated_data_dict, 'username')
    user_id = convert_user_name_or_id_to_id(username, context)

    showcase_admin_to_remove = ShowcaseAdmin.get(user_id=user_id)

    if showcase_admin_to_remove is None:
        raise toolkit.ObjectNotFound("ShowcaseAdmin with user_id '{0}' doesn't exist.".format(user_id))

    showcase_admin_to_remove.delete()
    model.repo.commit()
Esempio n. 8
0
def showcase_admin_list(context, data_dict):
    '''
    Return a list of dicts containing the id and name of all active showcase
    admin users.

    :rtype: list of dictionaries
    '''

    toolkit.check_access('ckanext_showcase_admin_list', context, data_dict)

    model = context["model"]

    user_ids = ShowcaseAdmin.get_showcase_admin_ids()

    if user_ids:
        q = model.Session.query(model.User) \
            .filter(model.User.state == 'active') \
            .filter(model.User.id.in_(user_ids))

        showcase_admin_list = []
        for user in q.all():
            showcase_admin_list.append({'name': user.name, 'id': user.id})
        return showcase_admin_list

    return []
Esempio n. 9
0
def _is_showcase_admin(context):
    '''
    Determines whether user in context is in the showcase admin list.
    '''
    user = context.get('user', '')
    userobj = model.User.get(user)
    return ShowcaseAdmin.is_user_showcase_admin(userobj)
Esempio n. 10
0
def showcase_admin_list(context, data_dict):
    '''
    Return a list of dicts containing the id and name of all active showcase
    admin users.

    :rtype: list of dictionaries
    '''

    toolkit.check_access('ckanext_showcase_admin_list', context, data_dict)

    model = context["model"]

    user_ids = ShowcaseAdmin.get_showcase_admin_ids()

    if user_ids:
        q = model.Session.query(model.User) \
            .filter(model.User.state == 'active') \
            .filter(model.User.id.in_(user_ids))

        showcase_admin_list = []
        for user in q.all():
            showcase_admin_list.append({'name': user.name, 'id': user.id})
        return showcase_admin_list

    return []
Esempio n. 11
0
    def test_showcase_admin_add_multiple_users(self):
        '''
        Calling ckanext_showcase_admin_add for multiple users correctly adds
        them to showcase admin list.
        '''
        user_to_add = factories.User()
        second_user_to_add = factories.User()

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user_to_add['name'])

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=second_user_to_add['name'])

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 2)
        nosetools.assert_true(user_to_add['id'] in ShowcaseAdmin.get_showcase_admin_ids())
        nosetools.assert_true(second_user_to_add['id'] in ShowcaseAdmin.get_showcase_admin_ids())
Esempio n. 12
0
    def test_showcase_admin_add_no_args(self):
        '''
        Calling ckanext_showcase_admin_add with no args raises ValidationError
        and no ShowcaseAdmin object is created.
        '''
        nosetools.assert_raises(toolkit.ValidationError, helpers.call_action,
                                'ckanext_showcase_admin_add', context={})

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
Esempio n. 13
0
    def test_showcase_admin_add_no_args(self):
        '''
        Calling ckanext_showcase_admin_add with no args raises ValidationError
        and no ShowcaseAdmin object is created.
        '''
        nosetools.assert_raises(toolkit.ValidationError, helpers.call_action,
                                'ckanext_showcase_admin_add', context={})

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
Esempio n. 14
0
    def test_showcase_admin_add_multiple_users(self):
        '''
        Calling ckanext_showcase_admin_add for multiple users correctly adds
        them to showcase admin list.
        '''
        user_to_add = factories.User()
        second_user_to_add = factories.User()

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user_to_add['name'])

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=second_user_to_add['name'])

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 2)
        nosetools.assert_true(user_to_add['id'] in ShowcaseAdmin.get_showcase_admin_ids())
        nosetools.assert_true(second_user_to_add['id'] in ShowcaseAdmin.get_showcase_admin_ids())
Esempio n. 15
0
    def test_showcase_admin_add_username_doesnot_exist(self):
        '''
        Calling ckanext_showcase_admin_add with non-existent username raises
        ValidationError and no ShowcaseAdmin object is created.
        '''
        nosetools.assert_raises(toolkit.ObjectNotFound, helpers.call_action,
                                'ckanext_showcase_admin_add', context={},
                                username='******')

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
Esempio n. 16
0
    def test_showcase_admin_add_username_doesnot_exist(self):
        '''
        Calling ckanext_showcase_admin_add with non-existent username raises
        ValidationError and no ShowcaseAdmin object is created.
        '''
        nosetools.assert_raises(toolkit.ObjectNotFound, helpers.call_action,
                                'ckanext_showcase_admin_add', context={},
                                username='******')

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
Esempio n. 17
0
    def test_showcase_admin_add_no_args(self):
        """
        Calling ckanext_showcase_admin_add with no args raises ValidationError
        and no ShowcaseAdmin object is created.
        """
        with pytest.raises(toolkit.ValidationError):
            helpers.call_action(
                "ckanext_showcase_admin_add",
                context={},
            )

        assert model.Session.query(ShowcaseAdmin).count() == 0
        assert ShowcaseAdmin.get_showcase_admin_ids() == []
    def test_showcase_admin_delete_user_removes_showcase_admin_object(self):
        '''
        Deleting a user also deletes the corresponding ShowcaseAdmin object.
        '''
        user = factories.User()

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user['name'])

        # There's a ShowcaseAdmin object
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 1)
        nosetools.assert_true(user['id'] in ShowcaseAdmin.get_showcase_admin_ids())

        # purge the user, should also delete the ShowcaseAdmin object
        # associated with it.
        user_obj = model.Session.query(model.User).get(user['id'])
        user_obj.purge()
        model.repo.commit_and_remove()

        # The ShowcaseAdmin has also been removed
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
Esempio n. 19
0
    def test_showcase_admin_add_creates_showcase_admin_user(self):
        '''
        Calling ckanext_showcase_admin_add adds user to showcase admin list.
        '''
        user_to_add = factories.User()

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user_to_add['name'])

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 1)
        nosetools.assert_true(user_to_add['id'] in ShowcaseAdmin.get_showcase_admin_ids())
Esempio n. 20
0
    def test_showcase_admin_add_creates_showcase_admin_user(self):
        '''
        Calling ckanext_showcase_admin_add adds user to showcase admin list.
        '''
        user_to_add = factories.User()

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user_to_add['name'])

        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 1)
        nosetools.assert_true(user_to_add['id'] in ShowcaseAdmin.get_showcase_admin_ids())
    def test_showcase_admin_delete_user_removes_showcase_admin_object(self):
        '''
        Deleting a user also deletes the corresponding ShowcaseAdmin object.
        '''
        user = factories.User()

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user['name'])

        # There's a ShowcaseAdmin object
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 1)
        nosetools.assert_true(user['id'] in ShowcaseAdmin.get_showcase_admin_ids())

        # purge the user, should also delete the ShowcaseAdmin object
        # associated with it.
        user_obj = model.Session.query(model.User).get(user['id'])
        user_obj.purge()
        model.repo.commit_and_remove()

        # The ShowcaseAdmin has also been removed
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
Esempio n. 22
0
    def test_showcase_admin_delete_user_removes_showcase_admin_object(self):
        """
        Deleting a user also deletes the corresponding ShowcaseAdmin object.
        """
        user = factories.User()

        helpers.call_action("ckanext_showcase_admin_add",
                            context={},
                            username=user["name"])

        # There's a ShowcaseAdmin object
        assert model.Session.query(ShowcaseAdmin).count() == 1
        assert user["id"] in ShowcaseAdmin.get_showcase_admin_ids()

        # purge the user, should also delete the ShowcaseAdmin object
        # associated with it.
        user_obj = model.Session.query(model.User).get(user["id"])
        user_obj.purge()
        model.repo.commit_and_remove()

        # The ShowcaseAdmin has also been removed
        assert model.Session.query(ShowcaseAdmin).count() == 0
        assert ShowcaseAdmin.get_showcase_admin_ids() == []
Esempio n. 23
0
    def test_showcase_admin_add_username_doesnot_exist(self):
        """
        Calling ckanext_showcase_admin_add with non-existent username raises
        ValidationError and no ShowcaseAdmin object is created.
        """
        with pytest.raises(toolkit.ObjectNotFound):
            helpers.call_action(
                "ckanext_showcase_admin_add",
                context={},
                username="******",
            )

        assert model.Session.query(ShowcaseAdmin).count() == 0
        assert ShowcaseAdmin.get_showcase_admin_ids() == []
Esempio n. 24
0
    def test_showcase_admin_add_creates_showcase_admin_user(self):
        """
        Calling ckanext_showcase_admin_add adds user to showcase admin list.
        """
        user_to_add = factories.User()

        assert model.Session.query(ShowcaseAdmin).count() == 0

        helpers.call_action(
            "ckanext_showcase_admin_add",
            context={},
            username=user_to_add["name"],
        )

        assert model.Session.query(ShowcaseAdmin).count() == 1
        assert user_to_add["id"] in ShowcaseAdmin.get_showcase_admin_ids()
    def test_showcase_admin_remove_deletes_showcase_admin_user(self):
        '''
        Calling ckanext_showcase_admin_remove deletes ShowcaseAdmin object.
        '''
        user = factories.User()

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user['name'])

        # There's a ShowcaseAdmin obj
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 1)

        helpers.call_action('ckanext_showcase_admin_remove', context={},
                            username=user['name'])

        # There's no ShowcaseAdmin obj
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
    def test_showcase_admin_remove_deletes_showcase_admin_user(self):
        '''
        Calling ckanext_showcase_admin_remove deletes ShowcaseAdmin object.
        '''
        user = factories.User()

        helpers.call_action('ckanext_showcase_admin_add', context={},
                            username=user['name'])

        # There's a ShowcaseAdmin obj
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 1)

        helpers.call_action('ckanext_showcase_admin_remove', context={},
                            username=user['name'])

        # There's no ShowcaseAdmin obj
        nosetools.assert_equal(model.Session.query(ShowcaseAdmin).count(), 0)
        nosetools.assert_equal(ShowcaseAdmin.get_showcase_admin_ids(), [])
Esempio n. 27
0
    def test_showcase_admin_remove_deletes_showcase_admin_user(self):
        """
        Calling ckanext_showcase_admin_remove deletes ShowcaseAdmin object.
        """
        user = factories.User()

        helpers.call_action("ckanext_showcase_admin_add",
                            context={},
                            username=user["name"])

        # There's a ShowcaseAdmin obj
        assert model.Session.query(ShowcaseAdmin).count() == 1

        helpers.call_action("ckanext_showcase_admin_remove",
                            context={},
                            username=user["name"])

        # There's no ShowcaseAdmin obj
        assert model.Session.query(ShowcaseAdmin).count() == 0
        assert ShowcaseAdmin.get_showcase_admin_ids() == []
Esempio n. 28
0
def set_private_if_not_admin_or_showcase_admin(private):
    userobj = model.User.get(c.user)
    if userobj and not (authz.is_sysadmin(c.user) or ShowcaseAdmin.is_user_showcase_admin(userobj)):
        return True
    else:
        return private