def get_dataset_information(self):
        log.info('Get information')
        context = {'user': c.user or c.author, 'auth_user_obj': c.userobj, 'for_view': True}

        # Package needs to have a organization group in the call to
        # check_access and also to save it
        try:
            check_access('package_create', context)
            context['ignore_auth'] = True
        except NotAuthorized:
            abort(401, _('Unauthorized to create a package'))

        post_data = ecportal_logic.transform_to_data_dict(request.POST)
        raw_ids = post_data.get('ids').split(',')

        data_list = []
        global_org = None

        for id in raw_ids:
            dataset_dict = {}
            dataset = get_action('package_show')(context,{'id':id})
            try:
                for item in plugins.PluginImplementations(plugins.IPackageController):
                    dataset = item.before_view(dataset)
            except BaseException as e:
                log.error("plugin error on preparation of dataset")
            selected_dataset = context.get('package') #type:DatasetDcatApOp

            if not global_org:
                org_id = selected_dataset.schema.publisher_dcterms['0'].uri.split('/')[-1]
                global_org = get_action('organization_show')(context, {'id': org_id.lower(),
                                                        'include_datasets': 'false'})
            else:
                if global_org['name'] != selected_dataset.schema.publisher_dcterms['0'].uri.split('/')[-1].lower():
                    abort(403, _('Unauthorized to create a package from different Publisher'))

            #org = selected_dataset.schema.publisher_dcterms.get('0').uri
            organization = dataset.get("organization")# todo get the name of the organization
            dataset_name = selected_dataset.schema.ckanName_dcatapop.get('0').value_or_uri
            dataset_title = "No title"
            for titlte_dcterms in selected_dataset.schema.title_dcterms.values():
                if titlte_dcterms.lang == 'en':
                    dataset_title = titlte_dcterms.value_or_uri
            dataset_url = selected_dataset.schema.uri
            status = ['published' if selected_dataset.privacy_state == 'public' else 'draft'] # todo get the correct value
            dataset_dict['title'] = dataset_title
            dataset_dict['url'] = dataset_url
            dataset_dict['organization'] = organization
            dataset_dict['status'] = status
            dataset_dict['name'] = dataset_name

            data_list.append(dataset_dict)

        result = [{'title': value['title'],
                   'name': value['name'],
                   'url': value['url'],
                   'publisher': value['organization'].get('name','').upper(),
                   'state': next((item for item in value['status'] if item is not None), '')} for value in data_list]
        return json.dumps(result)
示例#2
0
class GroupController(base.BaseController):

    group_type = 'group'

    ## hooks for subclasses

    def _group_form(self, group_type=None):
        return lookup_group_plugin(group_type).group_form()

    def _form_to_db_schema(self, group_type=None):
        return lookup_group_plugin(group_type).form_to_db_schema()

    def _db_to_form_schema(self, group_type=None):
        '''This is an interface to manipulate data from the database
        into a format suitable for the form (optional)'''
        return lookup_group_plugin(group_type).db_to_form_schema()

    def _setup_template_variables(self, context, data_dict, group_type=None):
        return lookup_group_plugin(group_type).\
            setup_template_variables(context, data_dict)

    def _new_template(self, group_type):
        return lookup_group_plugin(group_type).new_template()

    def _index_template(self, group_type):
        return lookup_group_plugin(group_type).index_template()

    def _about_template(self, group_type):
        return lookup_group_plugin(group_type).about_template()

    def _read_template(self, group_type):
        return lookup_group_plugin(group_type).read_template()

    def _history_template(self, group_type):
        return lookup_group_plugin(group_type).history_template()

    def _edit_template(self, group_type):
        return lookup_group_plugin(group_type).edit_template()

    def _activity_template(self, group_type):
        return lookup_group_plugin(group_type).activity_template()

    def _admins_template(self, group_type):
        return lookup_group_plugin(group_type).admins_template()

    def _bulk_process_template(self, group_type):
        return lookup_group_plugin(group_type).bulk_process_template()

    ## end hooks
    def _replace_group_org(self, string):
        ''' substitute organization for group if this is an org'''
        if self.group_type == 'organization':
            string = re.sub('^group', 'organization', string)
        return string

    def _action(self, action_name):
        ''' select the correct group/org action '''
        return get_action(self._replace_group_org(action_name))

    def _check_access(self, action_name, *args, **kw):
        ''' select the correct group/org check_access '''
        return check_access(self._replace_group_org(action_name), *args, **kw)

    def _render_template(self, template_name):
        ''' render the correct group/org template '''
        return render(self._replace_group_org(template_name))

    def _redirect_to(self, *args, **kw):
        ''' wrapper to ensue the correct controller is used '''
        if self.group_type == 'organization' and 'controller' in kw:
            kw['controller'] = 'organization'
        return h.redirect_to(*args, **kw)

    def _url_for(self, *args, **kw):
        ''' wrapper to ensue the correct controller is used '''
        if self.group_type == 'organization' and 'controller' in kw:
            kw['controller'] = 'organization'
        return h.url_for(*args, **kw)

    def _guess_group_type(self, expecting_name=False):
        """
            Guess the type of group from the URL handling the case
            where there is a prefix on the URL (such as /data/organization)
        """
        parts = [x for x in request.path.split('/') if x]

        idx = -1
        if expecting_name:
            idx = -2

        gt = parts[idx]
        if gt == 'group':
            gt = None

        return gt

    def index(self):
        group_type = self._guess_group_type()

        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'for_view': True,
            'with_private': False
        }

        q = c.q = request.params.get('q', '')
        data_dict = {'all_fields': True, 'q': q}
        sort_by = c.sort_by_selected = request.params.get('sort')
        if sort_by:
            data_dict['sort'] = sort_by
        try:
            self._check_access('site_read', context)
        except NotAuthorized:
            abort(401, _('Not authorized to see this page'))

        # pass user info to context as needed to view private datasets of
        # orgs correctly
        if c.userobj:
            context['user_id'] = c.userobj.id
            context['user_is_admin'] = c.userobj.sysadmin

        results = self._action('group_list')(context, data_dict)

        c.amount_group_diplayed = amount_group_displayed

        c.groups = results[:amount_group_displayed]
        c.hasmoregroups = len(results) > amount_group_displayed

        #c.page = h.Page(
        #    collection=results,
        #    page=request.params.get('page', 1),
        #    url=h.pager_url,
        #    items_per_page=21
        #)
        return render(self._index_template(group_type))

    def read(self, id, limit=20):

        if request.GET.get('ext_boolean') in ['all', 'any', 'exact']:
            base.session['ext_boolean'] = request.GET['ext_boolean']
            base.session.save()

        group_type = self._get_group_type(id.split('@')[0])
        if (group_type != self.group_type) and (group_type !=
                                                "eurovoc_domain"):
            abort(404, _('Incorrect group type'))

        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'schema': self._db_to_form_schema(group_type=group_type),
            'for_view': True
        }
        # Do not query for the group datasets when dictizing, as they will
        # be ignored and get requested on the controller anyway
        data_dict = {'id': id, 'include_datasets': False}

        # unicode format (decoded from utf8)
        q = c.q = request.params.get('q', '')

        try:
            c.group_dict = self._action('group_show')(context, data_dict)
            c.group = context['group']
        except NotFound:
            abort(404, _('Group not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read group %s') % id)

        self._read(id, limit)
        return render(self._read_template(c.group_dict['type']))

    def _read(self, id, limit):
        ''' This is common code used by both read and bulk_process'''
        group_type = self._get_group_type(id.split('@')[0])
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'schema': self._db_to_form_schema(group_type=group_type),
            'for_view': True,
            'extras_as_string': True
        }

        q = c.q = request.params.get('q', '')
        # Search within group
        if c.group_dict.get('is_organization'):
            q += ' owner_org:"%s"' % c.group_dict.get('id')
        else:
            q += ' groups:"%s"' % c.group_dict.get('name')

        c.description_formatted = h.render_markdown(
            c.group_dict.get('description'))

        context['return_query'] = True

        # c.group_admins is used by CKAN's legacy (Genshi) templates only,
        # if we drop support for those then we can delete this line.
        c.group_admins = new_authz.get_group_or_org_admin_ids(c.group.id)

        try:
            page = int(request.params.get('page', 1))
        except ValueError, e:
            abort(400, ('"page" parameter must be an integer'))

        # most search operations should reset the page counter:
        params_nopage = [(k, v) for k, v in request.params.items()
                         if k != 'page']

        new_params_nopage = []
        for key, value in params_nopage:
            if key == 'eurovoc_domains':
                new_params_nopage.append(('groups', value))
            else:
                new_params_nopage.append((key, value))

        params_nopage = new_params_nopage

        #sort_by = request.params.get('sort', 'name asc')
        sort_by = request.params.get('sort', None)

        def search_url(params):
            if group_type == 'organization':
                if c.action == 'bulk_process':
                    url = self._url_for(controller='organization',
                                        action='bulk_process',
                                        id=id)
                else:
                    url = self._url_for(controller='organization',
                                        action='read',
                                        id=id)
            else:
                url = self._url_for(controller='group', action='read', id=id)
            params = [
                (k, v.encode('utf-8') if isinstance(v, basestring) else str(v))
                for k, v in params
            ]
            return url + u'?' + urlencode(params)

        def drill_down_url(**by):
            return h.add_url_param(alternative_url=None,
                                   controller='group',
                                   action='read',
                                   extras=dict(id=c.group_dict.get('name')),
                                   new_params=by)

        c.drill_down_url = drill_down_url

        def remove_field(key, value=None, replace=None):
            if c.group_dict.get('is_organization'):
                return h.remove_url_param(
                    key,
                    value=value,
                    replace=replace,
                    controller='organization',
                    action='read',
                    extras=dict(id=c.group_dict.get('id')))
            else:
                return h.remove_url_param(
                    key,
                    value=value,
                    replace=replace,
                    controller='group',
                    action='read',
                    extras=dict(id=c.group_dict.get('name')))

        c.remove_field = remove_field

        def pager_url(q=None, page=None):
            params = list(params_nopage)
            params.append(('page', page))
            return search_url(params)

        try:
            c.fields = []
            search_extras = {}
            for (param, value) in request.params.items():
                if not param in ['q', 'page', 'sort'] \
                        and len(value) and not param.startswith('_'):
                    if not param.startswith('ext_'):
                        c.fields.append((param, value))
                        param = 'groups' if (param
                                             == 'eurovoc_domains') else param
                        q += ' %s: "%s"' % (param, value)
                    else:
                        search_extras[param] = value

            fq = 'capacity:"public"'
            user_member_of_orgs = [
                org['id'] for org in h.organizations_available('read')
            ]
            if (c.group and c.group.id in user_member_of_orgs):
                fq = ''
                context['ignore_capacity_check'] = True

            facets = OrderedDict()

            default_facet_titles = {
                'organization': _('Organizations'),
                'groups': _('Groups'),
                'tags': _('Tags'),
                'res_format': _('Formats'),
                'license_id': _('Licenses')
            }

            for facet in g.facets:
                if facet in default_facet_titles:
                    facets[facet] = default_facet_titles[facet]
                else:
                    facets[facet] = facet

            # Facet titles
            for plugin in plugins.PluginImplementations(plugins.IFacets):
                if self.group_type == 'organization':
                    facets = plugin.organization_facets(
                        facets, self.group_type, None)
                else:
                    facets = plugin.group_facets(facets, self.group_type, None)

            if 'capacity' in facets and (self.group_type != 'organization'
                                         or not user_member_of_orgs):
                del facets['capacity']

            c.facet_titles = facets

            data_dict = {
                'q': q,
                'fq': fq,
                'facet.field': facets.keys(),
                'rows': limit,
                'sort': sort_by,
                'start': (page - 1) * limit,
                'extras': search_extras
            }

            context_ = dict(
                (k, v) for (k, v) in context.items() if k != 'schema')
            query = get_action('package_search')(context_, data_dict)

            c.search_url_params = urlencode(_encode_params(params_nopage))
            c.page = h.Page(collection=query['results'],
                            page=page,
                            url=pager_url,
                            item_count=query['count'],
                            items_per_page=limit)

            c.group_dict['package_count'] = query['count']
            c.facets = query['facets']
            maintain.deprecate_context_item('facets',
                                            'Use `c.search_facets` instead.')

            c.search_facets = query['search_facets']
            c.search_facets_limits = {}
            for facet in c.facets.keys():
                limit = int(
                    request.params.get('_%s_limit' % facet,
                                       g.facets_default_number))
                c.search_facets_limits[facet] = limit
            c.page.items = query['results']

            c.sort_by_selected = sort_by

        except search.SearchError, se:
            log.error('Group search error: %r', se.args)
            c.query_error = True
            c.facets = {}
            c.page = h.Page(collection=[])
            c.search_url_params = ''
示例#3
0
 def _update_facet_titles(self, facets, group_type):
     for plugin in plugins.PluginImplementations(plugins.IFacets):
         facets = plugin.group_facets(facets, group_type, None)
示例#4
0
    def _read(self, id, limit):
        ''' This is common code used by both read and bulk_process'''
        group_type = self._get_group_type(id.split('@')[0])
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'schema': self._db_to_form_schema(group_type=group_type),
            'for_view': True,
            'extras_as_string': True
        }

        q = c.q = request.params.get('q', '')
        # Search within group
        if c.group_dict.get('is_organization'):
            q += ' owner_org:"%s"' % c.group_dict.get('id')
        else:
            q += ' groups:"%s"' % c.group_dict.get('name')

        c.description_formatted = h.render_markdown(
            c.group_dict.get('description'))

        context['return_query'] = True

        # c.group_admins is used by CKAN's legacy (Genshi) templates only,
        # if we drop support for those then we can delete this line.
        c.group_admins = authz.get_group_or_org_admin_ids(c.group.id)

        page = self._get_page_number(request.params)

        # most search operations should reset the page counter:
        params_nopage = [(k, v) for k, v in request.params.items()
                         if k != 'page']
        sort_by = request.params.get('sort', None)

        def search_url(params):
            if group_type == 'organization':
                if c.action == 'bulk_process':
                    url = self._url_for(controller='organization',
                                        action='bulk_process',
                                        id=id)
                else:
                    url = self._url_for(controller='organization',
                                        action='read',
                                        id=id)
            else:
                url = self._url_for(controller='group', action='read', id=id)
            params = [
                (k, v.encode('utf-8') if isinstance(v, basestring) else str(v))
                for k, v in params
            ]
            return url + u'?' + urlencode(params)

        def drill_down_url(**by):
            return h.add_url_param(alternative_url=None,
                                   controller='group',
                                   action='read',
                                   extras=dict(id=c.group_dict.get('name')),
                                   new_params=by)

        c.drill_down_url = drill_down_url

        def remove_field(key, value=None, replace=None):
            return h.remove_url_param(key,
                                      value=value,
                                      replace=replace,
                                      controller='group',
                                      action='read',
                                      extras=dict(id=c.group_dict.get('name')))

        c.remove_field = remove_field

        def pager_url(q=None, page=None):
            params = list(params_nopage)
            params.append(('page', page))
            return search_url(params)

        try:
            c.fields = []
            search_extras = {}
            for (param, value) in request.params.items():
                if not param in ['q', 'page', 'sort'] \
                        and len(value) and not param.startswith('_'):
                    if not param.startswith('ext_'):
                        c.fields.append((param, value))
                        q += ' %s: "%s"' % (param, value)
                    else:
                        search_extras[param] = value

            fq = 'capacity:"public"'
            user_member_of_orgs = [
                org['id'] for org in h.organizations_available('read')
            ]

            if (c.group and c.group.id in user_member_of_orgs):
                fq = ''
                context['ignore_capacity_check'] = True

            facets = OrderedDict()

            default_facet_titles = {
                'organization': _('Organizations'),
                'groups': _('Groups'),
                'tags': _('Tags'),
                'res_format': _('Formats'),
                'license_id': _('Licenses')
            }

            for facet in g.facets:
                if facet in default_facet_titles:
                    facets[facet] = default_facet_titles[facet]
                else:
                    facets[facet] = facet

            # Facet titles
            for plugin in plugins.PluginImplementations(plugins.IFacets):
                if self.group_type == 'organization':
                    facets = plugin.organization_facets(
                        facets, self.group_type, None)
                else:
                    facets = plugin.group_facets(facets, self.group_type, None)

            if 'capacity' in facets and (self.group_type != 'organization'
                                         or not user_member_of_orgs):
                del facets['capacity']

            c.facet_titles = facets

            data_dict = {
                'q': q,
                'fq': fq,
                'facet.field': facets.keys(),
                'rows': limit,
                'sort': sort_by,
                'start': (page - 1) * limit,
                'extras': search_extras
            }

            context_ = dict(
                (k, v) for (k, v) in context.items() if k != 'schema')
            query = get_action('package_search')(context_, data_dict)

            c.page = h.Page(collection=query['results'],
                            page=page,
                            url=pager_url,
                            item_count=query['count'],
                            items_per_page=limit)

            c.group_dict['package_count'] = query['count']
            c.facets = query['facets']
            maintain.deprecate_context_item('facets',
                                            'Use `c.search_facets` instead.')

            c.search_facets = query['search_facets']
            c.search_facets_limits = {}
            for facet in c.facets.keys():
                limit = int(
                    request.params.get('_%s_limit' % facet,
                                       g.facets_default_number))
                c.search_facets_limits[facet] = limit
            c.page.items = query['results']

            c.sort_by_selected = sort_by

        except search.SearchError, se:
            log.error('Group search error: %r', se.args)
            c.query_error = True
            c.facets = {}
            c.page = h.Page(collection=[])
示例#5
0
    def manage_package(self):
        package_type = 'dataset'
        context = {
            'user': c.user or c.author,
            'auth_user_obj': c.userobj,
            'save_locally': 'save_locally' in request.params,
            'add_rdf': 'add_rdf' in request.params,
            'add_delete': 'add_delete' in request.params,
            'upload': 'upload' in request.params,
        }

        # Package needs to have a organization group in the call to
        # check_access and also to save it
        try:
            check_access('package_create', context)
        except NotAuthorized:
            abort(401, _('Unauthorized to create a package'))

        data = None
        id = TEST
        workdir = os.path.join(BASE_DIR, c.user)
        if os.path.exists(
                os.path.join(workdir, request.POST.get('file_path',
                                                       'nofile'))):
            id = os.path.join(workdir, request.POST.get('file_path', ''))

        if context['upload']:
            request.POST.pop('upload')
            file = request.POST.pop('resource_file_upload')
            data = data or ecportal_logic.transform_to_data_dict(request.POST)
            data = self._safe_resource_upload(data, file)

        if context['save_locally'] and not data:
            request.POST.pop('save_locally')
            data = data or ecportal_logic.transform_to_data_dict(request.POST)

            for package in data.get('dataset', []):
                package = get_action('validate_dataset')(context, package)
                if package.get('domains_eurovoc'):
                    groups = ingestion.get_groups_from_database_by_title(
                        package.get('domains_eurovoc'))
                    package['groups'] = groups
                split_id = package['owner_org']
                if not data.get('organization'):
                    org = get_action('organization_show')(
                        context, {
                            'id': split_id,
                            'include_datasets': 'false'
                        })
                    package['organization'] = org
            self._create_temporary_files_for_packaging(context, data)
            return

        if context['add_rdf'] and not data:
            request.POST.pop('add_rdf')
            data = data or ecportal_logic.transform_to_data_dict(request.POST)
            tmp_list = data.get('dataset', [])
            tmp_list.append({'id': 'dew_dataset'})
            data['dataset'] = tmp_list

        if context['add_delete'] and not data:
            request.POST.pop('add_delete')
            data = data or ecportal_logic.transform_to_data_dict(request.POST)
            tmp_list = data.get('delete', [])
            tmp_list.append({'name': 'dew_delete_action'})
            data['delete'] = tmp_list

        if not data:
            data = {
                'dataset': [],
                'manifest': [{}],
                'files': [],
                'delete': [],
                'organization': {}
            }

            if id:
                zf = ingestion.read_zip_file_content(id)

                for file in zf.namelist():
                    if '.xml' in file and '/files' not in file:
                        fileContent = zf.read(file)
                        simple_name = os.path.basename(
                            os.path.splitext(file)[0])
                        manifest = ingestion.parse_xml_to_dict(fileContent)
                        data['manifest'] = [{
                            'publisher_uri':
                            manifest.get('ecodp:manifest').get(
                                '@ecodp:publisher'),
                            'package_id':
                            manifest.get('ecodp:manifest').get(
                                '@ecodp:package-id'),
                            'creation_date':
                            manifest.get('ecodp:manifest').get(
                                '@ecodp:creation-date-time')
                        }]

                        action_list = []
                        for del_action in manifest.get('ecodp:manifest').get(
                                'ecodp:action'):
                            if del_action.get('ecodp:remove'):
                                action_list.append({
                                    'uri':
                                    del_action.get('@codp:object-uri'),
                                    'name':
                                    del_action.get('@ecodp:object-ckan-name')
                                })

                        data['delete'] = action_list

                for file in zf.namelist():
                    if '.rdf' in file and '/files' not in file:
                        rdf = {}
                        fileContent = zf.read(file)
                        simple_name = os.path.basename(
                            os.path.splitext(file)[0])
                        rdf = ingestion.mapp_rdf2ckan_fields(fileContent)
                        split_id = rdf['owner_org'].split('/')
                        if not data.get('organization'):
                            org = get_action('organization_show')(
                                context, {
                                    'id': split_id[-1].lower(),
                                    'include_datasets': 'false'
                                })
                            data['organization'] = org
                        rdf['owner_org'] = org if org else rdf['owner_org']
                        rdf['id'] = simple_name

                        data['dataset'].append(rdf)
                        rdf['keywords'] = rdf.get('keyword_string', [])

                    elif '/files' in file and not file.endswith('/'):

                        if not os.path.join(WORKDIR, 'files'):
                            os.makedirs(os.path.join(WORKDIR, 'files'))
                        if not os.path.join(
                                WORKDIR + '/files',
                                data['manifest'][0].get('package_id')):
                            os.makedirs(
                                os.path.join(
                                    WORKDIR + '/files',
                                    data['manifest'][0].get('package_id')))
                        local_path = zf.extract(
                            file,
                            os.path.join(
                                WORKDIR + '/files',
                                data['manifest'][0].get('package_id')))
                        upload_file = {
                            'name': os.path.basename(file),
                            'path': file,
                            'local_path': local_path
                        }
                        data['files'].append(upload_file)

        for package in data.get('dataset', []):
            if package.get('groups'):
                glist = []
                for value in package.get('groups'):
                    glist.extend([result for result in value.values()])
                groups = ingestion.get_groups_from_database_by_title(glist)
                package['groups'] = groups

        for item in plugins.PluginImplementations(plugins.IPackageController):
            new_item = []
            for rdf in data['dataset']:
                new_item.append(item.before_view(rdf))
            data['dataset'] = new_item

        c.pkg_dict = data
        # for key, value in c.pkg_dict.get('datasets/EAC_Erasmus_mobility_statistics_2012_2013-odp.rdf').get('rdf:RDF').get('dcat:Dataset').iteritems():
        #    if 'dct:title' == key:
        #        new_lang = value.get('@xml:lang')
        #        new_text = value.get('#text')

        errors = {}
        error_summary = {}

        data['resources_types'] = dataset.RESOURCES_TYPES
        tags = logic.get_action('tag_list')(
            context, {
                'vocabulary_id': u'concepts_eurovoc'
            })
        c.resources_types_documentation = dataset.RESOURCES_TYPES_DOCUMENTATION
        c.resources_types_distribution = dataset.RESOURCES_TYPES_DISTRIBUTION
        c.resources_types_visualization = dataset.RESOURCES_TYPES_VISUALIZATION

        vars = {
            'data': data,
            'errors': errors,
            'error_summary': error_summary,
            'action': 'manage_package'
        }

        self._setup_template_variables(context, {}, package_type=package_type)
        c.form = base.render(
            self._package_form(package_type='ingestion_package'),
            extra_vars=vars)
        return base.render('user/manage_base.html')
示例#6
0
            facets = OrderedDict()

            default_facet_titles = {'groups': _('Groups'),
                                    'tags': _('Tags'),
                                    'res_format': _('Formats'),
                                    'license': _('Licence')}

            for facet in g.facets:
                if facet in default_facet_titles:
                    facets[facet] = default_facet_titles[facet]
                else:
                    facets[facet] = facet

            # Facet titles
            for plugin in plugins.PluginImplementations(plugins.IFacets):
                if self.group_type == 'organization':
                    facets = plugin.organization_facets(
                        facets, self.group_type, None)
                else:
                    facets = plugin.group_facets(
                        facets, self.group_type, None)

            if 'capacity' in facets and (self.group_type != 'organization' or
                                         not user_member_of_orgs):
                del facets['capacity']

            c.facet_titles = facets

            data_dict = {
                'q': q,
示例#7
0
def resource_create(context, data_dict):
    user = context['user']
    dataset = None  # type: DatasetDcatApOp
    active_cache = config.get('ckan.cache.active', 'false')
    _check_access('package_update', context, data_dict)

    pkg = pkg_dict = logic.get_action('package_show')(
        context, {
            'id': data_dict.pop('package_id', '')
        })
    dataset = context['package']

    old_dataset = pickle.dumps(dataset)
    try:
        dataset = dataset_transition_util.update_dataset_for_package_dict(
            dataset, data_dict)
        dataset = dataset_transition_util.update_resources_for_dataset(
            [data_dict], dataset, dataset)
    except ValidationError as e:
        import traceback
        log.error('{0}'.format(e))
        log.error(traceback.print_exc())
        raise e
    except Exception as e:
        import traceback
        log.error(traceback.print_exc())
        raise ValidationError('Could {0} not transform to new model'.format(
            dataset.dataset_uri))
        # old_data_dict = logic.get_action('package_show')(context, {'id': data_dict.get('id')})
        # old_dataset = context['package']  # type: DatasetDcatApOp
    start = time.time()
    dataset, errors = validation.validate_dacat_dataset(dataset, context)
    context['errors'] = errors
    log.info('validation took {0} sec'.format(time.time() - start))
    # TODO check the business rule of save
    if errors.get('fatal'):
        # dataset.privacy_state = DCATAPOP_PRIVATE_DATASET
        # dataset.add_draft_to_title()
        raise ValidationError(errors)
    elif errors.get(
            'error') and dataset.privacy_state == DCATAPOP_PUBLIC_DATASET:
        # dataset.privacy_state = DCATAPOP_PRIVATE_DATASET
        # dataset.add_draft_to_title()
        raise ValidationError(errors)
    elif errors.get(
            'error') and dataset.privacy_state == DCATAPOP_PRIVATE_DATASET:
        # dataset.add_draft_to_title()
        pass

    rev = model.repo.new_revision()
    rev.author = user
    if 'message' in context:
        rev.message = context['message']
    else:
        rev.message = _(
            u'REST API: Update object %s') % dataset.dataset_uri.split('/')[-1]

    try:
        result = dataset.save_to_ts(rev.id)
    except BaseException as e:
        log.error('Error while saving the package {0} to Virtuoso.'.format(
            dataset.dataset_uri))
        model.repo.rollback()
        raise ActionError(
            'Error while saving the package {0} to Virtuoso.'.format(
                dataset.dataset_uri))

    context_org_update = context.copy()
    context_org_update['ignore_auth'] = True
    context_org_update['defer_commit'] = True

    for item in lib_plugins.PluginImplementations(
            lib_plugins.IPackageController):
        item.edit(dataset)

        item.after_update(context, dataset)

    log.debug('Updated object %s' % dataset.dataset_uri)

    return_id_only = context.get('return_id_only', False)

    # Make sure that a user provided schema is not used on package_show
    context.pop('schema', None)

    if dataset.privacy_state == 'public' and active_cache == 'true':
        redis_cache.set_value_no_ttl_in_cache(dataset.dataset_uri,
                                              pickle.dumps(dataset))
    else:
        redis_cache.delete_value_from_cache(dataset.dataset_uri)

    try:
        redis_cache.flush_all_from_db(redis_cache.MISC_POOL)
        search.rebuild(dataset.dataset_uri.split('/')[-1])
    except Exception as e:
        log.error("Error while index the package {0} to Solr".format(
            dataset.dataset_uri))
        old_dataset = pickle.loads(old_dataset)
        dataset.schema = old_dataset.schema
        dataset.schema_catalog_record = old_dataset.schema_catalog_record
        dataset.privacy_state = old_dataset.privacy_state
        dataset.save_to_ts()
        search.rebuild(dataset.dataset_uri.split('/')[-1])
        model.repo.rollback()
        raise ActionError('Error while index the package {0} to Solr.'.format(
            dataset.dataset_uri))

    if not context.get('defer_commit'):
        model.repo.commit()

    for item in lib_plugins.PluginImplementations(
            lib_plugins.IResourceUrlChange):
        if item.name != 'qa':
            item.notify(dataset,
                        model.domain_object.DomainObjectOperation.changed)

    # we could update the dataset so we should still be able to read it.
    context['ignore_auth'] = True
    return_id_only = context.get('return_id_only', False)

    output = _get_action('legacy_package_show')(context, {
        'uri': dataset.dataset_uri
    })

    return output