Exemple #1
0
def filtered_download(resource_view_id):
    params = json.loads(request.form[u'params'])
    resource_view = get_action(u'resource_view_show')(None, {
        u'id': resource_view_id
    })

    search_text = text_type(params[u'search'][u'value'])
    view_filters = resource_view.get(u'filters', {})
    user_filters = text_type(params[u'filters'])
    filters = merge_filters(view_filters, user_filters)

    datastore_search = get_action(u'datastore_search')
    unfiltered_response = datastore_search(
        None, {
            u"resource_id": resource_view[u'resource_id'],
            u"limit": 0,
            u"filters": view_filters,
        })

    cols = [f[u'id'] for f in unfiltered_response[u'fields']]
    if u'show_fields' in resource_view:
        cols = [c for c in cols if c in resource_view[u'show_fields']]

    sort_list = []
    for order in params[u'order']:
        sort_by_num = int(order[u'column'])
        sort_order = (u'desc' if order[u'dir'] == u'desc' else u'asc')
        sort_list.append(cols[sort_by_num] + u' ' + sort_order)

    cols = [c for (c, v) in zip(cols, params[u'visible']) if v]

    colsearch_dict = {}
    columns = params[u'columns']
    for column in columns:
        if column[u'search'][u'value']:
            v = column[u'search'][u'value']
            if v:
                k = column[u'name']
                # replace non-alphanumeric characters with FTS wildcard (_)
                v = re.sub(r'[^0-9a-zA-Z\-]+', '_', v)
                # append ':*' so we can do partial FTS searches
                colsearch_dict[k] = v + u':*'

    if colsearch_dict:
        search_text = json.dumps(colsearch_dict)
    else:
        search_text = re.sub(r'[^0-9a-zA-Z\-]+', '_',
                             search_text) + u':*' if search_text else ''

    return h.redirect_to(
        h.url_for(u'datastore.dump', resource_id=resource_view[u'resource_id'])
        + u'?' + urlencode({
            u'q': search_text,
            u'plain': False,
            u'language': u'simple',
            u'sort': u','.join(sort_list),
            u'filters': json.dumps(filters),
            u'format': request.form[u'format'],
            u'fields': u','.join(cols),
        }))
Exemple #2
0
    def filtered_download(self, resource_view_id):
        params = json.loads(request.params['params'])
        resource_view = get_action(u'resource_view_show')(
            None, {
                u'id': resource_view_id
            })

        search_text = text_type(params['search']['value'])
        view_filters = resource_view.get(u'filters', {})
        user_filters = text_type(params['filters'])
        filters = merge_filters(view_filters, user_filters)

        datastore_search = get_action(u'datastore_search')
        unfiltered_response = datastore_search(
            None, {
                u"resource_id": resource_view[u'resource_id'],
                u"limit": 0,
                u"filters": view_filters,
            })

        cols = [f['id'] for f in unfiltered_response['fields']]
        if u'show_fields' in resource_view:
            cols = [c for c in cols if c in resource_view['show_fields']]

        sort_list = []
        for order in params['order']:
            sort_by_num = int(order['column'])
            sort_order = (u'desc' if order['dir'] == u'desc' else u'asc')
            sort_list.append(cols[sort_by_num] + u' ' + sort_order)

        cols = [c for (c, v) in zip(cols, params['visible']) if v]

        h.redirect_to(
            h.url_for(
                controller=u'ckanext.datastore.controller:DatastoreController',
                action=u'dump',
                resource_id=resource_view[u'resource_id']) + u'?' +
            urlencode({
                u'q': search_text,
                u'sort': u','.join(sort_list),
                u'filters': json.dumps(filters),
                u'format': request.params['format'],
                u'fields': u','.join(cols),
            }))
Exemple #3
0
    def filtered_download(self, resource_view_id):
        params = json.loads(request.params['params'])
        resource_view = get_action(u'resource_view_show')(
            None, {u'id': resource_view_id})

        search_text = text_type(params['search']['value'])
        view_filters = resource_view.get(u'filters', {})
        user_filters = text_type(params['filters'])
        filters = merge_filters(view_filters, user_filters)

        datastore_search = get_action(u'datastore_search')
        unfiltered_response = datastore_search(None, {
            u"resource_id": resource_view[u'resource_id'],
            u"limit": 0,
            u"filters": view_filters,
        })

        cols = [f['id'] for f in unfiltered_response['fields']]
        if u'show_fields' in resource_view:
            cols = [c for c in cols if c in resource_view['show_fields']]

        sort_list = []
        for order in params['order']:
            sort_by_num = int(order['column'])
            sort_order = (
                u'desc' if order['dir'] == u'desc'
                else u'asc')
            sort_list.append(cols[sort_by_num] + u' ' + sort_order)

        cols = [c for (c, v) in zip(cols, params['visible']) if v]

        h.redirect_to(
            h.url_for(
                controller=u'ckanext.datastore.controller:DatastoreController',
                action=u'dump',
                resource_id=resource_view[u'resource_id'])
            + u'?' + urlencode({
                u'q': search_text,
                u'sort': u','.join(sort_list),
                u'filters': json.dumps(filters),
                u'format': request.params['format'],
                u'fields': u','.join(cols),
                }))
Exemple #4
0
    def apiCheck(self, environ, start_response):
        logger.info("apiCheck start: %s ", str(datetime.datetime.time(datetime.datetime.now())))

        setWho = 'system'
        path_info = environ['PATH_INFO'].split("/")
        actionApi = path_info[-1]

        logger.info("action api is: %s", actionApi)

        try:
            backFlag = parseBoolString(config.get('ckan.gov_theme.is_back', False))
        except:
            backFlag = False

        if not backFlag:
            logger.info("if not backFlag: %s", str(not backFlag))

            http_real_address = config.get('ckan.http_real_address')

            logger.info(toolkit.request.environ)
            try:
                remoteAddress = toolkit.request.environ['REMOTE_ADDR']
            except:
                remoteAddress = "No REMOTE_ADDR"
            try:
                realAddress = toolkit.request.environ['HTTP_X_REAL_IP']
            except:
                realAddress = "No REAL_ADDR"

            logger.info("remoteAddress = %s", remoteAddress)
            logger.info("realAddress = %s", realAddress)
            try:
                is_internal_ckan_request = ((str(realAddress) == http_real_address))
            except:
                is_internal_ckan_request = False

            logger.info("Is INTERNAL CKAN REQUEST: " + str(is_internal_ckan_request))

            if not is_internal_ckan_request:
                setWho = 'user'
                if config.get('ckan.api.open', 'NotInList').find(actionApi) < 0:
                    return_dict = {}
                    return_dict['error'] = {'__type': 'Authorization Error',
                                            'message': _('Access denied')}
                    return_dict['success'] = False
                    logger.error("Access denied RemoteAddress = " + remoteAddress + " , API: " + actionApi + ", RealAddress: "+ realAddress)
                    return self._finish(403, return_dict, content_type='json')
        else:
            setWho = 'Back'
        try:
            request_data = self._get_request_data(True)
            logger.info("request_data")
            logger.info(request_data)
        except Exception as e:
            return_dict = {}
            return_dict['success'] = False
            return_dict['__type'] = 'Error'
            return_dict['message'] = e.message
            return self._finish(404, return_dict, content_type='json')

        try:
            helpStr = h.url_for(controller='api',
                                action='action',
                                logic_function='help_show',
                                ver='3',
                                name=actionApi,
                                qualified=True, )
            return_dict = {'help': helpStr}
        except:
            helpStr = ""

        try:
            logger.info("INapiCheck: About to run API: " + actionApi + ", For sytem: " + setWho )
            data = toolkit.get_action(actionApi)(context=environ, data_dict=request_data)
            logger.info("Run api successfuly " )
            return_dict['success'] = True
            return_dict['result'] = data
            return self._finish_ok(return_dict)

        except Exception as e:
            logger.debug("INapiCheck API Error: for action: " + actionApi + ", For sytem: " + setWho + ", Error message: " + e.message)
            return_dict['success'] = False
            return_dict['__type'] = 'Error'
            try:
                return_dict['message'] = e.error_dict
            except:
                return_dict['message'] = e.message
                # return json.dumps({'help': helpStr, 'success': False,  'msg': e.error_dict  })
            return self._finish(404, return_dict, content_type='json')
Exemple #5
0
def _save_new(self, context, package_type=None):
    """ 
       This function is for monkey patching to PackageController of ckan core.
       We add new behavior value 'go-read' here for value of button 'save' to 
       redirect to read url to break the work flow.
    """
    # The staged add dataset used the new functionality when the dataset is
    # partially created so we need to know if we actually are updating or
    # this is a real new.
    is_an_update = False
    ckan_phase = request.params.get('_ckan_phase')
    from ckan.lib.search import SearchIndexError
    try:
        data_dict = clean_dict(
            dict_fns.unflatten(tuplize_dict(parse_params(request.POST))))
        if ckan_phase:
            # prevent clearing of groups etc
            context['allow_partial_update'] = True
            # sort the tags
            if 'tag_string' in data_dict:
                data_dict['tags'] = self._tag_string_to_list(
                    data_dict['tag_string'])
            if data_dict.get('pkg_name'):
                is_an_update = True
                # This is actually an update not a save
                data_dict['id'] = data_dict['pkg_name']
                del data_dict['pkg_name']
                # don't change the dataset state
                data_dict['state'] = 'draft'
                # this is actually an edit not a save
                pkg_dict = get_action('package_update')(context, data_dict)

                if request.params['save'] == 'go-metadata':
                    # redirect to add metadata
                    url = h.url_for(controller='package',
                                    action='new_metadata',
                                    id=pkg_dict['name'])
                elif request.params['save'] == 'go-read':
                    # redirect to read dataset, this is the new
                    # behavior for draft state.
                    url = h.url_for(controller='package',
                                    action='read',
                                    id=pkg_dict['name'])
                else:
                    # redirect to add dataset resources
                    url = h.url_for(controller='package',
                                    action='new_resource',
                                    id=pkg_dict['name'])
                redirect(url)
            # Make sure we don't index this dataset
            if request.params['save'] not in ['go-resource', 'go-metadata']:
                data_dict['state'] = 'draft'
            # allow the state to be changed
            context['allow_state_change'] = True

        data_dict['type'] = package_type
        context['message'] = data_dict.get('log_message', '')
        pkg_dict = get_action('package_create')(context, data_dict)

        if ckan_phase:
            # redirect to add dataset resources
            url = h.url_for(controller='package',
                            action='new_resource',
                            id=pkg_dict['name'])
            redirect(url)

        self._form_save_redirect(pkg_dict['name'],
                                 'new',
                                 package_type=package_type)
    except NotAuthorized:
        abort(403, _('Unauthorized to read package %s') % '')
    except NotFound, e:
        abort(404, _('Dataset not found'))