Esempio n. 1
0
    def test_js_inserted_resource_view(self):
        from nose import SkipTest

        raise SkipTest("Test won't work until CKAN 1.5.2")

        from ckan.logic.action import get
        from ckan import model

        context = {"model": model, "ignore_auth": True}
        data = {"id": "annakarenina"}
        pkg = get.package_show(context, data)
        resource_id = pkg["resources"][0]["id"]

        command = LoadAnalytics("loadanalytics")
        command.TEST_HOST = MockClient("localhost", 6969)
        command.CONFIG = self.config
        command.run([])
        response = self.app.get(
            url_for(
                controller="package",
                action="resource_read",
                id="annakarenina",
                resource_id=resource_id,
            ))
        assert 'onclick="javascript: _gaq.push(' in response.body
Esempio n. 2
0
def restricted_package_show(context, data_dict):

    package_metadata = package_show(context, data_dict)

    # Ensure user who can edit can see the resource
    if authz.is_authorized('package_update', context,
                           package_metadata).get('success', False):
        return package_metadata

    # Custom authorization
    if isinstance(package_metadata, dict):
        restricted_package_metadata = dict(package_metadata)
    else:
        restricted_package_metadata = dict(package_metadata.for_json())

    # restricted_package_metadata['resources'] = _restricted_resource_list_url(
    #     context, restricted_package_metadata.get('resources', []))
    restricted_package_metadata[
        'resources'] = _restricted_resource_list_hide_fields(
            context, restricted_package_metadata.get('resources', []))

    restricted_package_metadata['resources'] = filter(
        lambda x: x.get('restricted', '') == '' or json.loads(x['restricted'])[
            'level'] == 'public',
        restricted_package_metadata.get('resources', []))
    restricted_package_metadata['num_resources'] = len(
        restricted_package_metadata['resources'])
    return (restricted_package_metadata)
Esempio n. 3
0
    def _create_or_update_package(self,package_dict,harvest_object):
        '''
            Creates a new package or updates an exisiting one according to the
            package dictionary provided. The package dictionary should look like
            the REST API response for a package:

            http://ckan.net/api/rest/package/statistics-catalunya

            Note that the package_dict must contain an id, which will be used to
            check if the package needs to be created or updated (use the remote
            dataset id).

            If the remote server provides the modification date of the remote
            package, add it to package_dict['metadata_modified'].

        '''
        try:
            #from pprint import pprint 
            #pprint(package_dict)
            ## change default schema
            schema = default_package_schema()
            schema["id"] = [ignore_missing, unicode]

            context = {
                'model': model,
                'session':Session,
                'user': u'harvest',
                'api_version':'2',
                'schema': schema,
            }

            # Check if package exists
            context.update({'id':package_dict['id']})
            try:
                existing_package_dict = package_show(context)
                # Check modified date
                if not 'metadata_modified' in package_dict or \
                   package_dict['metadata_modified'] > existing_package_dict['metadata_modified']:
                    log.info('Package with GUID %s exists and needs to be updated' % harvest_object.guid)
                    # Update package
                    updated_package = package_update_rest(package_dict,context)

                    harvest_object.package_id = updated_package['id']
                    harvest_object.save()
                else:
                    log.info('Package with GUID %s not updated, skipping...' % harvest_object.guid)

            except NotFound:
                # Package needs to be created
                del context['id']
                log.info('Package with GUID %s does not exist, let\'s create it' % harvest_object.guid)
                new_package = package_create_rest(package_dict,context)
                harvest_object.package_id = new_package['id']
                harvest_object.save()

            return True

        except ValidationError,e:
            log.exception(e)
            self._save_object_error('Invalid package with GUID %s: %r'%(harvest_object.guid,e.error_dict),harvest_object,'Import')
Esempio n. 4
0
def package_show_minimal(context, data_dict):
    '''Return the metadata of a dataset (package) and its resources.

    :param id: the id or name of the dataset
    :type id: string
    :param use_default_schema: use default package schema instead of
    a custom schema defined with an IDatasetForm plugin (default: False)
    :type use_default_schema: bool
    
    :rtype: dictionary

    '''
    package = get.package_show(context, data_dict)
    
    if check_logged_in(context):
        fulltext = _get_fulltext(package['id'])
        if fulltext:
            fulltext_dict = { 'key': 'full_text_search',
                              'value': fulltext.text
                            }
            package['extras'].append(fulltext_dict) 
        return package
    
    minimal_package =  _del_extra_field_from_dict(package)
    minimal_package = _del_main_field_from_dict(minimal_package)
    return minimal_package
Esempio n. 5
0
    def edit(self, id, data=None, errors=None, error_summary=None):
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'extras_as_string': True,
                   'preview': 'preview' in request.params,
                   'save': 'save' in request.params,
                   'moderated': config.get('moderated'),
                   'pending': True,
                   'schema': self._form_to_db_schema()}

        if (context['save'] or context['preview']) and not data:
            return self._save_edit(id, context)
        try:
            old_data = get.package_show(context, {'id':id})
            schema = self._db_to_form_schema()
            if schema:
                old_data, errors = validate(old_data, schema)
            data = data or old_data
        except NotAuthorized:
            abort(401, _('Unauthorized to read package %s') % '')
        except NotFound:
            abort(404, _('Package not found'))

        c.pkg = context.get("package")

        am_authz = self.authorizer.am_authorized(c, model.Action.EDIT, c.pkg)
        if not am_authz:
            abort(401, _('User %r not authorized to edit %s') % (c.user, id))

        errors = errors or {}
        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}

        self._setup_template_variables(context, {'id':'id'})
        c.form = render(self.package_form, extra_vars=vars)
        return render('package/edit.html')
Esempio n. 6
0
def restricted_package_show(context, data_dict, package_metadata=None):
    hide_inaccessible_resources = p.toolkit.asbool(data_dict.get('hide_inaccessible_resources', False))
    if not package_metadata:
        package_metadata = package_show(context, data_dict)

    # Ensure user who can edit can see the resource
    if authz.is_authorized(
            'package_update', context, package_metadata).get('success', False):
        return package_metadata

    # Custom authorization
    if isinstance(package_metadata, dict):
        restricted_package_metadata = dict(package_metadata)
    else:
        restricted_package_metadata = dict(package_metadata.for_json())

    # restricted_package_metadata['resources'] = _restricted_resource_list_url(
    #     context, restricted_package_metadata.get('resources', []))
    resources = restricted_package_metadata.get('resources', [])
    if hide_inaccessible_resources:
        resources = _restricted_resource_list_accessible_by_user(context, resources, package_dict=package_metadata)
        restricted_package_metadata['num_resources'] = len(resources)
    resources = _restricted_resource_list_hide_fields(context, resources)
    restricted_package_metadata['resources'] = resources

    return (restricted_package_metadata)
Esempio n. 7
0
def package_show(context, data_dict):
    package_dict = get.package_show(context, data_dict)
    package = Package.get(package_dict['id'])
    package_dict['ratings'] = package.get_average_rating()
    # if package_dict['type'] == 'dataset':
    #     send_log(context, package_dict, 'Dataset metadata accessed',
    #              'DatasetMetadataAccessed')
    return package_dict
Esempio n. 8
0
def package_show(context, data_dict):
    ''' This action is overriden so that the extra field "theme" is added.
    This is needed because when a dataset is exposed to DCAT it needs this
    field.

    Themes are coming from groups where a dataset is added to. The field
    "theme" exists in group's schema.'''

    result = get_core.package_show(context, data_dict)

    dataset_id = result.get('id')
    owner_org = result.get('owner_org')
    model = context.get('model')
    package = model.Package.get(dataset_id)

    if package.type == 'showcase':
        return result

    groups = package.get_groups(group_type='group')

    result = result.copy()
    extras = result.get('extras')
    extra_theme_found = False
    extra_publisher_email_found = False
    extra_publisher_url_found = False

    if extras:
        for extra in extras:
            if extra.get('key') == 'publisher_email':
                extra_publisher_email_found = True
                data_dict = {'id': owner_org}
                org = toolkit.get_action('organization_show')({}, data_dict)
                organization_email = org.get('organization_email')
                extra['value'] = organization_email

            if extra.get('key') == 'publisher_url':
                extra_publisher_url_found = True
                publisher_url = h.url_for(controller='organization',
                                          action='read',
                                          id=owner_org,
                                          qualified=True)
                extra['value'] = publisher_url

        return result
    else:
        result.update({'extras': []})
        extras = result.get('extras')

    org = toolkit.get_action('organization_show')({}, {'id': owner_org})
    organization_email = org.get('organization_email')

    if organization_email:
        extras.append({'key': 'publisher_email', 'value': organization_email})

    return result
Esempio n. 9
0
    def test_basic(self):

        # Create new records
        added, updated, warnings, errors = self.assert_csv_import('from_the_registry.csv',3,0,0,0)

        # Check that packages were actually created
        pkgs = package_list(self.context,{})
        assert len(pkgs) == 3

        # Update existing records and create a new one
        added, updated, warnings, errors = self.assert_csv_import('from_the_registry_update.csv',1,3,0,0)

        # Check that packages were updated and the new one created
        pkgs = package_list(self.context,{})
        assert len(pkgs) == 4

        pkg = package_show(self.context,{'id':'test-publisher-vu'})
        assert 'UPDATED' in pkg['title']
        pkg = package_show(self.context,{'id':'test-publisher-iq'})
        assert 'NEW' in pkg['title']
Esempio n. 10
0
def package_show(context, data_dict):
    '''
    Wraps the default package_show and adds additional information to the resources:
    resource size (for uploaded files) and resource revision timestamp
    '''
    # data_dict['include_tracking'] = True
    package_dict = logic_get.package_show(context, data_dict)

    _additional_hdx_package_show_processing(context, package_dict)

    return package_dict
Esempio n. 11
0
    def test_basic(self):

        # Create new records
        added, updated, warnings, errors = self.assert_csv_import('from_the_registry.csv',3,0,0,0)

        # Check that packages were actually created
        pkgs = package_list(self.context,{})
        assert len(pkgs) == 3

        # Update existing records and create a new one
        added, updated, warnings, errors = self.assert_csv_import('from_the_registry_update.csv',1,3,0,0)

        # Check that packages were updated and the new one created
        pkgs = package_list(self.context,{})
        assert len(pkgs) == 4

        pkg = package_show(self.context,{'id':'test-publisher-vu'})
        assert 'UPDATED' in pkg['title']
        pkg = package_show(self.context,{'id':'test-publisher-iq'})
        assert 'NEW' in pkg['title']
Esempio n. 12
0
def request_resource(context, data_dict):
    '''Sent Email with a resource request to the owner of a dataset.'''
    contact_email = data_dict.get('contact_email')
    owner_id = data_dict.get('creator_id')
    resource_id = data_dict.get('resource_id')
    resource_link = toolkit.url_for(
        action='resource_read',
        controller='package',
        id=data_dict.get('package_name'),
        resource_id=resource_id)
    package = package_show(context, {'id':data_dict.get('package_name')})
    dashboard_restricted = config.get(
        'ckan.site_url') + '/dashboard/restricted'
    # create request e-mail
    extra_vars = {
        'site_title': config.get('ckan.site_title'),
        'site_url': config.get('ckan.site_url'),
        'user_email': data_dict.get('email'),
        'resource_name': data_dict.get('resource_name'),
        'resource_link': config.get('ckan.site_url') + resource_link,
        'package_name': data_dict.get('pkg_title'),
        'message': data_dict.get('message', ''),
        'dashboard_restricted': dashboard_restricted,
        'admin_email_to': config.get('email_to', 'email_to_undefined')}

    body = render_jinja2(
        'restricted/emails/restricted_access_unauth_request.txt', extra_vars)
    subject = \
        _('Αίτημα πρόσβασης στο {0}  από τον χρήστη {1}').format(
            data_dict.get('resource_name'),
            data_dict.get('email'))

    # send e-mail
    try:
        ckan.lib.mailer.mail_recipient(data_dict.get('email'), contact_email,
                                       subject, body)
    except ckan.lib.mailer.MailerException:
        success = False
        # return 'Error sending e-mail'

    request_dict = {'resource_id': resource_id, 'message': data_dict.get('message'),
                    'owner_id': owner_id, 'request_email': data_dict.get('email')
                    }

    logic.save_restricted_request(request_dict)

    # return _('Mail sent!')
    data = {'resource_id': resource_id, 'maintainer_email': contact_email,
            'user_email': data_dict.get('email'), 'package_title':data_dict.get('pkg_title'),
            'message': data_dict.get('message')}
    return render(
        'restricted/restricted_request_access_result.html',
        extra_vars={'data': data, 'pkg_dict': package, 'success': True})
Esempio n. 13
0
def restricted_package_show(context, data_dict):
    package_metadata = package_show(context, data_dict)
    # Ensure user who can edit can see the resource
    if authz.is_authorized('package_update', context,
                           package_metadata).get('success', False):
        return package_metadata

    # Custom authorization
    restricted_package_metadata = package_metadata
    restricted_package_metadata['resources'] = _restricted_resource_list_url(
        context, restricted_package_metadata.get('resources', []))

    return restricted_package_metadata
Esempio n. 14
0
def package_create(context, data_dict):
    if type(data_dict) is dict:
        log.debug('Creating mapping...')
        mapped_resources = []
        if 'resources' in data_dict:
            for resource in data_dict['resources']:
                mapped_resource = generate_mapping(context, resource)
                mapped_resources.append(mapped_resource)
            data_dict['resources'] = mapped_resources

    package_dict = create.package_create(context, data_dict)
    package = None
    if type(package_dict) is not dict:
        package = get.package_show(context, {'id': package_dict})
    else:
        package = get.package_show(context, {'id': package_dict['id']})
    if package is not None:
        if package['type'] == 'dataset':
            send_dataset_log(context, package, 'Dataset created',
                             'DatasetPublished')
            update_de(package)

    return package_dict
Esempio n. 15
0
def show_package(context, data_dict):
    # this function solves the missing value error
    # when dataset schema is changed and we have old datasets
    # that were created prior to the schema change
    if context.get('for_view', None) or context.get('for_edit', None) or context.get('pending', None) or \
            context.get('allow_partial_update', None):
        context['validate'] = False

    if context.get('resource', None):
        model = context['model']
        pkg = model.Package.get(data_dict['id'])
        data_dict = model_dictize.package_dictize(pkg, context)
        if check_if_dataset_using_older_schema(data_dict['extras']):
            context['validate'] = False

    return package_show(context, data_dict)
Esempio n. 16
0
 def test_resource_list(self):
     # TODO restore this test. It doesn't make much sense with the
     # present resource list design.
     name = 'annakarenina'
     cache_url = 'http://thedatahub.org/test_cache_url.csv'
     # add a cache_url to the first resource in the package
     context = {'model': model, 'session': model.Session, 'user': '******'}
     data = {'id': 'annakarenina'}
     pkg = get.package_show(context, data)
     pkg['resources'][0]['cache_url'] = cache_url
     # FIXME need to pretend to be called by the api
     context['api_version'] = 3
     update.package_update(context, pkg)
     # check that the cache url is included on the dataset view page
     offset = url_for(controller='package', action='read', id=name)
     res = self.app.get(offset)
Esempio n. 17
0
def _restricted_resource_list_accessible_by_user(context, resource_list, package_dict=None):
    restricted_resources_list = []
    user_name = logic.restricted_get_username_from_context(context)
    user_obj = context.get('auth_user_obj')
    for resource in resource_list:
        resource_dict = dict(resource)
        if not package_dict:
            package_dict = package_show(context, {'id': resource_dict['package_id']})
        user_has_resource_access = logic.restricted_check_user_resource_access(
            user_name,
            resource_dict,
            package_dict,
            user_obj=user_obj,
            check_access_package_show=False,
            user_organization_dict=logic.get_organization_dict(user_name)
        ).get('success', False)
        if user_has_resource_access:
            restricted_resources_list.append(resource_dict)
    return restricted_resources_list
Esempio n. 18
0
    def read(self, id):
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'extras_as_string': True,
                   'schema': self._form_to_db_schema()}
        data_dict = {'id': id}
        split = id.split('@')
        if len(split) == 2:
            data_dict['id'], revision = split
            try:
                date = datetime.datetime(*map(int, re.split('[^\d]', revision)))
                context['revision_date'] = date
            except ValueError:
                context['revision_id'] = revision
        #check if package exists
        try:
            c.pkg_dict = get.package_show(context, data_dict)
            c.pkg = context['package']
        except NotFound:
            abort(404, _('Package not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read package %s') % id)
        
        cache_key = self._pkg_cache_key(c.pkg)        
        etag_cache(cache_key)
        
        #set a cookie so we know whether to display the welcome message
        c.hide_welcome_message = bool(request.cookies.get('hide_welcome_message', False))
        response.set_cookie('hide_welcome_message', '1', max_age=3600) #(make cross-site?)

        # used by disqus plugin
        c.current_package_id = c.pkg.id
        
        if config.get('rdf_packages'):
            accept_header = request.headers.get('Accept', '*/*')
            for content_type, exts in negotiate(autoneg_cfg, accept_header):
                if "html" not in exts: 
                    rdf_url = '%s%s.%s' % (config['rdf_packages'], c.pkg.id, exts[0])
                    redirect(rdf_url, code=303)
                break

        PackageSaver().render_package(c.pkg_dict, context)
        return render('package/read.html')
Esempio n. 19
0
    def test_js_inserted_resource_view(self):
        from nose import SkipTest
        raise SkipTest("Test won't work until CKAN 1.5.2")

        from ckan.logic.action import get
        from ckan import model
        context = {'model': model, 'ignore_auth': True}
        data = {'id': 'annakarenina'}
        pkg = get.package_show(context, data)
        resource_id = pkg['resources'][0]['id']

        command = LoadAnalytics("loadanalytics")
        command.TEST_HOST = MockClient('localhost', 6969)
        command.CONFIG = self.config
        command.run([])
        response = self.app.get(url_for(
            controller='package', action='resource_read', id='annakarenina',
            resource_id=resource_id
        ))
        assert 'onclick="javascript: _gaq.push(' in response.body
Esempio n. 20
0
    def test_js_inserted_resource_view(self):
        from nose import SkipTest
        raise SkipTest("Test won't work until CKAN 1.5.2")

        from ckan.logic.action import get
        from ckan import model
        context = {'model': model, 'ignore_auth': True}
        data = {'id': 'annakarenina'}
        pkg = get.package_show(context, data)
        resource_id = pkg['resources'][0]['id']

        command = LoadAnalytics("loadanalytics")
        command.TEST_HOST = MockClient('localhost', 6969)
        command.CONFIG = self.config
        command.run([])
        response = self.app.get(url_for(
            controller='package', action='resource_read', id='annakarenina',
            resource_id=resource_id
        ))
        assert 'onclick="javascript: _gaq.push(' in response.body
Esempio n. 21
0
 def test_resource_list(self, app):
     # TODO restore this test. It doesn't make much sense with the
     # present resource list design.
     name = "annakarenina"
     cache_url = "http://thedatahub.org/test_cache_url.csv"
     # add a cache_url to the first resource in the package
     context = {
         "model": model,
         "session": model.Session,
         "user": "******",
     }
     data = {"id": "annakarenina"}
     pkg = get.package_show(context, data)
     pkg["resources"][0]["cache_url"] = cache_url
     # FIXME need to pretend to be called by the api
     context["api_version"] = 3
     update.package_update(context, pkg)
     # check that the cache url is included on the dataset view page
     offset = url_for("dataset.read", id=name)
     res = app.get(offset)
Esempio n. 22
0
def restricted_package_show(context, data_dict):

    package_metadata = package_show(context, data_dict)

    # Ensure user who can edit can see the resource
    if authz.is_authorized('package_update', context,
                           package_metadata).get('success', False):
        return package_metadata

    # Custom authorization
    if (type(package_metadata) == type(dict())):
        restricted_package_metadata = dict(package_metadata)
    else:
        restricted_package_metadata = dict(package_metadata.for_json())

    restricted_package_metadata[
        'resources'] = _restricted_resource_list_hide_fields(
            context, restricted_package_metadata.get('resources', []))

    return (restricted_package_metadata)
Esempio n. 23
0
def package_update(context, data_dict):

    # The only thing we do here is remove some extras that are always
    # inherited from the dataset publisher, to avoid duplicating them
    _remove_extras_from_data_dict(data_dict)
    old_package_dict = get_core.package_show(
        context, {"id": data_dict.get('id') or data_dict.get('name')})
    updated_package = update_core.package_update(context, data_dict)
    # Part of first publisher date patch
    if 'owner_org' in data_dict:
        hlp.first_published_date_patch(updated_package.get('owner_org'))

    if old_package_dict.get('private') and not updated_package.get('private') and \
            updated_package.get('satte') != "deleted":
        # Data is published send an email
        package_title = updated_package.get('title') or updated_package.get(
            'name')
        send_data_published_notification(context,
                                         updated_package.get('owner_org', ''),
                                         package_title)

    return updated_package
Esempio n. 24
0
    def read_ajax(self, id, revision=None):
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'extras_as_string': True,
                   'schema': self._form_to_db_schema(),
                   'revision_id': revision}

        try:
            data = get.package_show(context, {'id': id})
            schema = self._db_to_form_schema()
            if schema:
                data, errors = validate(data, schema)
        except NotAuthorized:
            abort(401, _('Unauthorized to read package %s') % '')
        except NotFound:
            abort(404, _('Package not found'))

        ## hack as db_to_form schema should have this
        data['tag_string'] = ' '.join([tag['name'] for tag in data.get('tags', [])])
        data.pop('tags')
        data = flatten_to_string_key(data)
        response.headers['Content-Type'] = 'application/json;charset=utf-8'
        return json.dumps(data)
Esempio n. 25
0
    def comments(self, id):
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author, 'extras_as_string': True,
                   'schema': self._form_to_db_schema()}

        #check if package exists
        try:
            c.pkg_dict = get.package_show(context, {'id':id})
            c.pkg = context['package']
        except NotFound:
            abort(404, _('Package not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read package %s') % id)

        # used by disqus plugin
        c.current_package_id = c.pkg.id

        for item in self.extensions:
            item.read(c.pkg)

        #render the package
        PackageSaver().render_package(c.pkg_dict)
        return render('package/comments.html')
Esempio n. 26
0
def package_show_minimal(context, data_dict):
    '''Return the metadata of a dataset (package) and its resources.

    :param id: the id or name of the dataset
    :type id: string
    :param use_default_schema: use default package schema instead of
    a custom schema defined with an IDatasetForm plugin (default: False)
    :type use_default_schema: bool
    
    :rtype: dictionary

    '''
    package = get.package_show(context, data_dict)

    if check_logged_in(context):
        fulltext = _get_fulltext(package['id'])
        if fulltext:
            fulltext_dict = {'key': 'full_text_search', 'value': fulltext.text}
            package['extras'].append(fulltext_dict)
        return package

    minimal_package = _del_extra_field_from_dict(package)
    minimal_package = _del_main_field_from_dict(minimal_package)
    return minimal_package
Esempio n. 27
0
def package_show(context, data_dict):
    '''
    Wraps the default package_show and adds additional information to the resources:
    resource size (for uploaded files) and resource revision timestamp
    '''
    # data_dict['include_tracking'] = True
    package_dict = logic_get.package_show(context, data_dict)

    # added because showcase schema validation is generating "ckan.lib.navl.dictization_functions.Missing"
    if 'tracking_summary' in package_dict and not package_dict.get('tracking_summary'):
        del package_dict['tracking_summary']

    # this shouldn't be executed from showcases
    if package_dict.get('type') == 'dataset' and not context.get('no_compute_extra_hdx_show_properties'):

        member_list = get_action('hdx_member_list')(context, {'org_id': package_dict.get('owner_org')})
        if member_list and not member_list.get('is_member'):
            del package_dict['maintainer_email']

        if 'resources' in package_dict:
            # max_time = 0
            # j = None
            # i = 0
            for resource_dict in package_dict.get('resources', []):
                if _should_manually_load_property_value(context, resource_dict, 'size'):
                    resource_dict['size'] = _get_resource_filesize(resource_dict)

                if _should_manually_load_property_value(context, resource_dict, 'revision_last_updated'):
                    resource_dict['revision_last_updated'] = _get_resource_revison_timestamp(resource_dict)

                if _should_manually_load_property_value(context, resource_dict, 'hdx_rel_url'):
                    resource_dict['hdx_rel_url'] = _get_resource_hdx_relative_url(resource_dict)

            #     if _check_dataset_preview_selected_value(context, resource_dict, 'dataset_preview_enabled'):
            #         if resource_dict.get('dataset_preview_enabled') > max_time:
            #             max_time = resource_dict.get('dataset_preview_enabled')
            #             j = i
            #         package_dict['resources'][i]['dataset_preview_enabled'] = False
            #     i = i + 1
            #
            # if j is not None:
            #     package_dict['resources'][j]['dataset_preview_enabled'] = True


        # downloads_list = (res['tracking_summary']['total'] for res in package_dict.get('resources', []) if
        #                   res.get('tracking_summary', {}).get('total'))
        # package_dict['total_res_downloads'] = sum(downloads_list)

        if _should_manually_load_property_value(context, package_dict, 'total_res_downloads'):
            total_res_downloads = jql.downloads_per_dataset_all_cached().get(package_dict['id'], 0)
            log.debug('Dataset {} has {} downloads'.format(package_dict['id'], total_res_downloads))
            package_dict['total_res_downloads'] = total_res_downloads

        if _should_manually_load_property_value(context, package_dict, 'pageviews_last_14_days'):
            pageviews_last_14_days = jql.pageviews_per_dataset_last_14_days_cached().get(package_dict['id'], 0)
            log.debug('Dataset {} has {} page views in the last 14 days'.format(package_dict['id'], pageviews_last_14_days))
            package_dict['pageviews_last_14_days'] = pageviews_last_14_days

        if _should_manually_load_property_value(context, package_dict, 'has_quickcharts'):
            package_dict['has_quickcharts'] = False
            for resource_dict in package_dict.get('resources', []):
                resource_views = get_action('resource_view_list')(context, {'id': resource_dict['id']}) or []
                for view in resource_views:
                    if view.get("view_type") == 'hdx_hxl_preview':
                        package_dict['has_quickcharts'] = True
                        break

        if _should_manually_load_property_value(context, package_dict, 'has_geodata'):
            package_dict['has_geodata'] = False
            for resource_dict in package_dict.get('resources', []):
                if resource_dict.get('format') in GEODATA_FORMATS:
                    package_dict['has_geodata'] = True
                    break

        if _should_manually_load_property_value(context, package_dict, 'has_showcases'):
            package_dict['has_showcases'] = False
            package_dict['num_of_showcases'] = 0
            num_of_showcases = len(hdx_get_package_showcase_id_list(context, {'package_id': package_dict['id']}))
            if num_of_showcases > 0:
                package_dict['has_showcases'] = True
                package_dict['num_of_showcases'] = num_of_showcases

    return package_dict
Esempio n. 28
0
def restricted_package_show(context, data_dict):

    username = context.get('user')
    auth_context = context.get('__auth_audit')
    for_view = context.get('for_view')

    # include_tracking flag is only true for frontend requests on /dataset?
    include_tracking = data_dict.get('include_tracking')

    # try:
    #     auth_context2 = context['__auth_audit']
    #     # logger.info('auth_context2: %s' % auth_context2)
    # except:
    #     logger.error('No direct access to __auth_audit')
    # logger.info('auth_context: %s' % auth_context)
    skip_resource_auth_check = False

    # if auth_context:
    #     dataset_flag = True

    # The combination for_view=True with empty auth_audit seems to be only on /dataset page
    if for_view and not auth_context:
        skip_resource_auth_check = True

    # if include_tracking:
    #     skip_resource_auth_check = True

    try:
        package_metadata = package_show(context, data_dict)
        package_name = package_metadata.get('name')

        logger.info(
            u'User %s: restricted_package_show. Dataset: %s Context: ' %
            (username, package_name))
        print_context(context)
        logger.debug('data_dict: %s' % data_dict)

        # if dataset_flag:
        #     logger.info('Got request from /dataset. User %s: restricted_package_show. Dataset: %s' % (
        #         username, package_name))
        # else:
        #     logger.info('Request WITHOUT __auth_audit. User %s: restricted_package_show. Dataset: %s Context follows: ' % (
        #         username, package_name))
        #     print_context(context)

        # logger.info('User %s: restricted_package_show: %s' % (
        #     username, package_name))

        # try:
        #     logger.info('User %s: restricted_package_show. Context: %s, Data: %s, Dataset: %s' % (
        #         username, context, data_dict, package_name))
        # except:
        #     logger.error('Non-ASCII chars in context or data_dict. Ignore')
        #     pass

        # Ensure the resource is visible to all users who have Edit rights
        if authz.is_authorized('package_update', context,
                               package_metadata).get('success', False):
            logger.info('User %s can UPDATE dataset: %s' %
                        (username, package_name))
            return package_metadata

        # Custom authorization
        if isinstance(package_metadata, dict):
            restricted_package_metadata = dict(package_metadata)
        else:
            restricted_package_metadata = dict(package_metadata.for_json())

        if skip_resource_auth_check:
            logger.warning(
                'Request has for_view OR __auth_audit OR include_tracking. Skip checking each resource.'
            )

        else:
            resource_list = restricted_package_metadata.get('resources', [])
            logger.info(
                'User %s does NOT have update rights. Checking access for %s resources'
                % (username, len(resource_list)))
            restricted_package_metadata[
                'resources'] = _restricted_resource_list_hide_fields(
                    context,
                    resource_list,
                    package=restricted_package_metadata)
            logger.info('Finished Checking access for %s resources' %
                        len(resource_list))

        return (restricted_package_metadata)

    except:
        logger.error('Error in restricted_package_show')
        logger.error(traceback.format_exc())
        logger.warning(type(context))
        logger.warning(u'data_dict: %s' % data_dict)
        pass
Esempio n. 29
0
def package_show(context, data_dict):
    package_dict = get.package_show(context, data_dict)
    package = Package.get(package_dict['id'])
    package_dict['rating'] = package.get_average_rating()
    return package_dict
Esempio n. 30
0
def dgu_package_show(context, data_dict):
    return remove_pii(package_show(context, data_dict))