Esempio n. 1
0
def user_patch(context, data_dict):
    '''Patch a user

    :param id: the id or name of the user
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('user_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    user_dict = _get_action('user_show')(
        show_context,
        {'id': _get_or_bust(data_dict, 'id')})

    patched = dict(user_dict)
    patched.pop('display_name', None)
    patched.update(data_dict)
    return _update.user_update(context, patched)
def mailchimp_user_update(context, data_dict):
    user = user_update(context, data_dict)
    mailchimp_client = MailChimpClient(
        api_key=config.get('ckan.mailchimp.api_key', None),
        base_url=config.get('ckan.mailchimp.base_url', None),
        member_list_id=config.get('ckan.mailchimp.member_list_id', None))

    if user is not None and data_dict is not None:
        if data_dict.get('newsletter', None) == 'subscribed':
            # if user is not already in mailchimp add user to mailchimp
            if mailchimp_client.find_subscriber_by_email(
                    data_dict.get('email', None)) is None:
                split_names = name_splitter(
                    data_dict.get('fullname', data_dict.get('name', None)))
                mailchimp_client.create_new_subscriber(split_names[0],
                                                       split_names[1],
                                                       data_dict.get(
                                                           'email', None),
                                                       tags=["NAP-user"])
        elif data_dict.get('newsletter', None) is None or data_dict.get(
                'newsletter', None) == '':
            # if user is already in mailchimp remove user from mailchimp
            if mailchimp_client.find_subscriber_by_email(
                    data_dict.get('email', None)) is not None:
                mailchimp_client.delete_subscriber_by_email(
                    data_dict.get('email', None))

    return user
def dgu_user_update(context, data_dict):
    user_dict = user_update(context, data_dict)

    reset_password = context.get('reset_password')
    password_changed = 'password1' in data_dict and data_dict[
        'password1'] != data_dict['old_password']

    if reset_password or password_changed:
        log.info('User password %s: %s',
                 'is being reset' if reset_password else 'has changed',
                 data_dict['name'])

        user = context['user_obj']
        send_password_alert(user)
    return user_dict
Esempio n. 4
0
def gdpr_user_update(context, data_dict):
    context['schema'] = schema.default_update_user_schema()
    user_dict = user_update(context, data_dict)
    gdpr_accept_list = GDPRAccept.filter(user_id=user_dict['id'])
    delete_list = []
    for gdpr_accept in gdpr_accept_list:
        if 'policy-{}'.format(gdpr_accept.policy_id) not in data_dict.keys():
            delete_list.append(gdpr_accept.id)

    for _id in delete_list:
        GDPRAccept.delete(id=_id)

    for key, value in data_dict.items():
        if key.startswith('policy-'):
            policy_id = int(key.replace('policy-', ''))
            if GDPRAccept.get(user_id=user_dict['id'],
                              policy_id=policy_id) is None:
                GDPRAccept.create(user_id=user_dict['id'],
                                  policy_id=policy_id,
                                  datetime=datetime.datetime.now())
                model.repo.commit()
    return user_dict
Esempio n. 5
0
    def test_user_activity(self):
        """Test user activity streams HTML rendering."""

        # Register a new user.
        user_dict = {'name': 'billybeane',
                'fullname': 'Billy Beane',
                'about': 'General Manager, Oakland Athletics',
                'email': '*****@*****.**',
                'password': '******'}
        context = {
            'model': ckan.model,
            'session': ckan.model.Session,
            'user': self.sysadmin_user.name,
            'allow_partial_update': True,
            }
        user = user_create(context, user_dict)
        offset = url_for('user.activity', id=user['id'])
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s signed up' % user['fullname'] in stripped, stripped

        # Create a new package.
        package = {
            'name' : 'baseball_stats',
            'title' : "Billy's Stats about Baseball Players",
        }
        context['user'] = user['name']
        # FIXME This test use an old way to get at the schema to
        # recreate this we need to pretend to be using the api. We
        # should not be calling package_create like this we should be
        # going via the api or package controllers
        context['api_version'] = 3
        context['ignore_auth'] = True
        package = package_create(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s created the dataset %s ' % (
                user['fullname'], package['title']) in stripped, stripped

        # Add a resource to the package.
        resource = {
            'url': 'http://www.example.com',
            'description': "Chad Bradford's OBP Stats`",
            'format': 'cvs',
            'name': 'Chad Bradford Stats',
            }
        package['resources'].append(resource)
        request_data = {
                'id': package['id'],
                'resources': package['resources'],
                }
        package = package_update(context, request_data)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s added the resource %s to the dataset %s' % \
                (user['fullname'], resource['name'], package['title']) \
                in stripped, stripped

        # Update the package.
        package['title'] =  "Billy's Updated Stats about Baseball Players"
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated the dataset %s' \
                % (user['fullname'], package['title']) \
                in stripped, stripped

        # Update the resource.
        resource = package['resources'][0]
        resource['name'] = 'Chad Bradford Updated Stats'
        resource = resource_update(context, resource)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated the resource %s in the dataset %s' \
                % (user['fullname'], resource['name'], package['title']) \
                in stripped, stripped

        # Delete the resource.
        context['allow_partial_update'] = False
        package['resources'] = []
        package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the resource %s from the dataset %s' % \
                (user['fullname'], resource['name'], package['title']) \
                in stripped, stripped

        # Follow the package.
        follow_dataset(context, {'id': package['id']})
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s started following %s' % (user['fullname'],
                package['title']) not in stripped, stripped

        # Follow another user.
        follow_user(context, {'id': 'joeadmin'})
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s started following %s' % (user['fullname'],
                'joeadmin') not in stripped, stripped

        # Create a new group.
        group = {
            'name': 'baseball-stats-group',
            'title': 'A Group for Datasets about Baseball'
            }
        context['allow_partial_update'] = True
        group = group_create(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s created the group %s' % (user['fullname'], group['title']) \
                in stripped, stripped

        # Update the group.
        group['title'] = 'updated'
        group = group_update(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated the group %s' % (user['fullname'], group['title']) \
                in stripped, stripped

        # Delete the group.
        group['state'] = 'deleted'
        group_update(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the group %s' % (user['fullname'], group['title']) \
                in stripped, stripped

        # Add a new tag to the package.
        tag = {'name': 'baseball'}
        package['tags'].append(tag)
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s added the tag %s to the dataset %s' % \
                (user['fullname'], tag['name'], package['title']) \
                in stripped, stripped

        # Remove the tag from the package.
        package['tags'] = []
        context['allow_partial_update'] = False
        package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s removed the tag %s from the dataset %s' % \
                (user['fullname'], tag['name'], package['title']) \
                in stripped, stripped

        # Add an extra to the package.
        package['extras'].append({'key': 'quality', 'value': '10000'})
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s added the extra "%s" to the dataset %s' % \
                (user['fullname'], 'quality', package['title']) \
                in stripped, stripped

        # Update the extra.
        package['extras'][0]['value'] = 'updated'
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s changed the extra "%s" of the dataset %s' % \
                (user['fullname'], 'quality', package['title']) \
                in stripped, stripped

        # Delete the extra.
        del package['extras'][0]
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the extra "%s" from the dataset %s' % \
                (user['fullname'], 'quality', package['title']) \
                in stripped, stripped

        # Delete the package.
        # we need to get round the delete permission
        context['ignore_auth'] = True
        package_delete(context, package)
        del context['ignore_auth']
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the dataset %s' % \
                (user['fullname'], package['title']) \
                in stripped, stripped

        # Update the user's profile.
        user['about'] = ''
        user_update(context, user)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated their profile' % user['fullname'] \
                in stripped, stripped

        # By now we've created >15 activities, but only the latest 15 should
        # appear on the page.
        result = self.app.get(offset, status=200)
        assert result.body.count('<span class="actor">') \
                == 15, result.body.count('<span class="actor">')

        # The user's dashboard page should load successfully and have the
        # latest 15 activities on it.
        offset = url_for('dashboard.index')
        extra_environ = {'Authorization':
                str(ckan.model.User.get('billybeane').apikey)}
        result = self.app.get(offset, extra_environ=extra_environ,
                status=200)
        assert result.body.count('<span class="actor">') == 15, \
            result.body.count('<span class="actor">')
Esempio n. 6
0
    def test_activity(self):
        """Test activity streams HTML rendering."""

        # Register a new user.
        user_dict = {'name': 'billybeane',
                'fullname': 'Billy Beane',
                'about': 'General Manager, Oakland Athletics',
                'email': '*****@*****.**',
                'password': '******'}
        context = {
            'model': ckan.model,
            'session': ckan.model.Session,
            'user': self.sysadmin_user.name,
            'allow_partial_update': True,
            'extras_as_string': True,
            }
        user = user_create(context, user_dict)
        offset = url_for(controller='user', action='read', id=user['id'])
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s signed up.' % user['fullname'] in stripped, stripped

        # Create a new package.
        package = {
            'name' : 'baseball_stats',
            'title' : "Billy's Stats about Baseball Players",
        }
        context['user'] = user['name']
        package = package_create(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s created the dataset %s ' % (
                user['fullname'], package['title']) in stripped, stripped

        # Add a resource to the package.
        resource = {
            'url': 'http://www.example.com',
            'description': "Chad Bradford's OBP Stats`",
            'format': 'cvs',
            'name': 'Chad Bradford Stats',
            }
        package['resources'].append(resource)
        request_data = {
                'id': package['id'],
                'resources': package['resources'],
                }
        package = package_update(context, request_data)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s added the resource %s to the dataset %s' % \
                (user['fullname'], resource['name'], package['title']) \
                in stripped, stripped

        # Update the package.
        package['title'] =  "Billy's Updated Stats about Baseball Players"
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated the dataset %s' \
                % (user['fullname'], package['title']) \
                in stripped, stripped

        # Update the resource.
        resource = package['resources'][0]
        resource['name'] = 'Chad Bradford Updated Stats'
        resource = resource_update(context, resource)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated the resource %s in the dataset %s' \
                % (user['fullname'], resource['name'], package['title']) \
                in stripped, stripped

        # Delete the resource.
        context['allow_partial_update'] = False
        package['resources'] = []
        package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the resource %s from the dataset %s' % \
                (user['fullname'], resource['name'], package['title']) \
                in stripped, stripped

        # Create a new group.
        group = {
            'name': 'baseball-stats-group',
            'title': 'A Group for Datasets about Baseball'
            }
        context['allow_partial_update'] = True
        group = group_create(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s created the group %s' % (user['fullname'], group['name']) \
                in stripped, stripped

        # Update the group.
        group['title'] = 'updated'
        group = group_update(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated the group %s' % (user['fullname'], group['name']) \
                in stripped, stripped

        # Delete the group.
        group['state'] = 'deleted'
        group_update(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the group %s' % (user['fullname'], group['name']) \
                in stripped, stripped

        # Add a new tag to the package.
        tag = {'name': 'baseball'}
        package['tags'].append(tag)
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s added the tag %s to the dataset %s' % \
                (user['fullname'], tag['name'], package['title']) \
                in stripped, stripped

        # Remove the tag from the package.
        package['tags'] = []
        context['allow_partial_update'] = False
        package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s removed the tag %s from the dataset %s' % \
                (user['fullname'], tag['name'], package['title']) \
                in stripped, stripped

        # Add an extra to the package.
        package['extras'].append({'key': 'quality', 'value': '10000'})
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s added the extra "%s" to the dataset %s' % \
                (user['fullname'], 'quality', package['title']) \
                in stripped, stripped

        # Update the extra.
        package['extras'][0]['value'] = 'updated'
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s changed the extra "%s" of the dataset %s' % \
                (user['fullname'], 'quality', package['title']) \
                in stripped, stripped

        # Delete the extra.
        del package['extras'][0]
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the extra "%s" from the dataset %s' % \
                (user['fullname'], 'quality', package['title']) \
                in stripped, stripped

        # Delete the package.
        package_delete(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s deleted the dataset %s' % \
                (user['fullname'], package['title']) \
                in stripped, stripped

        # Update the user's profile.
        user['about'] = ''
        user_update(context, user)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert '%s updated their profile.' % user['fullname'] \
                in stripped, stripped

        # By now we've created >15 activities, but only the latest 15 should
        # appear on the page.
        result = self.app.get(offset, status=200)
        assert result.body.count('<div class="activity">') \
                == 15
Esempio n. 7
0
    def test_user_activity(self):
        """Test user activity streams HTML rendering."""

        # Register a new user.
        user_dict = {
            "name": "billybeane",
            "fullname": "Billy Beane",
            "about": "General Manager, Oakland Athletics",
            "email": "*****@*****.**",
            "password": "******",
        }
        context = {
            "model": ckan.model,
            "session": ckan.model.Session,
            "user": self.sysadmin_user.name,
            "allow_partial_update": True,
            "extras_as_string": True,
        }
        user = user_create(context, user_dict)
        offset = url_for(controller="user", action="read", id=user["id"])
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s signed up" % user["fullname"] in stripped, stripped

        # Create a new package.
        package = {"name": "baseball_stats", "title": "Billy's Stats about Baseball Players"}
        context["user"] = user["name"]
        # FIXME This test use an old way to get at the schema to
        # recreate this we need to pretend to be using the api. We
        # should not be calling package_create like this we should be
        # going via the api or package controllers
        context["api_version"] = 3
        context["ignore_auth"] = True
        package = package_create(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s created the dataset %s " % (user["fullname"], package["title"]) in stripped, stripped

        # Add a resource to the package.
        resource = {
            "url": "http://www.example.com",
            "description": "Chad Bradford's OBP Stats`",
            "format": "cvs",
            "name": "Chad Bradford Stats",
        }
        package["resources"].append(resource)
        request_data = {"id": package["id"], "resources": package["resources"]}
        package = package_update(context, request_data)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            "%s added the resource %s to the dataset %s" % (user["fullname"], resource["name"], package["title"])
            in stripped
        ), stripped

        # Update the package.
        package["title"] = "Billy's Updated Stats about Baseball Players"
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s updated the dataset %s" % (user["fullname"], package["title"]) in stripped, stripped

        # Update the resource.
        resource = package["resources"][0]
        resource["name"] = "Chad Bradford Updated Stats"
        resource = resource_update(context, resource)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            "%s updated the resource %s in the dataset %s" % (user["fullname"], resource["name"], package["title"])
            in stripped
        ), stripped

        # Delete the resource.
        context["allow_partial_update"] = False
        package["resources"] = []
        package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            "%s deleted the resource %s from the dataset %s" % (user["fullname"], resource["name"], package["title"])
            in stripped
        ), stripped

        # Follow the package.
        follow_dataset(context, {"id": package["id"]})
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s started following %s" % (user["fullname"], package["title"]) not in stripped, stripped

        # Follow another user.
        follow_user(context, {"id": "joeadmin"})
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s started following %s" % (user["fullname"], "joeadmin") not in stripped, stripped

        # Create a new group.
        group = {"name": "baseball-stats-group", "title": "A Group for Datasets about Baseball"}
        context["allow_partial_update"] = True
        group = group_create(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s created the group %s" % (user["fullname"], group["title"]) in stripped, stripped

        # Update the group.
        group["title"] = "updated"
        group = group_update(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s updated the group %s" % (user["fullname"], group["title"]) in stripped, stripped

        # Delete the group.
        group["state"] = "deleted"
        group_update(context, group)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s deleted the group %s" % (user["fullname"], group["title"]) in stripped, stripped

        # Add a new tag to the package.
        tag = {"name": "baseball"}
        package["tags"].append(tag)
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            "%s added the tag %s to the dataset %s" % (user["fullname"], tag["name"], package["title"]) in stripped
        ), stripped

        # Remove the tag from the package.
        package["tags"] = []
        context["allow_partial_update"] = False
        package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            "%s removed the tag %s from the dataset %s" % (user["fullname"], tag["name"], package["title"]) in stripped
        ), stripped

        # Add an extra to the package.
        package["extras"].append({"key": "quality", "value": "10000"})
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            '%s added the extra "%s" to the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped
        ), stripped

        # Update the extra.
        package["extras"][0]["value"] = "updated"
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            '%s changed the extra "%s" of the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped
        ), stripped

        # Delete the extra.
        del package["extras"][0]
        package = package_update(context, package)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert (
            '%s deleted the extra "%s" from the dataset %s' % (user["fullname"], "quality", package["title"])
            in stripped
        ), stripped

        # Delete the package.
        # we need to get round the delete permission
        context["ignore_auth"] = True
        package_delete(context, package)
        del context["ignore_auth"]
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s deleted the dataset %s" % (user["fullname"], package["title"]) in stripped, stripped

        # Update the user's profile.
        user["about"] = ""
        user_update(context, user)
        result = self.app.get(offset, status=200)
        stripped = self.strip_tags(result)
        assert "%s updated their profile" % user["fullname"] in stripped, stripped

        # By now we've created >15 activities, but only the latest 15 should
        # appear on the page.
        result = self.app.get(offset, status=200)
        assert result.body.count('<div class="activity">') == 15

        # The user's dashboard page should load successfully and have the
        # latest 15 activities on it.
        offset = url_for(controller="user", action="dashboard")
        extra_environ = {"Authorization": str(ckan.model.User.get("billybeane").apikey)}
        result = self.app.post(offset, extra_environ=extra_environ, status=200)
        assert result.body.count('<div class="activity">') == 15