示例#1
0
def get_localized_pkg(pkg_dict):
    if pkg_dict != '' and 'type' in pkg_dict:
        #  MULTILANG - Localizing package dict
        lang = getLanguage()

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

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

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

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

        #  MULTILANG - Localizing organization sub dict for the dataset edit page
        organization = pkg_dict.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

            pkg_dict['organization'] = organization

    return pkg_dict
示例#2
0
def getLocalizedTagName(tag_name=None):
    if tag_name:
        lang = get_lang()[0]
        localized_tag_name = TagMultilang.by_name(tag_name, lang)
        if localized_tag_name:
            return localized_tag_name.text
        else:
            return None
    else:
        return None
示例#3
0
def getLocalizedTagName(tag_name=None):
    if tag_name:
        lang = get_lang()[0]
        localized_tag_name = TagMultilang.by_name(tag_name, lang)
        if localized_tag_name:
            return localized_tag_name.text
        else:
            return None
    else:
        return None
示例#4
0
    def localized_tags_persist(self, extra_tag, pkg_dict, lang):
        if extra_tag:
            for tag in extra_tag:
                localized_tag = TagMultilang.by_name(tag.get('key'), lang)

                if localized_tag and localized_tag.text != tag.get('value'):
                    localized_tag.text = tag.get('value')
                    localized_tag.save()
                elif localized_tag is None:
                    # Find the tag id from the existing tags in dict
                    tag_id = None
                    for dict_tag in pkg_dict.get('tags'):
                        if dict_tag.get('name') == tag.get('key'):
                            tag_id = dict_tag.get('id')

                    if tag_id:
                        TagMultilang.persist(
                            {
                                'id': tag_id,
                                'name': tag.get('key'),
                                'text': tag.get('value')
                            }, lang)
示例#5
0
    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
示例#6
0
    def after_import_stage(self, package_dict):
        log.info(
            '::::::::: Performing after_import_stage  persist operation for localised dataset content :::::::::'
        )

        if bool(self._package_dict):
            session = Session

            package_id = package_dict.get('id')

            # Persisting localized packages
            try:
                # rows = session.query(PackageMultilang).filter(PackageMultilang.package_id == package_id).all()
                rows = PackageMultilang.get_for_package(package_id)

                if not rows:
                    log.info(
                        '::::::::: Adding new localised object to the package_multilang table :::::::::'
                    )

                    log.debug('::::: Persisting default metadata locale :::::')

                    loc_titles = self._package_dict.get('localised_titles')
                    if loc_titles:
                        log.debug('::::: Persisting title locales :::::')
                        for title in loc_titles:
                            PackageMultilang.persist(
                                {
                                    'id': package_id,
                                    'text': title.get('text'),
                                    'field': 'title'
                                }, title.get('locale'))

                    loc_abstracts = self._package_dict.get(
                        'localised_abstracts')
                    if loc_abstracts:
                        log.debug('::::: Persisting abstract locales :::::')
                        for abstract in loc_abstracts:
                            PackageMultilang.persist(
                                {
                                    'id': package_id,
                                    'text': abstract.get('text'),
                                    'field': 'notes'
                                }, abstract.get('locale'))

                    log.info(
                        '::::::::: OBJECT PERSISTED SUCCESSFULLY :::::::::')

                else:
                    log.info(
                        '::::::::: Updating localised object in the package_multilang table :::::::::'
                    )
                    for row in rows:
                        if row.field == 'title':
                            titles = self._package_dict.get('localised_titles')
                            if titles:
                                for title in titles:
                                    if title.get('locale') == row.lang:
                                        row.text = title.get('text')
                        elif row.field == 'notes':
                            abstracts = self._package_dict.get(
                                'localised_abstracts')
                            if abstracts:
                                for abstract in abstracts:
                                    if abstract.get('locale') == row.lang:
                                        row.text = abstract.get('text')

                        row.save()

                    log.info('::::::::: OBJECT UPDATED SUCCESSFULLY :::::::::')

                pass
            except Exception, e:
                # on rollback, the same closure of state
                # as that of commit proceeds.
                session.rollback()

                log.error('Exception occurred while persisting DB objects: %s',
                          e)
                raise

            # Persisting localized Tags

            loc_tags = self._package_dict.get('localized_tags')
            if loc_tags:
                log.debug('::::: Persisting tag locales :::::')
                for tag in loc_tags:
                    tag_name = tag.get('text')
                    tag_lang = tag.get('locale')
                    tag_localized_name = tag.get('localized_text')

                    tag = TagMultilang.by_name(tag_name, tag_lang)

                    if tag:
                        # Update the existing record
                        if tag_localized_name and tag_localized_name != tag.text:
                            tag.text = tag_localized_name

                            try:
                                tag.save()
                                log.info(
                                    '::::::::: OBJECT TAG UPDATED SUCCESSFULLY :::::::::'
                                )
                                pass
                            except Exception, e:
                                # on rollback, the same closure of state
                                # as that of commit proceeds.
                                session.rollback()

                                log.error(
                                    'Exception occurred while persisting DB objects: %s',
                                    e)
                                raise
                    else:
                        # Create a new localized record
                        existing_tag = model.Tag.by_name(tag_name)

                        if existing_tag:
                            TagMultilang.persist(
                                {
                                    'id': existing_tag.id,
                                    'name': tag_name,
                                    'text': tag_localized_name
                                }, tag_lang)
                            log.info(
                                '::::::::: OBJECT TAG PERSISTED SUCCESSFULLY :::::::::'
                            )
    def _read(self, id, limit, group_type):
        ''' This is common code used by both read and bulk_process'''
        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):
            controller = lookup_group_controller(group_type)
            action = 'bulk_process' if c.action == 'bulk_process' else 'read'
            url = h.url_for(controller=controller, action=action, 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
            self._update_facet_titles(facets, group_type)

            if 'capacity' in facets and (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']

            lang = get_lang()[0]
            
            q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == c.group_dict.get('id'), GroupMultilang.lang == lang).all() 

            if q_results:
                for result in q_results:
                    c.group_dict[result.field] = result.text
                    if result.field == 'title':
                        c.group_dict['display_name'] = result.text
            
            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

            # MULTILANG - Localizing Organizations display names in Facet list
            organizations = c.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() 

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

            # MULTILANG - Localizing Groups display names in Facet list
            groups = c.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() 

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

             # MULTILANG - Localizing Tags display names in Facet list
            tags = c.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

            c.page.items = query['results']
            
            # MULTILANG - Localizing Datasets names and descriptions in search list
            log.info(':::::::::::: Retrieving the corresponding localized title and abstract :::::::::::::::')

            for item in c.page.items:
                lang = get_lang()[0]

                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
                
            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=[])
    def after_import_stage(self, package_dict):        
        log.info('::::::::: Performing after_import_stage  persist operation for localised dataset content :::::::::')

        if self._package_dict:            
            session = Session

            package_id = package_dict.get('id')
            
            # Persisting localized packages 
            try:
                rows = session.query(PackageMultilang).filter(PackageMultilang.package_id == package_id).all()

                if not rows:
                    log.info('::::::::: Adding new localised object to the package_multilang table :::::::::')
                    
                    log.debug('::::: Persisting default metadata locale :::::')

                    log.debug('::::: Persisting tile locales :::::')
                    for title in self._package_dict.get('localised_titles'):
                        session.add_all([
                            PackageMultilang(package_id=package_id, field='title', field_type='localized', lang=title.get('locale'), text=title.get('text')),
                        ])

                    log.debug('::::: Persisting abstract locales :::::')
                    for abstract in self._package_dict.get('localised_abstracts'):
                        session.add_all([
                            PackageMultilang(package_id=package_id, field='notes', field_type='localized', lang=abstract.get('locale'), text=abstract.get('text')),
                        ])

                    session.commit()

                    log.info('::::::::: OBJECT PERSISTED SUCCESSFULLY :::::::::')

                else:
                    log.info('::::::::: Updating localised object in the package_multilang table :::::::::')
                    for row in rows:
                        if row.field == 'title': 
                            titles = self._package_dict.get('localised_titles')
                            for title in titles:
                                if title.get('locale') == row.lang:
                                    row.text = title.get('text')
                        elif row.field == 'notes': 
                            abstracts = self._package_dict.get('localised_abstracts')
                            for abstract in abstracts:
                                if abstract.get('locale') == row.lang:
                                    row.text = abstract.get('text')

                        row.save()

                    log.info('::::::::: OBJECT UPDATED SUCCESSFULLY :::::::::') 

                pass
            except Exception, e:
                # on rollback, the same closure of state
                # as that of commit proceeds. 
                session.rollback()

                log.error('Exception occurred while persisting DB objects: %s', e)
                raise

            # Persisting localized Tags
            try:
                for tag in self._package_dict.get('localized_tags'):
                    tag_name = tag.get('text')
                    tag_lang = tag.get('locale')
                    tag_localized_name = tag.get('localized_text')

                    tag = TagMultilang.by_name(tag_name, tag_lang)

                    if tag:
                        # Update the existing record                        
                        if tag_localized_name and tag_localized_name != tag.text:
                            tag.text = tag_localized_name
                            tag.save()

                            log.info('::::::::: OBJECT TAG UPDATED SUCCESSFULLY :::::::::') 
                    else:
                        # Create a new localized record
                        existing_tag = model.Tag.by_name(tag_name)

                        if existing_tag:
                            session.add_all([
                                TagMultilang(tag_id=existing_tag.id, tag_name=tag_name, lang=tag_lang, text=tag_localized_name),
                            ])

                            log.info('::::::::: OBJECT TAG PERSISTED SUCCESSFULLY :::::::::')

                        session.commit()
                pass
            except Exception, e:
                # on rollback, the same closure of state
                # as that of commit proceeds. 
                session.rollback()

                log.error('Exception occurred while persisting DB objects: %s', e)
                raise
示例#9
0
    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