示例#1
0
    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)
示例#2
0
    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)
示例#3
0
def upsert_resource_multilang(res_id, field_name, lang, text):
    try:
        from ckanext.multilang.model import ResourceMultilang
    except ImportError:
        log.warn('DCAT-AP_IT: multilang extension not available.')
        return

    ml = ResourceMultilang.get_for_pk(res_id, field_name, lang)
    if not ml and text:
        ResourceMultilang.persist_resources([ResourceMultilang(res_id, field_name, lang, text)])
    elif ml and not text:
        ml.purge()
    elif ml and not ml.text == text:
        ml.text = text
        ml.save()
示例#4
0
def get_localized_resource(resource_dict):
    #  MULTILANG - Localizing resource dict
    lang = getLanguage()

    # 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
示例#5
0
    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
示例#6
0
def get_for_resource(res_id):
    '''
    Returns all the localized fields of a dataset's resources, in a dict of dicts, i.e.:
         {FIELDNAME:{LANG:label, ...}, ...}

    Returns None if multilang extension not loaded.
    '''

    try:
        from ckanext.multilang.model import ResourceMultilang
    except ImportError:
        log.warn('DCAT-AP_IT: multilang extension not available.')

        return None

    records = ResourceMultilang.get_for_resource_id(res_id)
    return _multilang_to_dict(records)
示例#7
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