コード例 #1
0
ファイル: plugin.py プロジェクト: riccardo01/dati-ckan-docker
    def create(self, model_obj):
        otype = model_obj.type
        lang = helpers.getLanguage()

        ## CREATE GROUP OR ORGANIZATION
        if otype == 'group' or otype == 'organization' and lang:
            log.info('::::: Persisting localised metadata locale :::::')
            lang = helpers.getLanguage()

            group = model_dictize.group_dictize(model_obj, {'model': model, 'session': model.Session})

            GroupMultilang.persist(group, lang)
コード例 #2
0
ファイル: plugin.py プロジェクト: jenkin/ckanext-multilang
    def after_update(self, context, resource):
        otype = resource.get('type')
        lang = helpers.getLanguage()

        if otype != 'dataset' and lang:
            # r = model.Session.query(model.Resource).filter(model.Resource.id == resource.get('id')).all()
            r = model.Resource.get(resource.get('id'))
            if r:
                r = model_dictize.resource_dictize(r, {
                    'model': model,
                    'session': model.Session
                })

                # MULTILANG - persisting resource localized record in multilang table
                # q_results = model.Session.query(ResourceMultilang).filter(ResourceMultilang.resource_id == resource.get('id'), ResourceMultilang.lang == lang).all()
                q_results = ResourceMultilang.get_for_resource_id_and_lang(
                    r.get('id'), lang)
                if q_results and q_results.count() > 0:
                    for result in q_results:
                        result.text = r.get(result.field)
                        result.save()
                else:
                    log.info(
                        'Localised fields are missing in resource_multilang table, persisting ...'
                    )
                    ResourceMultilang.persist(r, lang)
コード例 #3
0
ファイル: plugin.py プロジェクト: riccardo01/dati-ckan-docker
    def after_create(self, context, resource):
        otype = resource.get('type')
        lang = helpers.getLanguage()

        if otype != 'dataset' and lang:
            #  MULTILANG - Creating new resource for multilang table
            # r = model.Session.query(model.Resource).filter(model.Resource.id == resource.get('id')).all()
            r = model.Resource.get(resource.get('id'))
            if r:
                log.info('Localised fields are missing in resource_multilang table, persisting ...')
                ResourceMultilang.persist(resource, lang)
コード例 #4
0
ファイル: plugin.py プロジェクト: riccardo01/dati-ckan-docker
    def before_show(self, resource_dict):
        lang = helpers.getLanguage()
        if lang:            
            #  MULTILANG - Localizing resources dict
            # q_results = model.Session.query(ResourceMultilang).filter(ResourceMultilang.resource_id == resource_dict.get('id'), ResourceMultilang.lang == lang).all()
            q_results = ResourceMultilang.get_for_resource_id_and_lang(resource_dict.get('id'), lang)

            if q_results:
                for result in q_results:
                    resource_dict[result.field] = result.text

        return resource_dict
コード例 #5
0
ファイル: plugin.py プロジェクト: jenkin/ckanext-multilang
    def edit(self, model_obj):
        otype = model_obj.type
        lang = helpers.getLanguage()

        ## EDIT GROUP OR ORGANIZATION
        if otype == 'group' or otype == 'organization' and lang:
            group = model_dictize.group_dictize(model_obj, {
                'model': model,
                'session': model.Session
            })

            # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == group.get('id')).all()
            q_results = GroupMultilang.get_for_group_id(group.get('id'))

            create_new = False
            if q_results:
                available_db_lang = []

                for result in q_results:
                    if result.lang not in available_db_lang:
                        available_db_lang.append(result.lang)

                    # check if the group identifier name has been changed
                    if result.name != group.get('name'):
                        result.name = group.get('name')
                        result.save()

                if lang not in available_db_lang:
                    create_new = True
                else:
                    for result in q_results:
                        if result.lang == lang:
                            result.text = group.get(result.field)
                            result.save()
            else:
                create_new = True

            if create_new == True:
                log.info(
                    ':::::::::::: Localized fields are missing in package_multilang table, persisting defaults using values in the table group :::::::::::::::'
                )
                GroupMultilang.persist(group, lang)
コード例 #6
0
ファイル: plugin.py プロジェクト: jenkin/ckanext-multilang
    def after_search(self, search_results, search_params):
        search_facets = search_results.get('search_facets')
        lang = helpers.getLanguage()

        if search_facets and lang:
            if 'tags' in search_facets:
                #  MULTILANG - Localizing Tags display names in Facet list
                tags = search_facets.get('tags')
                for tag in tags.get('items'):
                    localized_tag = TagMultilang.by_name(tag.get('name'), lang)

                    if localized_tag:
                        tag['display_name'] = localized_tag.text

            if 'organization' in search_facets:
                #  MULTILANG - Localizing Organizations display names in Facet list
                organizations = search_facets.get('organization')
                for org in organizations.get('items'):
                    # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.name == org.get('name'), GroupMultilang.lang == lang).all()
                    q_results = GroupMultilang.get_for_group_name_and_lang(
                        org.get('name'), lang)

                    if q_results:
                        for result in q_results:
                            if result.field == 'title':
                                org['display_name'] = result.text

            if 'groups' in search_facets:
                #  MULTILANG - Localizing Groups display names in Facet list
                groups = search_facets.get('groups')
                for group in groups.get('items'):
                    # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.name == group.get('name'), GroupMultilang.lang == lang).all()
                    q_results = GroupMultilang.get_for_group_name_and_lang(
                        group.get('name'), lang)

                    if q_results:
                        for result in q_results:
                            if result.field == 'title':
                                group['display_name'] = result.text

        search_results['search_facets'] = search_facets
        return search_results
コード例 #7
0
def recent_updates(n):
    #
    # Return a list of the n most recently updated datasets.
    #
    log.debug('::::: Retrieving latest datasets: %r' % n)
    context = {
        'model': model,
        'session': model.Session,
        'user': p.toolkit.c.user or p.toolkit.c.author
    }

    data_dict = {
        'rows': n,
        'sort': u'metadata_modified desc',
        'facet': u'false'
    }

    try:
        search_results = logic.get_action('package_search')(context, data_dict)
    except search.SearchError as e:
        log.error('Error searching for recently updated datasets')
        log.error(e)
        search_results = {}

    for item in search_results.get('results'):
        log.info(
            ':::::::::::: Retrieving the corresponding localized title and abstract :::::::::::::::'
        )

        lang = multilang_helpers.getLanguage()

        q_results = model.Session.query(PackageMultilang)\
                                 .filter(PackageMultilang.package_id == item.get('id'),
                                         PackageMultilang.lang == lang).all()

        if q_results:
            for result in q_results:
                item[result.field] = result.text

    return search_results.get('results', [])
コード例 #8
0
    def _save_new(self, context, package_type=None):
        # 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'])
                    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', '')

            #  MULTILANG - retrieving dict for localized tag's strings
            extra_tag = None
            if data_dict.get('extra_tag'):
                extra_tag = data_dict.get('extra_tag')
                # After saving in memory the extra_tag dict this must be removed because not present in the schema
                del data_dict['extra_tag']

            pkg_dict = get_action('package_create')(context, data_dict)

            lang = helpers.getLanguage()

            #  MULTILANG - persisting tags
            self.localized_tags_persist(extra_tag, pkg_dict, lang)

            # MULTILANG - persisting the localized package dict
            log.info('::::: Persisting localised metadata locale :::::')
            for field in self.pkg_localized_fields:
                if pkg_dict.get(field):
                    PackageMultilang.persist(
                        {
                            'id': pkg_dict.get('id'),
                            'field': field,
                            'text': pkg_dict.get(field)
                        }, lang)

            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(401, _('Unauthorized to read package %s') % '')
        except NotFound, e:
            abort(404, _('Dataset not found'))
コード例 #9
0
    def _save_edit(self, name_or_id, context, package_type=None):
        from ckan.lib.search import SearchIndexError
        log.debug('Package save request name: %s POST: %r', name_or_id,
                  request.POST)
        try:
            data_dict = clean_dict(
                dict_fns.unflatten(tuplize_dict(parse_params(request.POST))))
            if '_ckan_phase' in data_dict:
                # we allow partial updates to not destroy existing resources
                context['allow_partial_update'] = True
                if 'tag_string' in data_dict:
                    data_dict['tags'] = self._tag_string_to_list(
                        data_dict['tag_string'])
                del data_dict['_ckan_phase']
                del data_dict['save']
            context['message'] = data_dict.get('log_message', '')
            data_dict['id'] = name_or_id

            #  MULTILANG - retrieving dict for localized tag's strings
            extra_tag = None
            if data_dict.get('extra_tag'):
                extra_tag = data_dict.get('extra_tag')
                # After saving in memory the extra_tag dict this must be removed because not present in the schema
                del data_dict['extra_tag']

            pkg = get_action('package_update')(context, data_dict)

            c.pkg = context['package']
            c.pkg_dict = pkg

            lang = helpers.getLanguage()

            #  MULTILANG - persisting tags
            self.localized_tags_persist(extra_tag, c.pkg_dict, lang)

            #  MULTILANG - persisting package dict
            log.info(
                ':::::::::::: Saving the corresponding localized title and abstract :::::::::::::::'
            )

            # q_results = model.Session.query(PackageMultilang).filter(PackageMultilang.package_id == c.pkg_dict.get('id'), PackageMultilang.lang == lang).all()
            q_results = PackageMultilang.get_for_package_id_and_lang(
                c.pkg_dict.get('id'), lang)

            if q_results:
                pkg_processed_field = []
                for result in q_results:
                    # do not update multilang field if original pkg dict doesn't
                    # have this field anymore. otherwise IntegrityError will raise
                    # because text will be None
                    if c.pkg_dict.has_key(result.field):
                        pkg_processed_field.append(result.field)
                        log.debug('::::::::::::::: value before %r',
                                  result.text)
                        result.text = c.pkg_dict.get(result.field)
                        log.debug('::::::::::::::: value after %r',
                                  result.text)
                        result.save()

                # Check for missing localized fields in DB
                for field in self.pkg_localized_fields:
                    if field not in pkg_processed_field:
                        if c.pkg_dict.get(field):
                            PackageMultilang.persist(
                                {
                                    'id': c.pkg_dict.get('id'),
                                    'field': field,
                                    'text': c.pkg_dict.get(field)
                                }, lang)
            else:
                log.info(
                    ':::::::::::: Localised fields are missing in package_multilang table, persisting defaults using values in the table package :::::::::::::::'
                )
                for field in self.pkg_localized_fields:
                    if c.pkg_dict.get(field):
                        PackageMultilang.persist(
                            {
                                'id': c.pkg_dict.get('id'),
                                'field': field,
                                'text': c.pkg_dict.get(field)
                            }, lang)

            self._form_save_redirect(pkg['name'],
                                     'edit',
                                     package_type=package_type)
        except NotAuthorized:
            abort(401, _('Unauthorized to read package %s') % id)
        except NotFound, e:
            abort(404, _('Dataset not found'))
コード例 #10
0
ファイル: plugin.py プロジェクト: jenkin/ckanext-multilang
    def before_view(self, odict):
        otype = odict.get('type')
        lang = helpers.getLanguage()

        if lang:
            if otype == 'group' or otype == 'organization':
                #  MULTILANG - Localizing Group/Organizzation names and descriptions in search list
                # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == odict.get('id'), GroupMultilang.lang == lang).all()
                q_results = GroupMultilang.get_for_group_id_and_lang(
                    odict.get('id'), lang)

                if q_results:
                    for result in q_results:
                        odict[result.field] = result.text
                        if result.field == 'title':
                            odict['display_name'] = result.text

            elif otype == 'dataset':
                #  MULTILANG - Localizing Datasets names and descriptions in search list
                #  MULTILANG - Localizing Tags display names in Facet list
                tags = odict.get('tags')
                if tags:
                    for tag in tags:
                        localized_tag = TagMultilang.by_tag_id(
                            tag.get('id'), lang)

                        if localized_tag:
                            tag['display_name'] = localized_tag.text

                #  MULTILANG - Localizing package sub dict for the dataset read page
                # q_results = model.Session.query(PackageMultilang).filter(PackageMultilang.package_id == odict['id'], PackageMultilang.lang == lang).all()
                q_results = PackageMultilang.get_for_package_id_and_lang(
                    odict.get('id'), lang)

                if q_results:
                    for result in q_results:
                        if odict.get(result.field, None):
                            odict[result.field] = result.text
                        else:
                            extras = odict.get('extras', None)
                            if extras and len(extras) > 0:
                                for extra in extras:
                                    extra_key = extra.get('key', None)
                                    if extra_key and extra_key == result.field:
                                        extra['value'] = result.text

                    #if result.field == 'notes':
                    #    odict['notes'] = result.text

                #  MULTILANG - Localizing organization sub dict for the dataset read page
                organization = odict.get('organization')
                if organization:
                    # q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == organization.get('id'), GroupMultilang.lang == lang).all()
                    q_results = GroupMultilang.get_for_group_id_and_lang(
                        organization.get('id'), lang)

                    if q_results:
                        for result in q_results:
                            organization[result.field] = result.text

                    odict['organization'] = organization

                #  MULTILANG - Localizing resources dict
                resources = odict.get('resources')
                if resources:
                    for resource in resources:
                        # q_results = model.Session.query(ResourceMultilang).filter(ResourceMultilang.resource_id == resource.get('id'), ResourceMultilang.lang == lang).all()
                        q_results = ResourceMultilang.get_for_resource_id_and_lang(
                            resource.get('id'), lang)

                        if q_results:
                            for result in q_results:
                                resource[result.field] = result.text

        return odict
コード例 #11
0
def _group_or_org_list(context, data_dict, is_org=False):
    model = context['model']
    api = context.get('api_version')
    groups = data_dict.get('groups')
    group_type = data_dict.get('type', 'group')
    ref_group_by = 'id' if api == 2 else 'name'

    pagination_dict = {}
    limit = data_dict.get('limit')
    if limit:
        pagination_dict['limit'] = data_dict['limit']
    offset = data_dict.get('offset')
    if offset:
        pagination_dict['offset'] = data_dict['offset']
    if pagination_dict:
        pagination_dict, errors = _validate(
            data_dict, logic.schema.default_pagination_schema(), context)
        if errors:
            raise ValidationError(errors)

    sort = data_dict.get('sort') or 'name'
    q = data_dict.get('q')

    # order_by deprecated in ckan 1.8
    # if it is supplied and sort isn't use order_by and raise a warning
    order_by = data_dict.get('order_by', '')
    if order_by:
        log.warn('`order_by` deprecated please use `sort`')
        if not data_dict.get('sort'):
            sort = order_by

    # if the sort is packages and no sort direction is supplied we want to do a
    # reverse sort to maintain compatibility.
    if sort.strip() in ('packages', 'package_count'):
        sort = 'package_count desc'

    sort_info = _unpick_search(
        sort,
        allowed_fields=['name', 'packages', 'package_count', 'title'],
        total=1)

    all_fields = data_dict.get('all_fields', None)
    include_extras = all_fields and \
                     asbool(data_dict.get('include_extras', False))

    query = model.Session.query(model.Group)

    if include_extras:
        # this does an eager load of the extras, avoiding an sql query every
        # time group_list_dictize accesses a group's extra.
        query = query.options(sqlalchemy.orm.joinedload(model.Group._extras))

    query = query.filter(model.Group.state == 'active')
    if groups:
        query = query.filter(model.Group.name.in_(groups))
    if q:
        q = u'%{0}%'.format(q)

        ## MULTILANG FRAGMENT
        lang = helpers.getLanguage()
        groups_multilang_id_list = []

        q_results = model.Session.query(GroupMultilang).filter(
            GroupMultilang.lang.ilike(lang),
            GroupMultilang.text.ilike(q)).all()

        if q_results:
            for result in q_results:
                log.info(":::::::::::::: Group ID Found:::: %r",
                         result.group_id)
                groups_multilang_id_list.append(result.group_id)

            groups_multilang_id_list = set(groups_multilang_id_list)

        if len(groups_multilang_id_list) > 0:
            #query = query.filter(model.Group.id.in_(groups_multilang_id_list))
            query = query.filter(
                _or_(model.Group.name.ilike(q), model.Group.title.ilike(q),
                     model.Group.description.ilike(q),
                     model.Group.id.in_(groups_multilang_id_list)))
        else:
            query = query.filter(
                _or_(model.Group.name.ilike(q), model.Group.title.ilike(q),
                     model.Group.description.ilike(q)))
        ## END OF MULTILANG FRAGMENT

    query = query.filter(model.Group.is_organization == is_org)
    if not is_org:
        query = query.filter(model.Group.type == group_type)
    if sort_info:
        sort_field = sort_info[0][0]
        sort_direction = sort_info[0][1]
        if sort_field == 'package_count':
            query = query.group_by(model.Group.id, model.Group.name)
            sort_model_field = sqlalchemy.func.count(model.Group.id)
        elif sort_field == 'name':
            sort_model_field = model.Group.name
        elif sort_field == 'title':
            sort_model_field = model.Group.title

        if sort_direction == 'asc':
            query = query.order_by(sqlalchemy.asc(sort_model_field))
        else:
            query = query.order_by(sqlalchemy.desc(sort_model_field))

    if limit:
        query = query.limit(limit)
    if offset:
        query = query.offset(offset)

    groups = query.all()

    if all_fields:
        action = 'organization_show' if is_org else 'group_show'
        group_list = []
        for group in groups:
            data_dict['id'] = group.id
            for key in ('include_extras', 'include_tags', 'include_users',
                        'include_groups', 'include_followers'):
                if key not in data_dict:
                    data_dict[key] = False

            group_list.append(logic.get_action(action)(context, data_dict))
    else:
        group_list = [getattr(group, ref_group_by) for group in groups]

    return group_list